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代码及总结这道题目的总结 题目要求 ...
随机推荐
- java中XMLGregorianCalendar类型和Date类型之间的相互转换
import java.text.SimpleDateFormat;import java.util.Date;import java.util.GregorianCalendar;import ja ...
- 【原创】不用封装jar包 直接引入工程使用的方法(类似android的 is Library功能)
1.制作lib工程,这里我简单制作一个测试类 2.eclipse中 java Project工程引入方法 2.1.新建个java工程,在属性配置中选择 "Java Build Path&qu ...
- How to Make LastPass Even More Secure with Google Authenticator
Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...
- GCD介绍(二): 多核心的性能
GCD介绍(二): 多核心的性能 概念 为了在单一进程中充分发挥多核的优势,我们有必要使用多线程技术(我们没必要去提多进程,这玩意儿和GCD没关系).在低层,GCD全局dispatc ...
- 通常我们使用[NSDate date]方法得到的时间与当前时间不一致,如何解决?
NSDate *date = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger interv ...
- Java设计模式(学习整理)---策略模式
1. 模式定义 把会变化的内容取出并封装起来,以便以后可以轻易地改动或扩充部分,而不影响不需要变化的其他部分: 2.模式本质: 少用继承,多用组合,简单地说就是:固定不变的信息 ...
- in_array 判断问题的疑惑解决。
面试题中有一条是关于in_array判断的,题目如下: 如何大家没有深入了解in_array的类型判断过程,而是根据经验来选择,肯定很多人也是是选择了D答案的,具体的原因我也是从牛人的博客里面得到答案 ...
- cos-26上传个人案例
package cn.gdpe.upload; import java.io.File;import java.io.IOException;import java.util.Enumeration; ...
- easyui tree 判断点击的节点是否还存在子节点
有些业务需求是要求tree一次性全部加载,有些是需要异步加载的. 如果是一次性全部加载的tree,那怎么判断点击的节点是否还存在子节点? function loadTree(){ $('#tree') ...
- error_reporting()函数用法
首先要知道error_reporting()函数是用来设置错误级别并返回当前级别的.它有14个错误级别,如下: 1 E_ERROR 致命的运行时错误. 错误无法恢复过来 ...