POJ 1459 最大流 第二题
http://poj.org/problem?id=1459
也是网络流的基础,只是虚拟出一个源点和终点,对应的生产值和消费值就加到与源点和终点的边上,然后做一次bfs就好了。
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#define INF 999999999
//#define OPEN_FILE
using namespace std; const int MAXN = ;
int cap[MAXN][MAXN], flow[MAXN][MAXN], p[MAXN], c[MAXN], f[MAXN];
int n, ans; void get_cap(char s[])
{
int i, u, v, w;
u = ;
for (i = ; s[i] != ','; i++){
u = u * + (s[i] - '');
}
v = ;
for (++i; s[i] != ')'; i++){
v = v * + (s[i] - '');
}
int len = strlen(s);
w = ;
for (++i; i < len; i++){
w = w * + (s[i] - '');
}
cap[u][v] = w;
} void get_pc(char s[], int* t, int o)
{
int i, u, w;
u = ;
for (i = ; s[i] != ')'; i++){
u = u * + (s[i] - '');
}
int len = strlen(s);
w = ;
for (++i; i < len; i++){
w = w * + (s[i] - '');
}
if (o){
cap[n][u] = w;
}
else{
cap[u][n + ] = w;
} } void ek_bfs(){
queue<int> d;
int s, t, u, v;
int a[MAXN];
s = n;
t = n + ;
ans = ;
while (){
memset(a, , sizeof(a));
a[s] = INF;
d.push(s);
while (!d.empty()){
u = d.front();
d.pop();
for (v = ; v <= n + ; v++){
if (a[v] || cap[u][v] <= flow[u][v]) continue;
f[v] = u;
d.push(v);
a[v] = min(a[u] , cap[u][v] - flow[u][v]);
}
}
if (a[t] == ) break;
for (u = t; u != s; u = f[u]){
flow[f[u]][u] += a[t];
flow[u][f[u]] -= a[t];
}
ans += a[t];
}
} int main()
{
#ifdef OPEN_FILE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int m, np, nc, i, j;
char s[];
while (~scanf("%d%d%d%d", &n, &np, &nc, &m)){
memset(cap, , sizeof(cap));
memset(flow, , sizeof(flow));
memset(p, , sizeof(p));
memset(c, , sizeof(c));
for (i = ; i <= m; i++){
scanf("%s", s);
get_cap(s);
}
for (i = ; i <= np; i++){
scanf("%s", s);
get_pc(s, p, );
}
for (i = ; i <= nc; i++){
scanf("%s", s);
get_pc(s, c, );
}
ek_bfs();
printf("%d\n", ans);
}
}
POJ 1459 最大流 第二题的更多相关文章
- poj 1459 (最大流)
最大流简单题,,这题重要的是知道了scanf("%s",str);sscanf(str,"(%d,%d)%d",&x,&y,&w);读入 ...
- POJ 1087 最大流裸题 + map
A Plug for UNIX Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15597 Accepted: 5308 ...
- Power Network - poj 1459 (最大流 Edmonds-Karp算法)
Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24788 Accepted: 12922 Description A ...
- Going Home POJ - 2195 费用流板子题
On a grid map there are n little men and n houses. In each unit time, every little man can move one ...
- POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Network / FZU 1161 (网络流,最大流)
POJ 1459 Power Network / HIT 1228 Power Network / UVAlive 2760 Power Network / ZOJ 1734 Power Networ ...
- poj 1459 多源多汇点最大流
Sample Input 2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 ...
- hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))
Special Fish Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 05:统计单词数【NOIP2011复赛普及组第二题】
05:统计单词数 总时间限制: 1000ms 内存限制: 65536kB 描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次 ...
- hdu Flow Problem (最大流 裸题)
最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...
随机推荐
- C语言手册-read
名称: pread,read-从文件读 语法: #include <unistd.h> ssize_t pread(int fildes, void *buf, size_t nbyte, ...
- System.IO.IsolatedStorage 使用 IsolatedStorageFileStream 存储信息
在C#中还有一种叫做IsolatedStorage的存储机制,他存储信息的方式类似于我们的cookie, IsolatedStorage存储独立于每一个application,换句话说我们加载多个应用 ...
- Samurai's Stroke
题目链接 题意: 一个长度为L的木棍,有n个支点支撑,每一个点是一个int数.表示距离木棍左端点的距离.求在那些位置将木棍劈开能够使得至少有一个木棍掉下去,输出这些位置的长度 3 ≤ l ≤ 109; ...
- mac和iphone处理视频
今天在微信上面发现有视频打不开,也无法下载到相册 而到电脑上可以打开 搜了一下,发现格式不对,mp4有很多格式,有的是苹果支持不了的. 要下载一个转换器,我下载了“超级转霸”,然后把视频转成了ipho ...
- Asterisk[1]
Asterisk[1]是一款GPLv2协议下的开源电话应用平台.简单来说,Asterisk是一个server应用.可以完毕发起电话呼叫.接受电话呼叫.对电话呼叫进行定制处理. 1.2.1 通道驱动 a ...
- xml布局内容总结(四)--Android
(1)对于xml编写界面较复杂的情况下,使用include会使得编写和查看更清楚 <LinearLayout android:layout_width="mat ...
- 小贝_mysql select5种子句介绍
mysql select5种子句介绍 简要 一.五种字句 二.具体解释五种字句 一.五种字句 where.group by.having.order by.limit 二.具体解释五种字句 2.1.理 ...
- 【数据压缩】JPEG标准与原理解析
转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/50392230 CSDN-勿在浮沙筑高台 为了满足不同应用的需求,JPEG标准包 ...
- App server 与 Web server之间的区别
原文: http://www.javaworld.com/javaqa/2002-08/01-qa-0823-appvswebserver.html 简单来说,web服务器提供页面给浏览器,而app服 ...
- IDEA項目配置404
本人使用的版本是 Intellij IDEA 2017.1 最近刚学习使用 Intellij IDEA 开发项目,就遇到了坑爹的问题,部署一个简单的 ssh 项目,tomcat启动正常,没有任 ...