码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2195 Going Home(费用流)

时间:2017-07-20 23:51:20      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:i++   als   long   pre   front   ges   ble   problem   最小   

http://poj.org/problem?id=2195

题意:

在一个网格地图上,有n个小人和n栋房子。在每个时间单位内,每个小人可以往水平方向或垂直方向上移动一步,走到相邻的方格中。对每个小人,每走一步需要支付1美元,直到他走入到一栋房子里。每栋房子只能容纳一个小人。

计算出让n个小人移动到n个不同的房子需要支付的最小费用。

 

思路:

源点和每个人相连,容量为1,费用为0。

汇点和每栋房子相连,容量为1,费用为0。

每个人和每栋房子相连,容量为1,费用为人和房子之间的距离。

这样一来,跑一遍费用流即可。

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<cstdio>
  5 #include<sstream>
  6 #include<vector>
  7 #include<stack>
  8 #include<queue>
  9 #include<cmath>
 10 #include<map>
 11 #include<set>
 12 using namespace std;
 13 typedef long long ll;
 14 typedef long long ull;
 15 typedef pair<int,int> pll;
 16 const int INF = 0x3f3f3f3f;
 17 const int maxn = 1000 + 5;
 18 
 19 int n, m, k;
 20 
 21 struct Edge
 22 {
 23     int from, to, cap, flow, cost;
 24     Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
 25 };
 26 
 27 struct MCMF
 28 {
 29     int n, m;
 30     vector<Edge> edges;
 31     vector<int> G[maxn];
 32     int inq[maxn];
 33     int d[maxn];
 34     int p[maxn];
 35     int a[maxn];
 36 
 37     void init(int n)
 38     {
 39         this->n = n;
 40         for (int i = 0; i<n; i++) G[i].clear();
 41         edges.clear();
 42     }
 43 
 44     void AddEdge(int from, int to, int cap, int cost)
 45     {
 46         edges.push_back(Edge(from, to, cap, 0, cost));
 47         edges.push_back(Edge(to, from, 0, 0, -cost));
 48         m = edges.size();
 49         G[from].push_back(m - 2);
 50         G[to].push_back(m - 1);
 51     }
 52 
 53     bool BellmanFord(int s, int t, int &flow, int & cost)
 54     {
 55         for (int i = 0; i<n; i++) d[i] = INF;
 56         memset(inq, 0, sizeof(inq));
 57         d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
 58 
 59         queue<int> Q;
 60         Q.push(s);
 61         while (!Q.empty()){
 62             int u = Q.front(); Q.pop();
 63             inq[u] = 0;
 64             for (int i = 0; i<G[u].size(); i++){
 65                 Edge& e = edges[G[u][i]];
 66                 if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
 67                     d[e.to] = d[u] + e.cost;
 68                     p[e.to] = G[u][i];
 69                     a[e.to] = min(a[u], e.cap - e.flow);
 70                     if (!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
 71                 }
 72             }
 73         }
 74 
 75         if (d[t] == INF) return false;
 76         flow += a[t];
 77         cost += d[t] * a[t];
 78         for (int u = t; u != s; u = edges[p[u]].from)
 79         {
 80             edges[p[u]].flow += a[t];
 81             edges[p[u] ^ 1].flow -= a[t];
 82         }
 83         return true;
 84     }
 85 
 86     int MincostMaxdflow(int s, int t){
 87         int flow = 0, cost = 0;
 88         while (BellmanFord(s, t, flow, cost));
 89         return cost;
 90     }
 91 }t;
 92 
 93 struct node
 94 {
 95     int x, y;
 96 }people[maxn],house[maxn];
 97 
 98 int main()
 99 {
100     //freopen("in.txt","r",stdin);
101     while(~scanf("%d%d",&n,&m) && n && m)
102     {
103         char c;
104         int cnt_p=0, cnt_h=0;
105         for(int i=0;i<n;i++)
106         {
107             for(int j=0;j<m;j++)
108             {
109                 cin>>c;
110                 if(c==H)       {house[++cnt_h].x=i;house[cnt_h].y=j;}
111                 else if(c==m)  {people[++cnt_p].x=i;people[cnt_p].y=j;}
112             }
113         }
114 
115         int n=cnt_h;
116         int src=0, dst=2*n+1;
117         t.init(dst+1);
118 
119         for(int i=1;i<=cnt_p;i++)  t.AddEdge(src,i,1,0);
120         for(int i=1;i<=cnt_h;i++)  t.AddEdge(n+i,dst,1,0);
121 
122         for(int i=1;i<=cnt_p;i++)
123         {
124             for(int j=1;j<=cnt_h;j++)
125             {
126                 int dis=abs(people[i].x-house[j].x)+abs(people[i].y-house[j].y);
127                 t.AddEdge(i,n+j,1,dis);
128             }
129         }
130 
131         printf("%d\n",t.MincostMaxdflow(src,dst));
132     }
133     return 0;
134 }

 

POJ 2195 Going Home(费用流)

标签:i++   als   long   pre   front   ges   ble   problem   最小   

原文地址:http://www.cnblogs.com/zyb993963526/p/7214561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!