POJ 2159 Going Home 网络流 最小费用最大流
2016-05-12 16:01
706 查看
本题可以用网络流来解决,也可以用最大二分匹配,下面讲解一下网络流的思路。
题意是在一个地图中一些人要到一些房子,每个人都要找到一个房子,每个房子只能容纳一个人,人每移动一步有花费,问最少话费。
将人和房子都看成流量图中的节点,那么每个人到每个房子都有一条边,这条边的花费就是路径长度,因为每个房子的容量是1,因此这条边的容量也是1。这样就建出一个多源点多汇点的网络流量图,然后只需要在用一个超级源点指向每一个人,每一个房子指向一个超级汇点,花费都是0,容量都是1,再使用最小费用最大流算法计算即可。
Going Home
Description
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Sample Input
Sample Output
Source
Pacific Northwest 2004
查看原文:http://colorfulshark.cn/wordpress/go-home-1009.html
题意是在一个地图中一些人要到一些房子,每个人都要找到一个房子,每个房子只能容纳一个人,人每移动一步有花费,问最少话费。
将人和房子都看成流量图中的节点,那么每个人到每个房子都有一条边,这条边的花费就是路径长度,因为每个房子的容量是1,因此这条边的容量也是1。这样就建出一个多源点多汇点的网络流量图,然后只需要在用一个超级源点指向每一个人,每一个房子指向一个超级汇点,花费都是0,容量都是1,再使用最小费用最大流算法计算即可。
Going Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 20848 | Accepted: 10566 |
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Sample Input
2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
Sample Output
2 10 28
Source
Pacific Northwest 2004
#include <iostream> #include <stdio.h> #include <string.h> #include <queue> #include <math.h> using namespace std; const int MAXN=10000; const int MAXM=100000; const int INF=99999999; struct Edge { int to,next,cap,flow,cost; }; struct Home { int r,c; }; struct Man { int r,c; }; Edge edge[MAXM]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N;//点的总数,点的编号0-N-1 char mp[200][200]; Home home[MAXN]; Man man[MAXN]; int h_num,m_num; void init(int n) { //printf("N=%d\n",n); N=n; tol=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int cap,int cost) { edge[tol].to=v; edge[tol].cap=cap; edge[tol].cost=cost; edge[tol].flow=0; edge[tol].next=head[u]; head[u]=tol++; edge[tol].to=u; edge[tol].cap=0; edge[tol].cost=-cost; edge[tol].flow=0; edge[tol].next=head[v]; head[v]=tol++; } bool spfa(int s,int t) { memset(vis,false,sizeof(vis)); queue<int> q; while(!q.empty()) q.pop(); for(int i=0; i<N; i++) { dis[i]=INF; vis[i]=false; pre[i]=-1; } dis[s]=0; vis[s]=true; q.push(s); while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=false; for(int i=head[u]; i!=-1; i=edge[i].next) { int v=edge[i].to; if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost) { dis[v]=dis[u]+edge[i].cost; pre[v]=i; if(!vis[v]) { vis[v]=true; q.push(v); } } } } if(pre[t]==-1) return false; else return true; } int min_cost_flow(int s,int t)//源点和汇点 { int flow=0; int cost=0; while(spfa(s,t)) { int Min=INF; for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to]) { if(Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; } for(int i=pre[t]; i!=-1; i=pre[edge[i^1].to]) { edge[i].flow+=Min; edge[i^1].flow-=Min; cost+=edge[i].cost*Min; } flow+=Min; } return cost; } int getdis(int i,int j) { j=j-m_num-1; i--; Man m=man[i]; Home h=home[j]; return fabs(m.r-h.r)+fabs(m.c-h.c); } void show() { for(int i=0;i<N;i++) { printf("%d:",i); for(int j=head[i];j!=-1;j=edge[j].next) { int v=edge[j].to; printf(" %d",v); } printf("\n"); } } int main() { int r,c; int sink,source; int ans; while(~scanf("%d%d",&r,&c)&&(r||c)) { m_num=h_num=0; for(int i=0; i<r; i++) scanf("%s",mp[i]); for(int i=0; i<r; i++) { for(int j=0; j<c; j++) { if(mp[i][j]=='H') { home[h_num].r=i; home[h_num].c=j; ++h_num; } if(mp[i][j]=='m') { man[m_num].r=i; man[m_num].c=j; ++m_num; } } } source=0; sink=h_num+m_num+1; //printf("m_num=%d h_num=%d\n",m_num,h_num); init(sink+1); for(int i=1; i<=m_num; i++) addedge(source,i,1,0); for(int i=m_num+1; i<=m_num+h_num; i++) addedge(i,sink,1,0); for(int i=1; i<=m_num; i++) for(int j=m_num+1; j<=m_num+h_num; j++) addedge(i,j,1,getdis(i,j)); //show(); ans=min_cost_flow(source,sink); printf("%d\n",ans); } return 0; }
查看原文:http://colorfulshark.cn/wordpress/go-home-1009.html
相关文章推荐
- 《TCP/IP详解》学习笔记-第11章 UDP
- nginx将http重定向到https
- Tengine/nginx配置https/ssl/443端口健康检查
- angualrjs学习总结三(http、select、表格、指令以及事件)
- 网络流相关
- HTTP请求报文解剖
- 基于感知器模型的线性神经网络
- windows bat 获取特定ip地址对应的网络接口的名称
- TCP 建立连接和断开连接
- ajax+promise 封装http请求
- 转载本站文章请注明作者和出处 奇葩一朵朵 – http://www.cnblogs.com/season-huang/ ,请勿用于任何商业用途
- 【 总结 】crontab 使用脚本及直接获取HTTP状态码
- 关于神经网络学习的相关资源
- tomcat配置http digest认证
- Swift HTTP GET请求例子
- 基于感知器模型的线性神经网络
- Http响应和HttpServletResponse对应设置方法
- 机房网络规划
- 【UNIX网络编程】TCP客户/服务器程序示例
- 神经网络压缩:Deep Compression