Power Network

POJ-1459

  • 这题值得思索的就是特殊的输入,如何输入一连串字符。这里采用的方法是根据输入已知的输入格式,事先预定好要接受的数据类型。
  • 这里套用的板子也是最大流的模板,但是虽然可以ac但是时间有点卡,所以如果可以的话还是使用高级的算法。
#include<iostream>
#include<algorithm>
#include<fstream>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<cstring>
using namespace std;
//fstream fin("1.txt");
//streambuf *buf = cin.rdbuf(fin.rdbuf());//用于重定项输入改成,把cin当成fin
const int INF = 1 << 29;
const int maxn = 310;
int n, np, nc, m;
struct Edge {
int from, to, cap, flow;
Edge(int u, int v, int c, int f) : from(u), to(v), cap(c), flow(f) {}
};
struct EK{
int m;
vector<Edge> edges;//这里的边是实际边数的两倍,包括反向边
vector<int> G[maxn];//邻接表,G[i][j]表示结点i的第j条边在edges数组中的序号
int a[maxn];//a[i]表示起点到i的可改进量
int p[maxn];//edges数组中的编号,最短路图上p的入弧编号
void init(int n) {
for (int i = 0; i <= n; i++)
G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));//反向弧,容量为0
m = edges.size();
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}
int Maxflow(int s, int t) {
int flow = 0;
for (;;) {
memset(a, 0, sizeof(a));
queue<int> Q;
Q.push(s);
a[s] = INF;
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < G[x].size(); i++) {
Edge& e = edges[G[x][i]];
if (!a[e.to] && e.cap > e.flow) {
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap - e.flow);
Q.push(e.to);
}
}
if (a[t]) break;
}
if (!a[t]) break;
for (int u = t; u != s; u = edges[p[u]].from) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
}
flow += a[t];
}
return flow;
}
}ek;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
char rubbish;
int first, next;
int value;
while (cin >> n >> np >> nc >> m)
{
ek.init(n+1);
int s = n;
int t = n + 1;
for (int i = 0; i < m; i++)
{
cin >> rubbish;
cin >> first;
cin >> rubbish;
cin >> next;
cin >> rubbish;
cin >> value;
//cout<<rubbish<<" "<<first<<" "<<rubbish<<" "<<next<<" "<<rubbish<<" "<<value<<endl;
if (first == next)
continue;
//getchar();
ek.AddEdge(first,next,value);
}
for (int i = 0; i < np; i++)
{
cin >> rubbish;
cin >> first;
cin >> rubbish;
cin >> value;
ek.AddEdge(s,first,value);//超级源点与供电点相连
}
for (int i = 0; i < nc; i++)
{
cin >> rubbish;
cin >> first;
cin >> rubbish;
cin >> value;
ek.AddEdge(first,t,value);;//超级汇点与消费点相连
}
cout<<ek.Maxflow(s,t)<<endl;
}
return 0;
}

POJ-1459(最大流+EK算法)的更多相关文章

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

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

  2. 二分图的最大匹配——最大流EK算法

    序: 既然是个图,并且求边数的最大值.那么这就可以转化为网络流的求最大流问题. 只需要将源点与其中一子集的所有节点相连,汇点与另一子集的所有节点相连,将所有弧的流量限制置为1,那么最大流 == 最大匹 ...

  3. 最大流EK算法/DINIC算法学习

    之前一直觉得很难,没学过网络流,毕竟是基础知识现在重新来看. 定义一下网络流问题,就是在一幅有向图中,每条边有两个属性,一个是cap表示容量,一个是flow 表示流过的流量.我们要求解的问题就是从S点 ...

  4. 最大流——EK算法

    一.算法理论 [基本思想] 反复寻找源点s到汇点t之间的增广路径,若有,找出增广路径上每一段[容量-流量]的最小值delta,若无,则结束.在寻找增广路径时,可以用BFS来找,并且更新残留网络的值(涉 ...

  5. (通俗易懂小白入门)网络流最大流——EK算法

    网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...

  6. vector实现最大流EK算法

    序: 在之前的文章中实现了不利用STL实现EK算法,效率也较高.这次我们企图简化代码,减少变量的使用与手写模拟的代码. 注意:vector等STL的container在不开O2优化的时候实现同一个效果 ...

  7. 最大流EK算法模板

    最近学了下最大流算法,大概思想算是懵懵懂懂了,现在想把模板记录下来,以备后面深刻学习之用. #include<cstdio> #include<cstring> using n ...

  8. 【转】最大流EK算法

    转自:http://www.cnblogs.com/kuangbin/archive/2011/07/26/2117636.html 图-1 如图-1所示,在这个运输网络中,源点S和汇点T分别是1,7 ...

  9. POJ1273 最大流 EK算法

    套了个EK的模板 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdi ...

随机推荐

  1. hdu5637 Transform (bfs+预处理)

    Problem Description A list of n integers are given. For an integer x you can do the following operat ...

  2. hdu1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  3. Codeforces Round #428 (Div. 2) C. Journey (简单搜索)

    题意:给你一颗树(边是无向的),从根节点向下走,统计走到每个子节点的概率,求所有叶子节点的深度乘上概率的和. 题解:每层子节点的概率等于上一层节点的概率乘\(1\)除以这层的子节点数,所以我们用\(d ...

  4. Codeforces Round #647 (Div. 2) - Thanks, Algo Muse! C. Johnny and Another Rating Drop (规律,二进制)

    题意:有一个正整数\(n\),要求写出所有\(1\)~\(n\)的二进制数,统计相邻的两个二进制同位置上不同数的个数. 题解:打表找规律,不难发现: ​ \(00000\) ​ \(00001\) ​ ...

  5. 2019-2020 ACM-ICPC Brazil Subregional Programming Contest Problem M Maratona Brasileira de Popcorn (二分)

    题意:有\(n\)袋爆米花,某个队伍有\(c\)个队员,每个队员每秒做多可以吃\(t\)粒爆米花,但一袋爆米花只能由一个队员吃完,并且一个队员只能吃连续的一袋或几袋,不能隔着吃某一袋,求将所有爆米花吃 ...

  6. Codeforces Round #521 (Div. 3) E. Thematic Contests (离散化,二分)

    题意:有\(n\)个话题,每次都必须选取不同的话题,且话题数必须是上次的两倍,第一次的话题数可以任意,问最多能选取多少话题数. 题解:我们首先用桶来记录不同话题的数量,因为只要求话题的数量,与话题是多 ...

  7. ElasticSearch 交互使用

    Curl 命令 # 建立索引 [root@dbtest01 ~]# curl -XPUT 'http://10.0.0.121:9200/test' # 插入数据 [root@dbtest01 ~]# ...

  8. mybatis(四)缓存机制

    转载:https://www.cnblogs.com/wuzhenzhao/p/11103043.html 缓存是一般的ORM 框架都会提供的功能,目的就是提升查询的效率和减少数据库的压力.跟Hibe ...

  9. Pycharm无法import caffe

    这里是首先建立在读者可以在终端导入而无法在Pycharm中导入的情况下的: 参考链接(问题的最后一个回答) 选用了虚拟环境的python作为解释器, 但由于caffe的特殊性, 依然没有导入, 原因就 ...

  10. Self-publishing 自出版

    Self-publishing 自出版 出书 传统出版 在过去的几十年中,发布意味着要经过代理商和发布者. 自出版 如今,自助出版允许作者绕过出版商和书店,直接向公众出售. refs https:// ...