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 (2,4)7
(3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5
(0)5 (1)2 (3)2 (4)1 (5)4 7个点包括电站和用户,2个电站,3个用户,13条边,输入13条边,输入2个电站,输入3个用户


Sample Output

15
6 增加一个源点一个汇点即可
 /*
POJ 1459
*/ #include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#include<iostream>
using namespace std;
//****************************************************
//最大流模板
//初始化:g[][],start,end
//******************************************************
const int MAXN=;
const int INF=0x3fffffff;
int g[MAXN][MAXN];//存边的容量,没有边的初始化为0
int path[MAXN],flow[MAXN],start,end;
int n;//点的个数,编号0-n.n包括了源点和汇点。 queue<int>q;
int bfs()
{
int i,t;
while(!q.empty())q.pop();//把清空队列
memset(path,-,sizeof(path));//每次搜索前都把路径初始化成-1
path[start]=;
flow[start]=INF;//源点可以有无穷的流流进
q.push(start);
while(!q.empty())
{
t=q.front();
q.pop();
if(t==end)break;
//枚举所有的点,如果点的编号起始点有变化可以改这里
for(i=;i<=n;i++)
{
if(i!=start&&path[i]==-&&g[t][i])
{
flow[i]=flow[t]<g[t][i]?flow[t]:g[t][i];
q.push(i);
path[i]=t;
}
}
}
if(path[end]==-)return -;//即找不到汇点上去了。找不到增广路径了
return flow[end];
}
int Edmonds_Karp()
{
int max_flow=;
int step,now,pre;
while((step=bfs())!=-)
{
max_flow+=step;
now=end;
while(now!=start)
{
pre=path[now];
g[pre][now]-=step;
g[now][pre]+=step;
now=pre;
}
}
return max_flow;
}
int main()//多源多汇点,在前面加个源点,后面加个汇点,转成单源单汇点
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int np,nc,m;
int u,v,z;
while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF)
{
memset(g,,sizeof(g));
while(m--)
{
while(getchar()!='(');
scanf("%d,%d)%d",&u,&v,&z);
u++;v++;
g[u][v]=z;
}
while(np--)
{
while(getchar()!='(');
scanf("%d)%d",&u,&z);
u++;
g[][u]=z;
}
while(nc--)
{
while(getchar()!='(');
scanf("%d)%d",&u,&z);
u++;
g[u][n+]=z;
}
n++;
start=;
end=n;
printf("%d\n",Edmonds_Karp());
}
return ;
}

poj 1459 多源多汇点最大流的更多相关文章

  1. POJ 1459:Power Network(最大流)

    http://poj.org/problem?id=1459 题意:有np个发电站,nc个消费者,m条边,边有容量限制,发电站有产能上限,消费者有需求上限问最大流量. 思路:S和发电站相连,边权是产能 ...

  2. poj 1459 多源汇网络流 ISAP

    题意: 给n个点,m条边,有np个源点,nc个汇点,求最大流 思路: 超级源点把全部源点连起来.边权是该源点的最大同意值: 全部汇点和超级汇点连接起来,边权是该汇点的最大同意值. 跑最大流 code: ...

  3. POJ 1459 - Power Network 【Ek-最大流】

    <题目链接> 题目大意:给出 n 个点,其中包括 np个发电站,nc 个消费者, 剩下的全部都是中转点,再给出 这些点中的m 条边,代表这两点间的最大传输电量,并且给出发电站的最大发送电量 ...

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

  5. POJ 1459 &amp;&amp; ZOJ 1734--Power Network【最大流dinic】

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25108   Accepted: 13077 D ...

  6. 网络流之最大流Dinic --- poj 1459

    题目链接 Description A power network consists of nodes (power stations, consumers and dispatchers) conne ...

  7. 2018.07.06 POJ 1459 Power Network(多源多汇最大流)

    Power Network Time Limit: 2000MS Memory Limit: 32768K Description A power network consists of nodes ...

  8. POJ 1459 Power Network(网络流 最大流 多起点,多汇点)

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22987   Accepted: 12039 D ...

  9. Poj(1459),最大流,EK算法

    题目链接:http://poj.org/problem?id=1459 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Su ...

随机推荐

  1. map vs hash_map

    1. map, multimap, set, multiset g++ 中 map, multimap, set, multiset 由红黑树实现 map: bits/stl_map.h multim ...

  2. WPF 任务栏图标闪烁提醒

    using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServi ...

  3. JVM垃圾收集策略解析

    地址:http://developer.51cto.com/art/201002/184385_all.htm

  4. js实现table排序-sortable.js

    方案一.引用sortable.js包 /* <th class="thcss" style="width: 40px;" onclick="so ...

  5. PHP随笔

    php(PHP开发) PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,易于学习,使用广泛 ...

  6. LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android

    LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android jincon 发表于 2015-02-26 18:31:01 发表在: php开发 localresiz ...

  7. 【分布式存储】GlusterFS failing to mount at boot with Ubuntu 14.04

    GlusterFS failing to mount at boot with Ubuntu 14.04   Previously I asked about mounting GlusterFS a ...

  8. 【Spring】Spring系列6之Spring整合Hibernate

    6.Spring整合Hibernate 6.1.准备工作 6.2.示例 com.xcloud.entities.book com.xcloud.dao.book com.xcloud.service. ...

  9. 【云计算】开源的Docker Registry WebUI

    kwk/docker-registry-frontend   Code       Issues         9       Pull requests       6       Wiki   ...

  10. JavaScript String 对象方法

    String 对象方法 方法 描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. bold() 使用粗体显示字符串. charAt() ...