POJ 2195 Going Home(费用流)

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 }
时间: 2024-08-04 19:16:55

POJ 2195 Going Home(费用流)的相关文章

poj - 2195 Going Home (费用流 || 最佳匹配)

http://poj.org/problem?id=2195 第一个最佳匹配的题.可耻的模板都不会套. 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 2000 + 10; 8 9 const int INF = 0x7fffffff; 10 11 int

POJ 3680 Intervals 离散 + 费用流

Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6246   Accepted: 2542 Description You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize t

POJ 2175 Evacuation Plan 费用流 负圈定理

题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在负环上. 2.这个负环可能包括汇点t,所以构建残量网络的时候也要考虑防空洞到t上的容量. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring

POJ 3670 Intervals(费用流)

POJ 3680 Intervals 题目链接 题意:给定一些区间,每个区间有一个权值,要求用这些区间去覆盖,每个点最多覆盖k次,问最多得到权值多少 思路:典型的区间k覆盖问题,区间连边容量1,代价-w,然后其他点相邻两两连边,容量k,代价0,跑一下费用流即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm&g

POJ 2135 Farm Tour(费用流)

POJ 2135 Farm Tour 题目链接 题意:给定一个无向图,边有权值,求从1到n再从n到1的最短路 思路:费用流,连边容量为1(注意是无向图),然后源点和1连容量2,n和汇点连容量是2 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int

poj 3680 Intervals 最大费用流

题意: 给n给开区间(ai,bi)及相应权值wi,现在要选一些区间,要求任一点不能被超过k个区间覆盖,目标是最大化总的权重. 分析: 转化为求最大费用流,改改最小费用流的模板就好. 代码: //poj 3680 //sep9 #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxN=2048;

POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3256   Accepted: 855   Special Judge Description The City has a number of municipal buildings and a number of fallout shelters that were build

POJ 2516 Minimum Cost(费用流)

POJ 2516 Minimum Cost 题目链接 题意:转一篇题意吧..感觉写的很详细了,優YoU http://blog.csdn.net/lyy289065406/article/details/6742534 思路:一开始是把所有商家的每种物品和所有供应商所有物品连边跑费用流,结果TLE了,因为这样建出来的图,边数会非常的庞大 那么其实转化一下思路,每种物品之间是不会互相影响的,那么把每种物品求出来后,累加起来就是答案了,然后注意这题数据要读完,直接判断错没读完数据就会WA 代码: #

POJ 2175 Evacuation Plan 费用流消圈

题目大意:给出一个费用流的模型和已经流过的一些边,问是否存在比这个解更优的解. 思路:直接用原图做一次费用流求最优解会T掉.先介绍费用流消圈定理:如果当前费用流的残量网络中存在负圈,那么当前流不是最优的解. 其实很好理解,结合原图和流过流量之后的反边,若出现了负圈,那么就可以沿着这个负圈增广,而且费用更小. 不过为了解决这个题我们并不需要建立完整的网络流,只需要建立残量网络之后SPFA看是否能找到负环即可. 具体建立方法: 如果一个避难地点有值,那么T向这个避难地点连边,费用0 若当前避难地点没

Going Home POJ - 2195 (最小费用最大流)

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, unt