博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ2195&&HDU1533(KB11-D 最小费用最大流)
阅读量:5052 次
发布时间:2019-06-12

本文共 4397 字,大约阅读时间需要 14 分钟。

Going Home

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23515   Accepted: 11853

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. 
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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0

Sample Output

21028

Source

 
所有的房子和超级源点连边,容量为1,费用为0。
所有的人和超级汇点连边,容量为1,费用为0。
所有的房子和所有的人相互连边,容量为1,费用为房子和人的曼哈顿距离。
 
1 //2017-08-24  2 #include 
3 #include
4 #include
5 #include
6 #include
7 #include
8 9 using namespace std; 10 11 const int N = 1000; 12 const int M = 100000; 13 const int INF = 0x3f3f3f3f; 14 int head[N], tot; 15 struct Edge{ 16 int to, next, c, w;//c为容量,w为单位费用 17 }edge[M]; 18 19 void add_edge(int u, int v, int c, int w){ 20 edge[tot].c = c; 21 edge[tot].w = w; 22 edge[tot].to = v; 23 edge[tot].next = head[u]; 24 head[u] = tot++; 25 26 edge[tot].c = 0; 27 edge[tot].w = -w; 28 edge[tot].to = u; 29 edge[tot].next = head[v]; 30 head[v] = tot++; 31 } 32 33 bool vis[N]; 34 int pre[N], dis[N];//pre记录路径,dis记录到源点的最小花费 35 struct MinCostMaxFlow{ 36 int S, T; 37 int flow, cost; 38 void init(int _S, int _T){ 39 S = _S; 40 T = _T; 41 tot = 0; 42 memset(head, -1, sizeof(head)); 43 } 44 bool spfa(){ 45 memset(vis, 0, sizeof(vis)); 46 memset(dis, INF, sizeof(dis)); 47 dis[S] = 0; 48 vis[S] = 1; 49 queue
que; 50 que.push(S); 51 while(!que.empty()){ 52 int u = que.front(); 53 que.pop(); 54 for(int i = head[u]; i != -1; i = edge[i].next){ 55 int v = edge[i].to; 56 if(edge[i].c > 0 && dis[v] > dis[u]+edge[i].w){ 57 dis[v] = dis[u] + edge[i].w; 58 pre[v] = i; 59 if(!vis[v]){ 60 vis[v] = true; 61 que.push(v); 62 } 63 } 64 } 65 vis[u] = 0; 66 } 67 return dis[T] != INF; 68 } 69 int dfs(int &flow){ 70 int u, p, sum = INF, ans = 0; 71 for(u = T; u != S; u = edge[p^1].to){ 72 //记录路径上的最小流值 73 p = pre[u]; 74 sum = min(sum, edge[p].c); 75 } 76 for(u = T; u != S; u = edge[p^1].to){ 77 p = pre[u]; 78 edge[p].c -= sum; 79 edge[p^1].c += sum; 80 ans += sum*edge[p].w; 81 } 82 flow += sum; 83 return ans; 84 } 85 int maxflow(){ 86 cost = 0, flow = 0; 87 while(spfa()){ //寻找增广路并增广 88 cost += dfs(flow); 89 } 90 return cost; 91 } 92 }mcmf; 93 94 string grid[N]; 95 int x[N], y[N]; 96 97 int main() 98 { 99 std::ios::sync_with_stdio(false);100 //freopen("inputD.txt", "r", stdin);101 int n, m;102 while(cin>>n>>m && (n || m)){103 int cnt_m = 0, cnt_h = 0;104 int s = N-2, t = N-3;105 mcmf.init(s, t);106 for(int i = 0; i < n; i++){107 cin>>grid[i];108 for(int j = 0; j < m; j++){109 if(grid[i][j] == 'H'){110 add_edge(s, cnt_h, 1, 0);111 x[cnt_h] = i;112 y[cnt_h++] = j;113 }114 }115 }116 for(int i = 0; i < n; i++){117 for(int j = 0; j < m; j++){118 if(grid[i][j] == 'm'){119 add_edge(cnt_h+cnt_m, t, 1, 0);120 for(int k = 0; k < cnt_h; k++){121 add_edge(k, cnt_h+cnt_m, 1, abs(i-x[k])+abs(j-y[k]));122 }123 cnt_m++;124 }125 }126 }127 cout<
<

 

转载于:https://www.cnblogs.com/Penn000/p/7425425.html

你可能感兴趣的文章
ulimit
查看>>
php代码执行顺序
查看>>
php 写入数据到MySQL以及从MySQL获取数据,页面出现乱码的解决方法
查看>>
MYSQL视图的学习笔记
查看>>
爬虫基础
查看>>
laravel常用artisan命令
查看>>
130292015038 张雅周 第一章作业
查看>>
获取文件字段并生产一个新的页面
查看>>
IIS 添加 MIME
查看>>
[转]协同管理系统
查看>>
安装了OFFICE2007,每次打开word时都显示配置microsoft office professional plus 解决方法...
查看>>
联合体和结构体的区别
查看>>
相同文件名引发的教训
查看>>
android调用系统相机并获取图片
查看>>
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
查看>>
Spark共享变量(广播变量、累加器)
查看>>
mongoose项目随笔
查看>>
JBOSS实现RMI时注意的问题
查看>>
ckeditor添加代码插入功能及高亮显示(插件)
查看>>
php 过滤html标签的函数
查看>>