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 最大流 第二题的更多相关文章

  1. poj 1459 (最大流)

    最大流简单题,,这题重要的是知道了scanf("%s",str);sscanf(str,"(%d,%d)%d",&x,&y,&w);读入 ...

  2. POJ 1087 最大流裸题 + map

    A Plug for UNIX Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15597   Accepted: 5308 ...

  3. Power Network - poj 1459 (最大流 Edmonds-Karp算法)

      Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 24788   Accepted: 12922 Description A ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. 05:统计单词数【NOIP2011复赛普及组第二题】

    05:统计单词数 总时间限制:  1000ms 内存限制:  65536kB 描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次 ...

  9. hdu Flow Problem (最大流 裸题)

    最大流裸题,贴下模版 view code#include <iostream> #include <cstdio> #include <cstring> #incl ...

随机推荐

  1. 【codeforces 335A】Banana

    [题目链接]:http://codeforces.com/contest/335/problem/A [题意] 让你构造一个长度为n的字符串; 每次你可以从这个字符串中任意取走字符; 让你求出取的次数 ...

  2. 2015 Multi-University Training Contest 1 hdu 5290 Bombing plan

    Bombing plan Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  3. LaTeX 简介与安装

    1 LaTeX 简介 Leslie Lamport 开发的 LaTeX 是当今世界上最流行和使用最为广泛的TeX格式.它构筑在Plain TeX的基础之上,并加进了很多的功能以使得使用者可以更为方便的 ...

  4. Ubuntu系统中中文的设置

    这几天心血来潮,在实习公司真的没什么事情做,在学长的推荐下就学习了一下Ubuntu系统,感觉还是挺不错的,这个系统,所以我也推荐刚开始学习liunx系统的妹子们也可以先学一下这个. 1>你可以在 ...

  5. php中的页面跳转和重定向

    php中的页面跳转和重定向 ThinkPHP中跳转和重定向的区别 跳转: 浏览器认为: 当前URL请求成功, 重新请求新的URL. 浏览器会 记录当前的URL 和 新的URL 在请求历史记录中. 回退 ...

  6. ORM中基于对象查询与基于queryset查询

    感谢老男孩~  一步一步走下去 前面是视图函数 后面是表结构models.py from django.shortcuts import render, HttpResponse from djang ...

  7. 36.创建模板mylist

    node.h #pragma once //创建模板 template <class T> class Node { public: T t;//数据 Node *pNext;//指针域 ...

  8. [C++] upper_bound和lower_bound

    upper_bound 源码 template <class ForwardIterator, class T> ForwardIterator upper_bound (ForwardI ...

  9. C#篇(二)——属性的实质

    属性的内部实现其实就是方法 我们平时写的代码: class Student { private int age; public int Age { get { return age; } set { ...

  10. 文本域内容在div中带换行显示

    function ReplaceSeperator(mobiles) { var i; var result = ""; var c; for (i = 0; i < mob ...