Time Limit:1s Memory Limit:64MByte
Submissions:273Solved:77
There are n boys and m girls, and they live in different n+m cities.
The distance between city i and city j is L[i][j](if L[i][j]==−1, It means that there is no road between city i and city j ).
A girl and a boy can make up of a couple, and the value of this couple is the shortest distance between them by the city roads.
Boys live in city [1,n], and girls live in city [n+1,n+m].It ensures that it is an undirected graph.
Your mission is to arrange some of them to be some couples so that the sum of all couple's value is biggest.
Note: you just need the max couple’s value of couples, and it maybe not the max number of couples.
明显裸的二分图最大权匹配
没学过KM,只好跑最小费用流
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
const int inf=1e9+7;
const int INF=inf;
const int N = 200+5;//点
const int M = N*N*10;//边
struct Edge{
int to,c,w,next;//c:流量,w:费用
}edge[M];
int head[N];
int nume=0;
inline void addEdge(int k,int u,int v,int c,int w){
edge[k].to=v,
edge[k].c=c,
edge[k].w=w,
edge[k].next=head[u];
head[u]=k;
}
inline void addEdge(int u,int v,int c,int w){
addEdge(nume++,u,v,c,w);
addEdge(nume++,v,u,0,-w);
}
void init(int n){
fill(head,head+n+1,-1);
nume=0;
}
bool used[N];
int dis[N],load[N],p[N];//距离 ,前驱边,前驱点
bool spfa(int s,int e,int n){
deque<int>que;
for(int i=0;i<=n;++i){
dis[i]=INF;
load[i]=p[i]=-1;
used[i]=false;
}
que.push_back(s);
dis[s]=0;
used[s]=true;
while(!que.empty()){
int u=que.front();
que.pop_front();
used[u]=false;
for(int i=head[u];i!=-1;i=edge[i].next){
if(edge[i].c>0){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].w){
dis[v]=dis[u]+edge[i].w;
p[v]=u;
load[v]=i;
if(used[v]==false){
used[v]=true;
que.push_back(v);
}
}
}
}
}
return dis[e]!=INF;
}
int min_cost_flow(int s,int t,int n){
int ansflow=0,anscost=0;//最大流,最小费用
while(spfa(s,t,n)){
int u=t;
int f=INF;
while(p[u]!=-1){
f=min(f,edge[load[u]].c);
u=p[u];
}
u=t;
while(p[u]!=-1){
edge[load[u]].c-=f;
edge[load[u]^1].c+=f;
u=p[u];
}
anscost+=dis[t]*f;
ansflow+=f;
}
return anscost;
}
int disCity[N][N];
void floyd(int d[][N],int n){
for(int k=1;k<=n;++k){
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
d[i][j]=min(d[i][k]+d[k][j],d[i][j]);
}
}
}
}
void slove(int n,int m){
fill(head,head+n+m+3,-1);
const int S=n+m+1,T=S+1;
for(int i=1;i<=n;++i){
addEdge(S,i,1,0);
}
for(int i=1;i<=n;++i){
for(int j=n+1;j<=n+m;++j){
if(disCity[i][j]!=inf){
addEdge(i,j,1,-disCity[i][j]);
}
addEdge(i,j,1,0);
}
}
for(int j=n+1;j<=n+m;++j){
addEdge(j,T,1,0);
}
printf("%d\n",-min_cost_flow(S,T,n+m+2));
}
int main(int argc, char *argv[])
{
//freopen("/home/lu/Documents/r.txt","r",stdin);
int n,m,T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n+m;++i){
for(int j=1;j<=n+m;++j){
scanf("%d",&disCity[i][j]);
if(disCity[i][j]==-1){
disCity[i][j]=inf;
}
}
}
floyd(disCity,n+m);
slove(n,m);
}
return 0;
}