POJ 1459 网络流 EK算法
题意:
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20
2 1 1 2 表示 共有2个节点,生产能量的点1个,消耗能量的点1个, 传递能量的通道2条;
(0,1)20 (1,0)10 代表(起点,终点)最大传递的能量 (0)15 (产生能量的点)产生的最大能量
(1)20 (消费能量的点)消费的最大能量 初学网络流,我想从基础练起;就先用EK算法写一遍 这道题看似很难,但其实只要加一个源点以及汇点,让所有的产生能量的点指向源点,让所有的消费能量的点指向汇点;
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define N 110
#define INF 0x3f3f3f3f
#define Min(a, b) a<b?a:b
int G[N][N], pre[N]; int EK(int s, int e);
bool BFS(int s, int e); int main()
{
int point, source, dest, edge;
while(scanf("%d%d%d%d", &point, &source, &dest, &edge)!=EOF)
{
int a, b, flow, in, inflow, out, outflow;
char ch;
memset(G, 0, sizeof(G));
for(int i=0; i<edge; i++)
{
//scanf("(%d,%d)%d", &a, &b, &flow);
cin>>ch>>a>>ch>>b>>ch>>flow;
G[a+1][b+1]+=flow;
} for(int i=0; i<source; i++)
{
//scanf("(%d)%d", &out, &outflow);
cin>>ch>>out>>ch>>outflow;
G[0][out+1]+=outflow;
} for(int i=0; i<dest; i++)
{
//scanf("(%d)%d", &in, &inflow);
cin>>ch>>in>>ch>>inflow;
G[in+1][point+1]+=inflow;
} int ans=EK(0, point+1);
printf("%d\n", ans); }
return 0;
} int EK(int s, int e)
{
int maxflow=0; while(BFS(s, e))
{
int minflow=INF; for(int i=e; i!=s; i=pre[i])
minflow=Min(minflow, G[pre[i]][i]); for(int j=e; j!=s; j=pre[j])
{
G[pre[j]][j]-=minflow;
G[j][pre[j]]+=minflow;
}
maxflow+=minflow;
} return maxflow;
} bool BFS(int s, int e)
{
memset(pre, -1, sizeof(pre)); queue<int>Q;
Q.push(s); while(Q.size())
{
int i=Q.front(); Q.pop(); if(i==e)
return true; for(int j=0; j<=e; j++)
{
if(G[i][j]&&pre[j]==-1)
{
pre[j]=i;
Q.push(j);
}
}
}
return false;
}
POJ 1459 网络流 EK算法的更多相关文章
- 最大网络流 EK 算法
网络流是什么类型的问题,看一道题目你就知道了 点击打开链接 . 默认具备图论的基本知识,网络流概念比较多,先看看书熟悉一下那些概念.比较好!一个寄出的网络最大流.EK算法写的. 这是一幅网络,求S ...
- 网络流EK算法模板
\(EK\)算法的思想就是每一次找一条增广路进行增广. 注意几个点: 存图时\(head\)数组要设为\(-1\). 存图的代码是这样的: inline void add(int u, int v, ...
- HDU1532 Drainage Ditches 网络流EK算法
Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...
- Drainage Ditches(网络流(EK算法))
计算最大流,EK算法模板题. #include <stdio.h> #include <string.h> #include <queue> using names ...
- poj 1459 网络流问题`EK
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 24930 Accepted: 12986 D ...
- Power Network POJ - 1459 [网络流模板]
http://poj.org/problem?id=1459 嗯,网络流模板...多源点多汇点的图,超级汇点连发电厂,用户连接超级汇点 Status Accepted Time 391ms Memor ...
- Power Network (poj 1459 网络流)
Language: Default Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 23407 ...
- 网络流Ek算法
例题: Flow Problem HDU - 3549 Edmonds_Karp算法其实是不断找增广路的过程. 但是在找的过程中是找"最近"的一天增广路, 而不是找最高效的一条增 ...
- POJ 3281 网络流dinic算法
B - Dining Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
随机推荐
- Apache Nifi 开发
Apache NiFi是由美国过国家安全局(NSA)贡献给Apache基金会的开源项目.其设计目标是自己主动化系统间的数据流.基于其工作流式的编程理念.NiFi很易于使用,强大.可靠及高可配置. 两个 ...
- 关于java后台如何接收xml格式的数据
业务场景:用户发送下单请求,格式为xml格式,服务器接收数据完成下单,并返回结果给客户. 请求格式: <request> <head> <sign></sig ...
- java中的四种权限
1.私有权限(private) private可以修饰数据成员,构造方法,方法成员,不能修饰类(此处指外部类,不考虑内部类).被private修饰的成员,只能在定义它们的类中使用,在其他类中不能调用. ...
- style="display:{{searchInput==='' ? 'none':'block'}} "
当用户没有有效输入时,是否显示提交按钮 style="display:{{searchInput==='' ? 'none':'block'}} "
- GBK和UTF-8文字编码的区别
UTF-8是一种国际化标准的文字编码,我们已知Windows系统程序已经将最初的UTF-8转向Unicode,而GBK的存在是为了中国国情而创造的,不过GBK也将伴随着中文字符的一直流传下去. GBK ...
- 基于网页api(接口)实现查快递
之前在网上找到一款下载某慕课网站的java版软件,我想知道他是怎么实现:对于视频的下载的,毕竟网页源码中大都不会直接放视频的地址,但是没有公布源码,我就反编译,等到了部分“源码”,逻辑上还是有些问题, ...
- 洛谷 P3263 [JLOI2015]有意义的字符串
洛谷 首先,看到\((\frac{(b+\sqrt{d})}{2})^n\),很快能够想到一元二次方程的解\(\frac{-b\pm\sqrt{\Delta}}{2a}\). 所以可以推出,\(\fr ...
- PAT 1002. A+B for Polynomials (25)
This time, you are supposed to find A+B where A and B are two polynomials. Input Each input file con ...
- ubuntu 安装Nodejs
ubuntu 安装Nodejs 1.在软件管理器里面安装nodejs2.由于版本很老,所以需要更新版本:先安装npm , sudo apt install npm然后用npm安装 n 命令,更新nod ...
- LeNet5
Lecun Y, Bottou L, Bengio Y, et al. Gradient-based learning applied to document recognition[J]. Proc ...