http://poj.org/problem?id=1459

题意:有一个电路网络,每个节点可以产生、传递、消耗若干电量,有点线连接结点,每个电线有最大传输量,求这个网络的最大消费量。

思路:从源点到发电站连边,流量为发电量,从用户到汇点连边,流量为消费量,再根据电线连双向边,求最大流即可。

 #include<stdio.h>
#include<string.h>
#include<queue>
const int N=;
const int INF=<<;
using namespace std;
int map[N][N];
int pre[N];
int n,np,nc,m; int bfs(int s,int d)
{
queue<int>q;
memset(pre,-,sizeof(pre));
pre[s] = ;
int k;
q.push(s);
while(!q.empty())
{
k = q.front();
q.pop();
for (int i = ; i <= n+; i ++)
{
if (pre[i]==- && map[k][i] > )
{
pre[i] = k;
if (i==d)
return ;
q.push(i);
}
}
}
return ;
}
int maxflow(int s,int d)
{
int maxf = ;
while(bfs(s,d))
{
int minf = INF;
for (int i = d; i!=s; i = pre[i])
//minf = minf < map[pre[i]][i] ? minf:map[pre[i]][i];
minf = min(minf,map[pre[i]][i]);
for (int i = d; i!=s; i = pre[i])
{
map[pre[i]][i] -= minf;
map[i][pre[i]] += minf;
}
maxf += minf;
}
return maxf;
}
int main()
{
int s,d;
int u,v,w;
char str[];
while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
{
memset(map,,sizeof(map));
s = n;
d = n+;
for (int i = ; i < m; i ++)
{
scanf("%s",str);
sscanf(str,"(%d,%d)%d",&u,&v,&w);
map[u][v] = w; }
for (int i = ; i < np; i ++)
{
scanf("%s",str);
sscanf(str,"(%d)%d",&v,&w);
map[s][v] = w;
}
for (int i = ; i < nc; i ++)
{
scanf("%s",str);
sscanf(str,"(%d)%d",&u,&w);
map[u][d] = w;
}
printf("%d\n",maxflow(s,d));
}
return ;
}

最新文章

  1. 去除inline-block元素间间距
  2. 字符串、数组方法实战--charAt(),split(),indexOf(),substring()
  3. LeetCode之371. Sum of Two Integers
  4. iOS开发——网络实用技术OC篇&amp;网络爬虫-使用青花瓷抓取网络数据
  5. JSONP理解和使用
  6. Android多线程----异步消息处理机制之Handler详解
  7. Java网络编程(UDP协议-聊天程序)
  8. Net中的AOP
  9. fork();
  10. JavaScript-打开新窗口
  11. Jsp中使用数据库连接池.
  12. 通常编译亲测56Y国际象棋源代码,精仿56Y国际象棋完整的源代码下载!
  13. POI-处理大Excel文件(xls)
  14. ubuntu16.04 英文环境安装google chrome
  15. jstl常用语句
  16. nyoj 数的长度
  17. 人工智能算法综述(二) RNN and LSTM
  18. 一个spinner控件使用的实例
  19. 【搬运】 Page Object 官方文档 (新增了Widget特性)
  20. day11-(cookie&amp;&amp;session)

热门文章

  1. 10、scala面向对象编程之Trait
  2. Linux内存压力测试-memtester工具
  3. VS2015编译ffmpeg的问题解决
  4. HDU 4027(线段树)
  5. [pytorch学习]1.pytorch ubuntu安装
  6. ionic使用cryptojs加密 复制到黏贴版 使用md5
  7. python学习笔记--深拷贝与浅拷贝的区别
  8. hdu 5179 beautiful number
  9. 【codeforces 527B】Error Correct System
  10. C. Vladik and Memorable Trip DP