POJ 2195 Going Home 最小费用最大流 尼玛,心累
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
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
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
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=;
const int maxm=;
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[];
struct node{
int x,y; }p[maxn],q[maxn]; void init(int nn){
n=nn;
tot=;
memset(head,-,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=;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].to=u;
edge[tot].cap=;
edge[tot].cost=-cost;
edge[tot].flow=;
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=;i<=n;i++){
dis[i]=inf;
vis[i]=false;
pre[i]=-;
}
dis[s]=;
vis[s]=true;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u];i!=-;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]==-)
return false;
else
return true; }
int mincost(int s,int t,int &cost){
int flow=;
cost=;
// printf("%d\n",head[2]);
while(spfa(s,t)){
int Min=inf;
for(int i=pre[t];i!=-;i=pre[edge[i^].to]){
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
}
for(int i=pre[t];i!=-;i=pre[edge[i^].to]){
edge[i].flow+=Min;
edge[i^].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==&&y==)
break;
n=x*y+;
init(n);
int start=;
int end=n;
int pp=,qq=;
for(int i=;i<=x;i++){
scanf("%s",str+);
for(int j=;j<=y;j++){
int cnt=(i-)*y+j;
if(str[j]=='m'){
addedge(start,cnt,,);
p[pp].x=i;
p[pp++].y=j;
}
if(str[j]=='H'){
addedge(cnt,end,,);
q[qq].x=i;
q[qq++].y=j;
}
}
// printf("%s\n",str+1);
} for(int i=;i<pp;i++){
for(int j=;j<qq;j++){
addedge((p[i].x-)*y+p[i].y,(q[j].x-)*y+q[j].y,,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 ;
}
5634445 | qwerqqq![]() |
D![]() |
Accepted
|
1188 | 110 | 2650 |
5 min ago
|
|
5634440 | qwerqqq![]() |
D![]() |
Wrong Answer
|
2650 |
5 min ago
|
|||
5634430 | qwerqqq![]() |
D![]() |
Accepted
|
1188 | 125 | 2650 |
6 min ago
|
|
5634424 | qwerqqq![]() |
D![]() |
Accepted
|
1188 | 110 | 2654 |
7 min ago
|
|
5634358 | qwerqqq![]() |
D![]() |
Accepted
|
1188 | 125 | 2654 |
12 min ago
|
|
5634345 | qwerqqq![]() |
D![]() |
Accepted
|
1192 | 110 | 2654 |
12 min ago
|
|
5634339 | qwerqqq![]() |
D![]() |
Accepted
|
1188 | 110 | 2655 |
12 min ago
|
|
5634334 | qwerqqq![]() |
D![]() |
Accepted
|
1228 | 110 | 2655 |
13 min ago
|
|
5634307 | qwerqqq![]() |
D![]() |
Accepted
|
1540 | 110 | 2656 |
16 min ago
|
|
5634289 | qwerqqq![]() |
D![]() |
Wrong Answer
|
2651 |
17 min ago
|
|||
5634263 | qwerqqq![]() |
D![]() |
Time Limit Exceeded
|
2647 |
19 min ago
|
|||
5634250 | qwerqqq![]() |
D![]() |
Wrong Answer
|
2647 |
21 min ago
|
|||
5634209 | qwerqqq![]() |
D![]() |
Wrong Answer
|
3005 |
24 min ago
|
|||
5634202 | qwerqqq![]() |
D![]() |
Time Limit Exceeded
|
3005 |
24 min ago
|
|||
5631907 | qwerqqq![]() |
D![]() |
Wrong Answer
|
2662 |
14 hr ago
|
POJ 2195 Going Home 最小费用最大流 尼玛,心累的更多相关文章
- POJ 2195 - Going Home - [最小费用最大流][MCMF模板]
题目链接:http://poj.org/problem?id=2195 Time Limit: 1000MS Memory Limit: 65536K Description On a grid ma ...
- poj 2195 Going Home(最小费用最大流)
题目:http://poj.org/problem?id=2195 有若干个人和若干个房子在一个给定网格中,每人走一个都要一定花费,每个房子只能容纳一人,现要求让所有人进入房子,且总花费最小. 构造一 ...
- poj 2351 Farm Tour (最小费用最大流)
Farm Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17230 Accepted: 6647 Descri ...
- POJ 2157 Evacuation Plan [最小费用最大流][消圈算法]
---恢复内容开始--- 题意略. 这题在poj直接求最小费用会超时,但是题意也没说要求最优解. 根据线圈定理,如果一个跑完最费用流的残余网络中存在负权环,那么顺着这个负权环跑流量为1那么会得到更小的 ...
- poj 2135 Farm Tour 最小费用最大流建图跑最短路
题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...
- POJ 3680: Intervals【最小费用最大流】
题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
- POJ 2135 Farm Tour [最小费用最大流]
题意: 有n个点和m条边,让你从1出发到n再从n回到1,不要求所有点都要经过,但是每条边只能走一次.边是无向边. 问最短的行走距离多少. 一开始看这题还没搞费用流,后来搞了搞再回来看,想了想建图不是很 ...
- [poj] 1235 Farm Tour || 最小费用最大流
原题 费用流板子题. 费用流与最大流的区别就是把bfs改为spfa,dfs时把按deep搜索改成按最短路搜索即可 #include<cstdio> #include<queue> ...
- POJ 2516 Minimum Cost [最小费用最大流]
题意略: 思路: 这题比较坑的地方是把每种货物单独建图分开算就ok了. #include<stdio.h> #include<queue> #define MAXN 500 # ...
随机推荐
- Java多线程系列
一.参考文献 1.:Java多线程系列目录 (一) 基础篇 01. Java多线程系列--“基础篇”01之 基本概念 02. Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 03. ...
- 如何在命令行模式下查看Python帮助文档---dir、help、__doc__
如何在命令行模式下查看Python帮助文档---dir.help.__doc__ 1.dir函数式可以查看对象的属性,使用方法很简单,举str类型为例,在Python命令窗口输入 dir(str) 即 ...
- HashSet与HashMap的区别
本文由 ImportNew - 唐小娟 翻译自 Javarevisited.欢迎加入翻译小组.转载请见文末要求. HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到C ...
- JavaWeb学习总结(一)——JavaWeb开发入门
http://www.cnblogs.com/xdp-gacl/p/3729033.html 只为成功找方法,不为失败找借口! JavaWeb学习总结(一)--JavaWeb开发入门 一.基本概念 1 ...
- css3d总结
3d舞台 设置 perspective(景深): length, 可以设置overflow:hidden 3d舞台下 -> 3d元素容器 设置 transform-style: preserv ...
- python字符类型的一些方法
python 字符串和字节互转换.bytes(s, encoding = "utf8") str(b, encoding = "utf-8") i.isspac ...
- Hello World(本博客启程篇)
Hello World 作为本博客第一篇日志,作为程序员,无论走到哪里,做什么事,必须先输出这句话. 一个想法 从今天3月份到现在一直在学技术,过程中坑的解决.知识的总结以及想法等都写到了" ...
- Coding上传项目步骤
step1:在coding上面创建一个项目mybokestep2:在git 命令台中进入项目的根目录下面,使用git init创建.git文件夹和.gitigonre文件,帮组本地与远程的链接step ...
- jsf简介
JSF实现了基于web的以下三个梦想 1.java程序员不必顾虑HTTP的细节,可以按照原本熟悉的事件驱动模型来设计后台系统,并通过一个能担保数据类型无误的数据传递接口将后台系统与前台界面结合在一起. ...
- Python 爬虫笔记、多线程、xml解析、基础笔记(不定时更新)
1 Python学习网址:http://www.runoob.com/python/python-multithreading.html