博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2195 Going Home 最小费用流 难度:1
阅读量:5127 次
发布时间:2019-06-13

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

Going Home
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17955   Accepted: 9145

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 扫描路径得到人和房子的坐标,相减得所费路程,然后建边最小费用流即可
1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 const int inf=0x7fffffff; 8 int n,m; 9 10 char maz[101][101]; 11 int man[101][2]; 12 int house[101][2]; 13 int hlen,mlen; 14 15 16 const int sups=201; 17 const int supt=202; 18 19 int cost[203][203]; 20 int f[203][203]; 21 int e[203][203]; 22 int len[203]; 23 24 int d[203]; 25 int pre[203]; 26 bool vis[203]; 27 28 queue
que; 29 30 int main(){ 31 while(scanf("%d%d",&n,&m)==2&&n&&m){ 32 input: 33 hlen=mlen=0; 34 gets(maz[0]); 35 for(int i=0;i
0){ 75 fill(d,d+203,inf); 76 d[sups]=0; 77 que.push(sups); 78 while(!que.empty()){ 79 int fr=que.front();que.pop(); 80 vis[fr]=false; 81 for(int i=0;i
0&&d[to]>d[fr]+cost[fr][to]){ 84 d[to]=d[fr]+cost[fr][to]; 85 pre[to]=fr; 86 if(!vis[to]){ 87 que.push(to); 88 vis[to]=true; 89 } 90 } 91 } 92 } 93 94 assert(d[supt]!=inf); 95 96 int sub=flow; 97 for(int i=supt;i!=sups;i=pre[i]){ 98 sub=min(flow,f[pre[i]][i]); 99 }100 flow-=sub;101 ans+=sub*d[supt];102 for(int i=supt;i!=sups;i=pre[i]){103 f[pre[i]][i]-=sub;104 f[i][pre[i]]+=sub;105 }106 107 }108 printf("%d\n",ans);109 }110 return 0;111 }

 

转载于:https://www.cnblogs.com/xuesu/p/3924027.html

你可能感兴趣的文章
BZOJ3926: [Zjoi2015]诸神眷顾的幻想乡(广义后缀自动机)
查看>>
mysql 中 時間和日期函數大全
查看>>
mongodb基本语法
查看>>
[凯立德]2014全分辨率C-Car 4.0机车C2610版完美懒人包
查看>>
[LeetCode] Same Tree
查看>>
给Entity Framework添加执行的超时时间
查看>>
【总结】瞬时高并发(秒杀/活动)Redis方案(转)
查看>>
numpy模块
查看>>
iPhone图形开发绘图小结
查看>>
从零开始搭建微信硬件开发环境全过程——1小时掌握微信硬件开发流程
查看>>
Android应用程序线程消息循环模型分析
查看>>
Swift - 下标脚本方法介绍及实例
查看>>
解决方案设置centos上redmine2.3.0点击我的帐户和个人设置出现500错误的解决方案...
查看>>
最小较小codeforces 2B The least round way
查看>>
对象编译器ARC内部工作原理
查看>>
Java URLClassLoader动态加载外部java代码
查看>>
做一名合格的软件使用者
查看>>
设计方案考量的准则与细则
查看>>
Go语言【第四篇】:Go运算符
查看>>
学习进度条——第十七周
查看>>