poj - 2195 Going Home (费用流 || 最佳匹配)
2015-06-29 17:04
537 查看
http://poj.org/problem?id=2195
对km算法不理解,模板用的也不好。
下面是大神的解释。
当然还可以用费用流来做.
[align=left]算出每一个到每一个房子的距离后建图。[/align]
[align=left]源点与人连,容量1,费用0[/align]
[align=left]人与每个房子都要连,容量1,费用为距离[/align]
[align=left]每个房子与汇点连,容量1,费用0[/align]
[align=left]求一次最小费用即可[/align]
[align=left]另外此题数据的范围开大点。[/align]
[align=left]Bellmanford:[/align]
SPFA:
参考:http://blog.csdn.net/lenleaves/article/details/7904588
对km算法不理解,模板用的也不好。
下面是大神的解释。
KM算法的要点是在相等子图中寻找完备匹配,其正确性的基石是:任何一个匹配的权值之和都不大于所有顶点的顶标之和,而能够取到相等的必然是最大权匹配。
左右两边点数不等时,KM算法的正确性也是可以得到保证的。原因如下: 由KM算法中可行点标的定义,有: 任意匹配的权值 <= 该匹配所覆盖的所有点的顶标值 <= KM算法所得到的匹配所覆盖的所有点的顶标值 = KM算法所得到的的匹配的权值 上面的证明与网上大多数证明不同点在于,网上的证明中只是模糊地说“图中所有点的顶标值”,这只在两点集点数相等时才正确。 上面第二个不等号最为关键,它的理由是:假定|X|<=|Y|,则任意时刻,Y集合中KM算法所取的匹配未覆盖的点的顶标必为0!
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxn = 2000 + 10; const int INF = 0x7fffffff; int n,m; int W[maxn][maxn]; //存储权值 int Lx[maxn], Ly[maxn]; // 顶标 int left[maxn]; // left[i]为右边第i个点的匹配点编号 bool S[maxn], T[maxn]; // S[i]和T[i]为左/右第i个点是否已标记 bool match(int i){ S[i] = true; for(int j = 1; j <= m; j++) if (Lx[i]+Ly[j]==W[i][j] && !T[j]) { T[j] = true; if (!left[j] || match(left[j])) { left[j] = i; return true; } } return false; } void update(){ int a = INF; for(int i = 1; i <= n; i++) if(S[i]) for(int j = 1; j <= m; j++) if(!T[j]) a = min(a, W[i][j]-Lx[i]-Ly[j]); //若是最大权匹配则Lx[i]+Ly[j]-W[i][j]; 同样是取最小值 for(int i = 1; i <= n; i++) { if(S[i]) Lx[i] -= a; if(T[i]) Ly[i] += a; } } void KM() { for(int i = 1; i <= n; i++) { left[i] = Lx[i] = Ly[i] = 0; for(int j = 1; j <= m; j++) Lx[i] = min(Lx[i], W[i][j]); //若是最大权匹配.则初始值顶标取最大值,最小匹配则去最小值 } for(int i = 1; i <= n; i++) { for(;;) { for(int j = 1; j <= m; j++) S[j] = T[j] = 0; if(match(i)) break; else update(); } } } char s[305]; struct point { int x,y; }; point man[305],house[305]; int main(){ //freopen("a.txt","r",stdin); int sum,man_num,house_num; while(scanf("%d%d",&n,&m)!=EOF&&(n+m)) { int i,j; man_num=1,house_num=1; for(int i=1;i<=n;i++) { scanf("%s",s+1); for(int j=1;j<=m;j++) { if(s[j]=='m') { man[man_num].x=i; man[man_num++].y=j; } else if(s[j]=='H') { house[house_num].x=i; house[house_num++].y=j; } } } man_num--,house_num--; // printf("%d %d\n",man_num,house_num); n=man_num;m=house_num; for(int i=1;i<=man_num;i++) for(int j=1;j<=house_num;j++) W[i][j]=abs(man[i].x-house[j].x)+abs(man[i].y-house[j].y); sum=0; KM(); // 最大权匹配 for(i=1;i<=m;i++) sum+=W[left[i]][i]; printf("%d\n",sum); } //for(int i = 1; i <= n; i++) printf("left[%d]=%d\n", i,left[i]); //getch(); return 0; }
当然还可以用费用流来做.
[align=left]算出每一个到每一个房子的距离后建图。[/align]
[align=left]源点与人连,容量1,费用0[/align]
[align=left]人与每个房子都要连,容量1,费用为距离[/align]
[align=left]每个房子与汇点连,容量1,费用0[/align]
[align=left]求一次最小费用即可[/align]
[align=left]另外此题数据的范围开大点。[/align]
[align=left]Bellmanford:[/align]
#include<cstdio> #include<cstring> #include<queue> #include<vector> #include<algorithm> #include<cassert> using namespace std; const int maxn = 2000 + 20; const int INF = 1000000000; struct Edge { int from, to, cap, flow, cost; Edge(int u, int v, int c, int f, int w):from(u),to(v),cap(c),flow(f),cost(w) {} }; struct MCMF { int n, m; vector<Edge> edges; vector<int> G[maxn]; int inq[maxn]; // 是否在队列中 int d[maxn]; // Bellman-Ford int p[maxn]; // 上一条弧 int a[maxn]; // 可改进量 void init(int n) { this->n = n; for(int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from, int to, int cap, int cost) { edges.push_back(Edge(from, to, cap, 0, cost)); edges.push_back(Edge(to, from, 0, 0, -cost)); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BellmanFord(int s, int t, int flow_limit, int& flow, int& cost) { for(int i = 0; i < n; i++) d[i] = INF; memset(inq, 0, sizeof(inq)); d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; queue<int> Q; Q.push(s); while(!Q.empty()) { int u = Q.front(); Q.pop(); inq[u] = 0; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(e.cap > e.flow && d[e.to] > d[u] + e.cost) { d[e.to] = d[u] + e.cost; p[e.to] = G[u][i]; a[e.to] = min(a[u], e.cap - e.flow); if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; } } } } if(d[t] == INF) return false; if(flow + a[t] > flow_limit) a[t] = flow_limit - flow; flow += a[t]; cost += d[t] * a[t]; for(int u = t; u != s; u = edges[p[u]].from) { edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; } return true; } // 需要保证初始网络中没有负权圈 int MincostFlow(int s, int t, int flow_limit, int& cost) { int flow = 0; cost = 0; while(flow < flow_limit && BellmanFord(s, t, flow_limit, flow, cost)); return flow; } }; MCMF g; typedef struct{ int x,y; }Point; Point man[maxn],home[maxn]; int man_sum,home_sum; int main() { int n,m,i,j; char s[maxn]; while(~scanf("%d%d",&n,&m) && n || m) { getchar(); man_sum=home_sum=1; for(i=1;i<=n;i++) { //记录下人与房子的坐标 gets(s); for(j=0;j<m;j++) { if(s[j]=='m') { man[man_sum].x=i; man[man_sum++].y=j+1; } if(s[j]=='H') { home[home_sum].x=i; home[home_sum++].y=j+1; } } } man_sum--; home_sum--; g.init(man_sum+home_sum+2); for(i=1;i<=man_sum;i++) g.AddEdge(0, i, 1, 0); //源点指向人 for(i=1;i<=man_sum;i++) //人指向房子 { for(j=man_sum+1;j<=man_sum+home_sum;j++) { g.AddEdge(i, j, 1, abs(man[i].x-home[j-man_sum].x)+abs(man[i].y-home[j-man_sum].y)); } } for(i=man_sum+1;i<=man_sum+home_sum;i++) g.AddEdge(i, man_sum+home_sum+1, 1, 0); //房子指向汇点 int cost; g.MincostFlow(0, man_sum+home_sum+1, man_sum, cost); printf("%d\n", cost); } return 0; }
SPFA:
参考:http://blog.csdn.net/lenleaves/article/details/7904588
#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<cmath> #include<cstdio> using namespace std; #define MAXN 2000 #define MAXM 110000 #define INF 0x3FFFFFF struct edge { int to,c,w,next; }; edge e[MAXM]; bool in[MAXN]; int hx[500],hy[500],mx[500],my[500]; int head[MAXN],dis[MAXN],pre[MAXN],en,maxflow,mincost; int vn,st,ed,r,c,hn,mn; void add(int a,int b,int c,int d) { e[en].to=b; e[en].c=c; e[en].w=d; e[en].next=head[a]; head[a]=en++; e[en].to=a; e[en].c=0; e[en].w=-d; e[en].next=head[b]; head[b]=en++; } bool spfa(int s) { queue<int> q; for(int i=0;i<=vn;i++) { dis[i]=INF; in[i]=false; pre[i]=-1; } dis[s]=0; in[s]=true; q.push(s); while(!q.empty()) { int tag=q.front(); in[tag]=false; q.pop(); for(int i=head[tag];i!=-1;i=e[i].next) { int j=e[i].to; if(e[i].w+dis[tag]<dis[j] && e[i].c) { dis[j]=e[i].w+dis[tag]; pre[j]=i; if(!in[j]) { q.push(j); in[j]=true; } } } } if(dis[ed]==INF) return false; return true; } void update() { int flow=INF; for (int i=pre[ed];i!=-1;i=pre[e[i^1].to]) if(e[i].c<flow) flow=e[i].c; for (int i=pre[ed];i!=-1;i=pre[e[i^1].to]) { e[i].c-=flow,e[i^1].c+=flow; } maxflow+=flow; mincost+=flow*dis[ed]; } void mincostmaxflow() { maxflow=0,mincost=0; while(spfa(st)) update(); } void solve() { char s[500]; hn=0,mn=0,en=0; memset(head,-1,sizeof(head)); for(int i=1;i<=r;i++) { scanf("%s",s); for(int j=0;j<c;j++) { if(s[j]=='m') mx[mn]=i,my[mn++]=j+1; if(s[j]=='H') hx[hn]=i,hy[hn++]=j+1; } } vn=mn+hn+5; st=0,ed=mn+hn+1; for(int i=0;i<mn;i++) for(int j=0;j<hn;j++) { int dist=abs(mx[i]-hx[j])+abs(my[i]-hy[j]); add(i+1,j+1+mn,1,dist); } for(int i=0;i<mn;i++) add(st,i+1,1,0); for(int i=0;i<hn;i++) add(i+1+mn,ed,1,0); mincostmaxflow(); printf("%d\n",mincost); } int main() { while(scanf("%d%d",&r,&c)!=EOF && r+c) solve(); return 0; }
相关文章推荐
- Harry Potter and the Goblet of Fire
- 安装django
- Algorithm --> 字符串中最长不重合子串长度
- Algorithms—154.Find Minimum in Rotated Sorted Array II
- go语法之一
- Algorithms—153.Find Minimum in Rotated Sorted Array
- beego框架之日志模块
- 利用Go语言初步搭建一个web应用的教程
- Algorithm --> DFS和BFS
- Algorithms—151.Reverse Words in a String
- Django
- 如何解决Django 1.8在migrate时失败?
- 在Go程序中实现服务器重启的方法
- Algorithm --> 判读是否是子树
- 最新Google hosts文件地址连接
- 让我们一起Go(十二)
- 让我们一起Go(十三)
- 让我们一起Go(十一)
- 让我们一起Go(十)
- 让我们一起Go(九)