POJ 2195 Going Home 最小费用最大流 尼玛,心累
2016-03-12 11:30
507 查看
D - Going Home
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2195
Appoint description:
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
Sample Output
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2195
Appoint description:
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 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0
Sample Output
2 10 28 尼玛心累啊,为什么从源点链接房子,在链接人在链接会点jiu不行,,,,,,必须按照源点---》ren---》房子,,,,会点的顺序建图 还有数组额外注意, 尼玛了,因为数组开小,wa了14个小时,,,,,
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<vector> #include<map> #include<algorithm> #include<queue> using namespace std; const int maxn=10005; const int maxm=20505; const int inf=0x3f3f3f3f; struct Edge{ int to; int next; int cap,flow,cost; }edge[maxm]; int head[maxn],tot; int pre[maxn],dis[maxn]; bool vis[maxn]; int n; char str[500]; struct node{ int x,y; }p[maxn],q[maxn]; void init(int nn){ n=nn; tot=0; memset(head,-1,sizeof(head)); } void addedge(int u,int v,int cap,int cost){ edge[tot].to=v; edge[tot].cap=cap; edge[tot].cost=cost; edge[tot].flow=0; edge[tot].next=head[u]; head[u]=tot++; edge[tot].to=u; edge[tot].cap=0; edge[tot].cost=-cost; edge[tot].flow=0; edge[tot].next=head[v]; head[v]=tot++; } bool spfa(int s,int t){ queue<int >q; // printf("n====%d\n",n); for(int i=0;i<=n;i++){ dis[i]=inf; vis[i]=false; pre[i]=-1; } dis[s]=0; vis[s]=true; q.push(s); while(!q.empty()){ int u=q.front(); q.pop(); vis[u]=false; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(edge[i].cap>edge[i].flow&&dis[v]>dis[u]+edge[i].cost){ dis[v]=dis[u]+edge[i].cost; pre[v]=i; if(!vis[v]){ vis[v]=true; q.push(v); } } } } if(pre[t]==-1) return false; else return true; } int mincost(int s,int t,int &cost){ int flow=0; cost=0; // printf("%d\n",head[2]); while(spfa(s,t)){ int Min=inf; for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){ if(Min>edge[i].cap-edge[i].flow) Min=edge[i].cap-edge[i].flow; } for(int i=pre[t];i!=-1;i=pre[edge[i^1].to]){ edge[i].flow+=Min; edge[i^1].flow-=Min; cost+=edge[i].cost*Min; } flow+=Min; } // printf("cost=====%d flow======%d\n",cost,flow); return flow; } int main(){ int x,y; while(scanf("%d%d",&x,&y)!=EOF){ if(x==0&&y==0) break; n=x*y+1; init(n); int start=0; int end=n; int pp=0,qq=0; for(int i=1;i<=x;i++){ scanf("%s",str+1); for(int j=1;j<=y;j++){ int cnt=(i-1)*y+j; if(str[j]=='m'){ addedge(start,cnt,1,0); p[pp].x=i; p[pp++].y=j; } if(str[j]=='H'){ addedge(cnt,end,1,0); q[qq].x=i; q[qq++].y=j; } } // printf("%s\n",str+1); } for(int i=0;i<pp;i++){ for(int j=0;j<qq;j++){ addedge((p[i].x-1)*y+p[i].y,(q[j].x-1)*y+q[j].y,1,abs(p[i].x-q[j].x)+abs(p[i].y-q[j].y)); } } int tmp; int ans=mincost(start,end,tmp); printf("%d\n",tmp); } return 0; }
5634445 | qwerqqq | D | Accepted | 1188 | 110 | G++ | 2650 | 5 min ago |
5634440 | qwerqqq | D | Wrong Answer | G++ | 2650 | 5 min ago | ||
5634430 | qwerqqq | D | Accepted | 1188 | 125 | G++ | 2650 | 6 min ago |
5634424 | qwerqqq | D | Accepted | 1188 | 110 | G++ | 2654 | 7 min ago |
5634358 | qwerqqq | D | Accepted | 1188 | 125 | G++ | 2654 | 12 min ago |
5634345 | qwerqqq | D | Accepted | 1192 | 110 | G++ | 2654 | 12 min ago |
5634339 | qwerqqq | D | Accepted | 1188 | 110 | G++ | 2655 | 12 min ago |
5634334 | qwerqqq | D | Accepted | 1228 | 110 | G++ | 2655 | 13 min ago |
5634307 | qwerqqq | D | Accepted | 1540 | 110 | G++ | 2656 | 16 min ago |
5634289 | qwerqqq | D | Wrong Answer | G++ | 2651 | 17 min ago | ||
5634263 | qwerqqq | D | Time Limit Exceeded | C++ | 2647 | 19 min ago | ||
5634250 | qwerqqq | D | Wrong Answer | G++ | 2647 | 21 min ago | ||
5634209 | qwerqqq | D | Wrong Answer | G++ | 3005 | 24 min ago | ||
5634202 | qwerqqq | D | Time Limit Exceeded | C++ | 3005 | 24 min ago | ||
5631907 | qwerqqq | D | Wrong Answer | G++ | 2662 | 14 hr ago |
相关文章推荐
- Goldengate 简单部署测试
- 关于 AlphaGo 论文的阅读笔记
- django url.py name字段
- Google翻译
- Django--登录实例
- 我与小娜(34):AlphaGo会打”小报告“吗?
- 阿尔法狗3:0李世石!在机器人研发上,我国却还在做三十年前的来料加工
- 编写GO的WEB开发框架 (十): Log和Access_Log
- 第五篇 使用category action data启动特定的Activity
- Django 相关知识
- goldengate ogg的拆分和合并
- sublime text2 配置成golang IDE
- AlphaGo>李世石?1:0
- CodeForces-630 D. Hexagons!【递推】
- GoAccess日志分析工具使用文档
- Sicily 1013 Going Home
- django no module splite3的问题
- mongo3.0基础篇
- [golang note] 接口使用
- 读《Introducing Go》O'Reilly , 第1,2章