Going Home
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5814 Accepted Submission(s): 3073
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.
![](https://i-blog.csdnimg.cn/blog_migrate/0d77b0d8574d5186f189f53f1ecd55a5.jpeg)
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.
![](https://i-blog.csdnimg.cn/blog_migrate/0d77b0d8574d5186f189f53f1ecd55a5.jpeg)
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 1028
初学网络流,找道水题练练手。值得注意的点是不能用邻接矩阵存边,不然会超时。
存边的方法,好像叫链式前向星??不太确定,反正说白就是头插邻接表。
然后如果一条边的编号为i,每次存两条边的话,它的逆边一定为 i^1 (i^0=i, i ^1一定是取到和它一对的那个数,很容易想明白。比如0和1是一对,2和3是一对,以此类推)。
上代码。
先上第一份用邻接矩阵存边的代码,很遗憾,超时
#include <iostream>
#include<stdio.h>
#include<queue>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
int capacity[110][110];
int road[110][110];
int cost[110][110];
int father[111];
struct ZZ
{
int x;
int y;
};
int comcost(int x1,int y1,int x2,int y2)
{
int sum=abs(x1-x2)+abs(y1-y2);
return sum;
}
int s=105;
int t=106;
vector<ZZ> man;
vector<ZZ> house;
bool spfa(int s,int t,int n)
{
int mark[120]={0};
int dist[120];
memset(father,-1,sizeof(father));
memset(dist,-1,sizeof(dist));
int np=s;
dist[np]=0;
queue<int> Q;
Q.push(np);
while(!Q.empty())
{
np=Q.front();
Q.pop();
mark[np]=0;
for(int i=0;i<n;i++)
{
if(capacity[np][i]>0&&(dist[i]>dist[np]+cost[np][i]||dist[i]==-1) )
{
//cout<<i<<' '<<capacity[np][i]<<endl;
dist[i]=dist[np]+cost[np][i];
father[i]=np;
if(mark[i]==0)
{
mark[i]=1;
Q.push(i);
}
}
}
}
if(dist[t]==-1) return false;
return true;
}
int MCMF(int s,int t,int n)
{
int sum=0;
int minn;
while(spfa(s,t,n))
{
minn=9999999;
int p=t;
while(p!=s)
{
int fa=father[p];
minn=min(minn,capacity[fa][p]);
sum+=cost[fa][p];
p=father[p];
}
p=t;
while(p!=s)
{
int fa=father[p];
capacity[fa][p]-=minn;
capacity[p][fa]+=minn;
p=father[p];
}
}
return sum;
}
int main()
{
int n,m;
while(1)
{
cin>>n>>m;
if(n==m&&n==0) break;
man.clear();
house.clear();
memset(capacity,0,sizeof(capacity));
ZZ tmmp;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
char tmp;
cin>>tmp;
if(tmp=='m')
{
tmmp.x=i;
tmmp.y=j;
man.push_back(tmmp);
}
if(tmp=='H')
{
tmmp.x=i;
tmmp.y=j;
house.push_back(tmmp);
}
}
int num=man.size();
s=2*num;
t=s+1;
for(int i=0;i<man.size();i++)
{
capacity[s][i]=1;
capacity[i+num][t]=1;
for(int j=0; j<house.size();j++)
{
cost[i][j+num]=comcost(man[i].x,man[i].y,house[j].x,house[j].y);
cost[j+num][i]=-cost[i][j+num];
capacity[i][j+num]=1;
}
}
//cout<<MCMF(s,t,2*num)<<endl;
int aa=MCMF(s,t,num*2+2);
cout<<aa;
}
return 0;
}
改进后的代码,AC
#include <iostream>
#include<stdio.h>
#include<queue>
#include<string>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
int father[11100];
int head[12000]={-1};
int edgenum=0;
struct EDGE
{
int from,to,capacity,cost,next;
}edge[40000];
void newedge(int from,int to,int cap,int cost) //没必要存from,因为可以用逆边求出来
{
EDGE TMP={0,to,cap,cost,head[from]}; //这个赋值就很风骚
head[from]=edgenum;
edge[edgenum++]=TMP;
TMP={0,from,0,-cost,head[to]};
head[to]=edgenum;
edge[edgenum++]=TMP;
}
struct ZZ
{
int x;
int y;
};
int comcost(int x1,int y1,int x2,int y2)
{
int sum=abs(x1-x2)+abs(y1-y2);
return sum;
}
int s=105;
int t=106;
vector<ZZ> man;
vector<ZZ> house;
bool spfa(int s,int t,int n)
{
int mark[12000]={0};
int dist[12000];
memset(father,-1,sizeof(father));
memset(dist,-1,sizeof(dist));
int np=s;
dist[np]=0;
queue<int> Q;
Q.push(np);
while(!Q.empty())
{
np=Q.front();
Q.pop();
mark[np]=0;
for(int i=head[np];i!=-1;i=edge[i].next)
{
if(edge[i].capacity>0&&(dist[edge[i].to]==-1||dist[edge[i].to]>dist[np]+edge[i].cost))
// if(capacity[np][i]>0&&(dist[i]>dist[np]+cost[np][i]||dist[i]==-1) )
{
//cout<<i<<' '<<capacity[np][i]<<endl;
//dist[i]=dist[np]+cost[np][i];
dist[edge[i].to]=dist[np]+edge[i].cost;
father[edge[i].to]=i; //重要,存的是边而不是节点,因为下面更新用的是边。
if(mark[edge[i].to]==0)
{
mark[edge[i].to]=1;
Q.push(edge[i].to);
}
}
}
}
if(dist[t]==-1) return false;
return true;
}
int MCMF(int s,int t,int n)
{
int sum=0;
int minn;
while(spfa(s,t,n))
{
minn=9999999;
int pre=t; //点
while(pre!=s)
{
int fa=edge[father[pre]^1].to;
minn=min(minn,edge[father[pre]].capacity);
sum+=edge[father[pre]].cost;
pre=fa;
}
pre=t;
while(pre!=s)
{
int fa=edge[father[pre]^1].to;
edge[father[pre]].capacity-=minn;
edge[father[pre]^1].capacity+=minn;
pre=fa;
}
}
return sum;
}
int main()
{
int n,m;
while(1)
{
cin>>n>>m;
if(n==m&&n==0) break;
man.clear();
house.clear();
memset(head,-1,sizeof(head));
edgenum=0;
ZZ tmmp;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
char tmp;
cin>>tmp;
if(tmp=='m')
{
tmmp.x=i;
tmmp.y=j;
man.push_back(tmmp);
}
if(tmp=='H')
{
tmmp.x=i;
tmmp.y=j;
house.push_back(tmmp);
}
}
int num=man.size();
s=2*num;
t=s+1;
for(int i=0;i<man.size();i++)
{
//capacity[s][i]=1;
//capacity[i+num][t]=1;
newedge(s,i,1,0);
newedge(i+num,t,1,0);
for(int j=0; j<house.size();j++)
{
//cost[i][j+num]=comcost(man[i].x,man[i].y,house[j].x,house[j].y);
//cost[j+num][i]=-cost[i][j+num];
//capacity[i][j+num]=1;
int costt=comcost(man[i].x,man[i].y,house[j].x,house[j].y);
newedge(i,j+num,1,costt);
}
}
cout<<MCMF(s,t,2*num+2)<<endl;
}
return 0;
}