Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4852 Accepted Submission(s): 2545
Total Submission(s): 4852 Accepted Submission(s): 2545
Problem 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.
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 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1533; 想不一下最小费用流的题目,结果自己写模板写半天,最后还tlm,好尴尬~ 仅此留下代码留念,目前还不知道自己的为何超时。 tlm 代码:#include<bits/stdc++.h> using namespace std; const int INF=1e9+7; char s[111][111]; int head[11111],len; struct node { int x,y; } men[1111],house[1111]; struct Edge { int u,v,c,w,next; } edge[111111]; void addedge(int u,int v,int c,int w) { edge[len].u=u; edge[len].v=v; edge[len].c=c; edge[len].w=w; edge[len].next=head[u]; head[u]=len++; edge[len].u=v; edge[len].v=u; edge[len].c=-c; edge[len].w=0; edge[len].next=head[v]; head[v]=len++; } int d[11111],vis[1111],a[11111],p[1111],u[1111]; int spfa(int s,int t,int &cost,int &flow) { for(int i=0; i<=t; i++) d[i]=INF; d[s]=0; vis[s]=1; queue<int>q; q.push(s); a[s]=INF; u[s]++; memset(u,0,sizeof(u)); while(!q.empty()) { int nownode=q.front(); q.pop(); vis[nownode]=0; for(int i=head[nownode]; i!=-1; i=edge[i].next) { if(edge[i].w>0&&d[edge[i].v]>d[edge[i].u]+edge[i].c) { d[edge[i].v]=d[edge[i].u]+edge[i].c; a[edge[i].v]=min(a[edge[i].u],edge[i].w); p[edge[i].v]=i; if(!vis[edge[i].v]) { q.push(edge[i].v); u[edge[i].v]++; vis[edge[i].v]=1; } } } } if(d[t]==INF) return false; flow+=a[t]; cost+=d[t]*a[t]; int u=t; for(int i=p[u];i!=s;i=p[edge[i].u]) { edge[i].w-=a[t]; edge[i^1].w+=a[t]; } return true; } int MCMF(int s,int t) { int cost=0,flow=0; while(spfa(s,t,cost,flow)); return cost; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF&&(n+m)) { len=1; memset(head,-1,sizeof(head)); for(int i=0; i<n; i++) scanf("%s",s[i]); int menlen=0,houselen=0; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(s[i][j]=='H') { house[houselen].x=i; house[houselen++].y=j; } if(s[i][j]=='m') { men[menlen].x=i; men[menlen++].y=j; } } } int s=0,t=menlen*2+1; for(int i=1; i<=menlen; i++) addedge(0,i,0,1); for(int i=menlen+1; i<=menlen*2; i++) addedge(i,t,0,1); for(int i=0; i<menlen; i++) { for(int j=0; j<menlen; j++) { int distance=fabs(men[i].x-house[j].x)+fabs(men[i].y-house[j].y); addedge(i+1,j+1+menlen,distance,distance); } } printf("%d\n",MCMF(0,t)); } return 0; }
别人ac代码
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<vector> #include<queue> #define inf 0x7fffffff using namespace std; const int maxn=10000+100; const int M = 40000+100; int n,m,from,to; struct node { int v,flow,cost; int next; }edge[M*4]; int head[maxn],edgenum; int dis[maxn],pre[maxn],pid[maxn],vis[maxn]; void add(int u,int v,int flow,int cost) { edge[edgenum].v=v ;edge[edgenum].flow=flow ; edge[edgenum].cost=cost ;edge[edgenum].next=head[u]; head[u]=edgenum++; edge[edgenum].v=u ;edge[edgenum].flow=0; edge[edgenum].cost=-cost ;edge[edgenum].next=head[v]; head[v]=edgenum++; } int spfa() { for (int i=1 ;i<=to ;i++) { dis[i]=inf; vis[i]=0; } queue<int> Q; Q.push(from); dis[from]=0; vis[from]=1; while (!Q.empty()) { int u=Q.front() ;Q.pop() ; vis[u]=0; for (int i=head[u] ;i!=-1 ;i=edge[i].next) { int v=edge[i].v; if (edge[i].flow>0 && dis[v]>dis[u]+edge[i].cost) { dis[v]=dis[u]+edge[i].cost; pre[v]=u; pid[v]=i; if (!vis[v]) { vis[v]=1; Q.push(v); } } } } return dis[to]; } int mincost() { int aug=0,maxflow=0; int ans=0,tmp=0; while (1) { tmp=spfa(); if (tmp==inf) break; aug=inf; for (int i=to ;i!=from ;i=pre[i]) { if (edge[pid[i] ].flow<aug) aug=edge[pid[i] ].flow; } for (int i=to ;i!=from ;i=pre[i]) { edge[pid[i] ].flow -= aug; edge[pid[i]^1 ].flow += aug; } maxflow += aug; ans += tmp; } return ans; } int main() { while (scanf("%d%d",&n,&m)!=EOF) { if (!n && !m) break; memset(head,-1,sizeof(head)); edgenum=0; char str[111][111]; memset(str,0,sizeof(str)); for (int i=1 ;i<=n ;i++) { scanf("%s",str[i]+1); } from=n*m+1; to=from+1; int h[111],c[111],cnt=0; int h2[111],c2[111],cnt2=0; for (int i=1 ;i<=n ;i++) { for (int j=1 ;j<=m ;j++) { if (str[i][j]=='m') { add(from,(i-1)*m+j,1,0); h[cnt]=i ;c[cnt]=j ;cnt++ ; } else if (str[i][j]=='H') { add((i-1)*m+j,to,1,0); h2[cnt2]=i ;c2[cnt2]=j ;cnt2++ ; } } } for (int i=0 ;i<cnt ;i++) { for (int j=0 ;j<cnt2 ;j++) add((h[i]-1)*m+c[i],(h2[j]-1)*m+c2[j],1,abs(h[i]-h2[j])+abs(c[i]-c2[j])); } printf("%d\n",mincost()); } return 0; } 那以后就用这份模板好了,在解决自己问题代码之前。