POJ-1459(最大流+EK算法)
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;
}
Either Excellent or Rusty