zoj 2404 最小费用流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2404
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector> #define maxn 550
#define maxe 100000
#define INF 0x3f3f3f
using namespace std; struct Edge{
int from,to,cap,flow,cost;
int next;
void assign(int a,int b,int c,int d,int e,int f){
from = a; to = b; cap = c; flow = d;
cost = e; next = f;
}
}; struct MCMF{
int n,cnt;
int head[maxn];
int d[maxn];
Edge edges[maxe];
int inq[maxn];
int p[maxn];
int res[maxn]; void init(int n){
this->n = n;
cnt = ;
memset(head,-,sizeof(head));
} void addedge(int a,int b,int c,int d,int e){
edges[cnt].assign(a,b,c,d,e,head[a]);
head[a] = cnt++;
edges[cnt].assign(b,a,,,-e,head[b]);
head[b] = cnt++;
}
bool SPFA(int s,int t,int &flow,int& cost){
memset(d,0x3f,sizeof(d));
memset(inq,,sizeof(inq));
d[s] = ; inq[s] = ; p[s] = s; res[s] = INF; res[t] = ; queue<int> Q;
Q.push(s);
while(!Q.empty()){
int u = Q.front(); Q.pop();
inq[u] = ; for(int i=head[u];i!=-;i=edges[i].next){
Edge& e = edges[i];
if(e.cap > e.flow && d[e.to] > d[e.from] + e.cost){
d[e.to] = d[e.from] + e.cost;
p[e.to] = i;
res[e.to] = min(res[u],e.cap - e.flow);
if(!inq[e.to]){
Q.push(e.to); inq[e.to] = ;
}
}
}
}
if(res[t] == ) return false;
flow += res[t];
cost += d[t]*res[t];
for(int i=t;i!=s;i=edges[p[i]].from){
edges[p[i]].flow += res[t];
edges[p[i]^].flow -= res[t];
}
return true;
}
}solver;
int Mincost(int s,int t){
int flow = , cost = ;
while(solver.SPFA(s,t,flow,cost)){}
return cost;
} int main()
{
//freopen("input.txt","r",stdin);
int N,M; while(cin>>N>>M && N+M){
int ltail = ;
int rtail = ;
struct node{
int x, y;
}l[maxn],r[maxn]; char ch[];
for(int i=;i<=N;i++){
scanf("%s",ch);
for(int j=;j<M;j++){
if(ch[j] == 'H'){
r[rtail].x = i; r[rtail++].y = j;
}
else if(ch[j] == 'm'){
l[ltail].x = i; l[ltail++].y = j;
}
}
}
int n = ltail + rtail;
solver.init(n);
int s = , t = n+;
for(int i=;i<ltail;i++) solver.addedge(s,i+,,,);
for(int i=;i<rtail;i++) solver.addedge(i++ltail,t,,,);
for(int i=;i<ltail;i++)
for(int j=;j<rtail;j++){
int cost = (int)abs(1.0*l[i].x-1.0*r[j].x) + (int)abs(1.0*l[i].y-1.0*r[j].y);
solver.addedge(i+,ltail++j,,,cost);
}
printf("%d\n",Mincost(s,t));
}
}
zoj 2404 最小费用流的更多相关文章
- ZOJ 2404 Going Home 【最小费用最大流】
思路: 把房子和人看成点,加上源点和汇点. 源点和每个人连容量为1,权值为0的边. 每个人和每个房子连容量为1,权值为距离的边. 每个房子和汇点连容量为1,权值为0的边. #include<st ...
- 通过ipv6访问 g o o g l e
Google.Youtube.Facebook等均支持IPv6访问,IPv4网络的用户大部分都无法访问,比如Gmail,Google Docs等等各种相关服务.而该类网站大部分均已接入IPv6网络,因 ...
- ZOJ题目分类
ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- ZOJ 刷题记录 (。・ω・)ノ゙(Progress:31/50)
[热烈庆祝ZOJ回归] P1002:简单的DFS #include <cstdio> #include <cstring> #include <algorithm> ...
- ZOJ 3362 Beer Problem(SPFA费用流应用)
Beer Problem Time Limit: 2 Seconds Memory Limit: 32768 KB Everyone knows that World Finals of A ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
随机推荐
- copssh加bitvise
只是简单记录下自己在成功使用的方案: 目的:为了突破公司对网站和qq的限制 具备的条件:一台云服务器.Copssh_4.1.0.bitvise ssh client 4.62.公司电脑客户端 一.首先 ...
- Wireshark提示没有一个可以抓包的接口
这是由于win下默认NPF服务是关闭的,需要以管理员的身份开启这个服务 Windows上安装wireshark时,会遇到NPF驱动无法启动的情况,一般如果采用管理员的方式就可以正常启动,或者是将NPF ...
- anjularjs slider控件替代方案
做项目需要一个slider控件,找了很久没有找到合适的指令集,无意间看到可以直接用range替代,不过样式有点丑. <label> <input type="range&q ...
- 进程识别号(PID)的理解
PID(Process Identification)操作系统里指进程识别号,也就是进程标识符.操作系统里每打开一个程序都会创建一个进程ID,即PID. PID(进程控制符)英文全称为Process ...
- Zend Server安装后首次运行就出现Internal Server Error的解决
无论是使用哪个版本的Zend Server来搭建PHP服务器,首次运行都会出现Internal Server Error的错误,对很多新手而言,每当看到这种错误时,那一刻内心绝对都是崩溃的.然而,这个 ...
- NSCondition的用法,NSCondication实现线程同步,生产者消费问题实现(转载)
NSCondition的用法 使用NSCondition,实现多线程的同步,即,可实现生产者消费者问题. 基本思路是,首先要创建公用的NSCondition实例.然后: 消费者取得锁,取产品,如果没有 ...
- 再看IOC, 读深入理解DIP、IoC、DI以及IoC容器
IoC则是一种 软件设计模式,它告诉你应该如何做,来解除相互依赖模块的耦合.控制反转(IoC),它为相互依赖的组件提供抽象,将依赖(低层模块)对象的获得交给第三方(系统)来控制,即依赖对象不在被依赖模 ...
- ROW_NUMBER分页的注意事项
之前在使用ROW_NUMBER分页获取数据的时候,直接用ROW_NUMBER里的SELECT语句查出了所有的数据. like this: select * from ( select row_numb ...
- yzoi1109&&viojs1042最小步数的一点看法——回文数
Description - 问题描述 有一天,雄霸传授本人风神腿法第一式:捕风捉影..............的步法(弟子一:堂主,你大喘气呀.风:你给我闭嘴.)捕风捉影的关键是换气(换不好就会大喘气 ...
- XAMPP 使用教程
XAMPP 是一个把Apache网页服务器与PHP.Perl及MySQL集合在一起的安装包,允许用户可以在自己的电脑上轻易的建立网页服务器.使用 XAMPP 您可以轻松的在本机调试您的 PHP ...