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代码及总结这道题目的总结 题目要求 ...
随机推荐
- 介绍一个小工具 Linqer
http://www.cnblogs.com/huangxincheng/archive/2011/05/12/2044990.html
- Linq 调试
void Main() { var MyMonitor = new Devart.Data.Oracle.OracleMonitor(); MyMonitor.IsActive = true; var ...
- java Web Services搭建环境时遇到的各种问题,记录一下。 java.lang.OutOfMemoryError: PermGen space,org/apache/struts2/util/ObjectFactoryDestroyable
情况:在同一个,myEclipes 下加载俩个项目,一个seriver端,一个client端. 必备: myEclipes ,apache-tomcat-7.0.42,apache-tomcat ...
- Deep Learning 学习随记(五)深度网络--续
前面记到了深度网络这一章.当时觉得练习应该挺简单的,用不了多少时间,结果训练时间真够长的...途中debug的时候还手贱的clear了一下,又得从头开始运行.不过最终还是调试成功了,sigh~ 前一篇 ...
- iOS中ARC内部原理
ARC会自动插入retain和release语句.ARC编译器有两部分,分别是前端编译器和优化器. 1. 前端编译器 前端编译器会为“拥有的”每一个对象插入相应的release语句.如果对象的所有权修 ...
- JS获取屏幕各种高
<script language="javascript"> var h = ""; h += " 网页可见区域宽:"+ doc ...
- Vijos1734 NOI2010 海拔 平面图最小割
建立平面图的对偶图,把最小割转化成最短路问题 Dijkstra算法堆优化 (被输入顺序搞WA了好几次T_T) #include <cstdio> #include <cstring& ...
- 用Apache实现一个ip虚拟多个web站点
如何用Apache实现一个ip虚拟多个web站点? 首先添加虚拟的服务器名 <virtualhost www.xxx.com:80="">DocumentRoot d: ...
- 模块化编程AMD&CommonJS
为什么要模块化编程 如果JS也可以像类似python,Java使用import,引入我们想要的模块,想要什么模块,就加载什么模块,可以给前端编程带来更多的便捷,结构更加清晰明了.但是,这样做有一个前提 ...
- bzoj3583: 杰杰的女性朋友 && 4362: Graph
Description 给出一张n个点的有向图G(V,E).对于任意两个点u,v(u可以等于v),u向v的连边数为: ∑OUT(u,i) * IN(v,i),其中1<=i<=K 其中k和数 ...