Going Home(最小费用最大流)
Going Home
http://poj.org/problem?id=2195
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26135 | Accepted: 13106 |
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.
![](http://poj.org/images/2195_1.jpg)
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.
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.
![](http://poj.org/images/2195_1.jpg)
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
最小费用最大流模板题
每个点连相邻的边建图
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<algorithm> 3 #include<queue> 4 #include<cstring> 5 using namespace std; 6 7 const int INF=0x3f3f3f3f; 8 const int N=500005; 9 const int M=500005; 10 int top; 11 int dist[N],pre[N]; 12 bool vis[N]; 13 int c[N]; 14 int maxflow; 15 16 struct Vertex{ 17 int first; 18 }V[N]; 19 struct Edge{ 20 int v,next; 21 int cap,flow,cost; 22 }E[M]; 23 24 void init(){ 25 memset(V,-1,sizeof(V)); 26 top=0; 27 maxflow=0; 28 } 29 30 void add_edge(int u,int v,int c,int cost){ 31 E[top].v=v; 32 E[top].cap=c; 33 E[top].flow=0; 34 E[top].cost=cost; 35 E[top].next=V[u].first; 36 V[u].first=top++; 37 } 38 39 void add(int u,int v,int c,int cost){ 40 add_edge(u,v,c,cost); 41 add_edge(v,u,0,-cost); 42 } 43 44 bool SPFA(int s,int t,int n){ 45 int i,u,v; 46 queue<int>qu; 47 memset(vis,false,sizeof(vis)); 48 memset(c,0,sizeof(c)); 49 memset(pre,-1,sizeof(pre)); 50 for(i=1;i<=n;i++){ 51 dist[i]=INF; 52 } 53 vis[s]=true; 54 c[s]++; 55 dist[s]=0; 56 qu.push(s); 57 while(!qu.empty()){ 58 u=qu.front(); 59 qu.pop(); 60 vis[u]=false; 61 for(i=V[u].first;~i;i=E[i].next){ 62 v=E[i].v; 63 if(E[i].cap>E[i].flow&&dist[v]>dist[u]+E[i].cost){ 64 dist[v]=dist[u]+E[i].cost; 65 pre[v]=i; 66 if(!vis[v]){ 67 c[v]++; 68 qu.push(v); 69 vis[v]=true; 70 if(c[v]>n){ 71 return false; 72 } 73 } 74 } 75 } 76 } 77 if(dist[t]==INF){ 78 return false; 79 } 80 return true; 81 } 82 83 int MCMF(int s,int t,int n){ 84 int d; 85 int i,mincost; 86 mincost=0; 87 while(SPFA(s,t,n)){ 88 d=INF; 89 for(i=pre[t];~i;i=pre[E[i^1].v]){ 90 d=min(d,E[i].cap-E[i].flow); 91 } 92 maxflow+=d; 93 for(i=pre[t];~i;i=pre[E[i^1].v]){ 94 E[i].flow+=d; 95 E[i^1].flow-=d; 96 } 97 mincost+=dist[t]*d; 98 } 99 return mincost; 100 } 101 string mp[105]; 102 int dir[4][2]={0,1,1,0,0,-1,-1,0}; 103 int main(){ 104 int n,m; 105 int v,u,w,c; 106 int s,t; 107 while(cin>>n>>m){ 108 if(!n&&!m) break; 109 init(); 110 for(int i=0;i<n;i++){ 111 cin>>mp[i]; 112 } 113 s=0,t=n*m+1; 114 for(int i=0;i<n;i++){ 115 for(int j=0;j<m;j++){ 116 if(mp[i][j]=='m'){ 117 add(s,i*m+j+1,1,0); 118 } 119 else if(mp[i][j]=='H'){ 120 add(i*m+j+1,t,1,0); 121 } 122 } 123 } 124 int xx,yy; 125 for(int i=0;i<n;i++){ 126 for(int j=0;j<m;j++){ 127 for(int k=0;k<4;k++){ 128 xx=i+dir[k][0]; 129 yy=j+dir[k][1]; 130 if(xx>=0&&xx<n&&yy>=0&&yy<m){ 131 add(i*m+j+1,xx*m+yy+1,INF,1); 132 } 133 } 134 } 135 } 136 int ans=MCMF(s,t,t+1); 137 cout<<ans<<endl; 138 } 139 }
posted on 2018-11-19 14:07 Fighting_sh 阅读(249) 评论(0) 编辑 收藏 举报