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代码及总结这道题目的总结 题目要求 ...
随机推荐
- HashMap深度解析(二)
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/16890151 上一篇比较深入的分析了HashMap在put元素时的整体过 ...
- 本地tomcat访问mysql数据库
虽然以前经常听人说起过tomcat,但是今天头一次使用tomcat. 1.Tomcat的安装过程: 首先应该从Apache官方网站上下载是用于Windows的.zip压缩包. 下面是相应的下载链接: ...
- HTML meta标签总结与属性使用介绍
之前学习前端中,对meta标签的了解仅仅只是这一句. <meta charset="UTF-8"> 但是打开任意的网站,其head标签内都有一列的meta标签.比如我博 ...
- Spring_DI利用set方法赋值Demo
Person.java public class Person { private Long pid; private String pname; private Student student; p ...
- 【HOJ2430】【贪心+树状数组】 Counting the algorithms
As most of the ACMers, wy's next target is algorithms, too. wy is clever, so he can learn most of th ...
- JQUERY、AJAX双击DIV,直接修改DIV内的内容
最近在做后台功能开发的时候,用到对排序字段的修改,感觉只为了修改一个排序值,而要重新进入编辑页比较麻烦,于是自己动手写…… 最近在做后台功能开发的时候,用到对排序字段的修改,感觉只为了修改一个排序值, ...
- 如何为jquery添加方法
以下内容引自一位网友的帖子: jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery ...
- mysql数据类型——时间类型
四种日期格式: 每个时间类型有一个有效值范围和一个"零"值,当指定不合法的MySQL不能表示的值时使用"零"值. YEAR 0000 YYYY ...
- 基于BitNami for XAMPP进行Drupal7安装的教程(Win7平台)
BitNami是一个开源项目,该项目产生的开源软件包可用于安装Web应用程序和解决方案堆栈,以及虚拟设备.BitNami 提供wordpress.joomla.drupal.bbpress等开源程序的 ...
- uboot环境变量分析
uboot的环境变量在程序的运行和调试过程中都发挥着比较重要的作用. 一.环境变量 可以理解为全局变量,但是他的生命周期比全局变量要长,当程序已经结束运行时,全局变量就会消亡,但是环境变量在下次上电运 ...