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);
}
}

最新文章

  1. WPF中找不到Image或者Image不是Drawing系列
  2. 使用cachemanager做缓存(Session的缓存)
  3. 学习ASP.NET缓存机制
  4. HBase 分布式环境搭建
  5. Linux选型:开源不是免费 首选红帽和SUSE
  6. 【Java线程池快速学习教程】
  7. linux zeromq
  8. CodeForces 412D Giving Awards
  9. 数据转换d2d.js
  10. Python编程笔记 - 列表
  11. DevExpress中GridControl自定义汇总列值(有选择性的汇总)
  12. 高性能缓存系统Memcached在ASP.NET MVC中应用
  13. UART简介及与COM口的区别
  14. win7 X64系统上 PL/SQL不能识别Oracle实例
  15. Application Request Route实现IIS Server Farms集群负载
  16. dev16 cxgrid 在DLL里报0地址错
  17. C# 希尔排序
  18. jmeter验证WEB页面的href链接请求
  19. HTML解析HtmlAgility学习
  20. UVA1185 Big Number

热门文章

  1. Json相关内容
  2. 《Craking the Coding interview》python实现---01
  3. #undef 的用法
  4. CodeForces 453A
  5. TCP/IP、UDP、 Http、Socket的差别
  6. Linux怎样改动root用户的password
  7. Constructor call must be the first statement in a constructor
  8. python对MySQL进行添加修改删除以及字符串的操作
  9. NodeJS学习笔记 进阶 (4)基于express+muter的文件上传(ok)
  10. 洛谷1726 上白泽慧音 tarjan模板