Topic Link: http://poj.org/problem?id=1459
A power grid contains some nodes (power stations, consumers, dispatch stations), which are connected by wires. Each node u may be supplied with the electric energy of S (U), S (U) ≥0, and may also generate electricity from P (u). 0≤p (U) ≤pmax (u); site u may also consume C (U) power, 0≤c (U) ≤min (S (u), Cmax (U)); May transmit D (U) Power, D ( u) = S (u) + P (U)-C (U). These quantities have the following limiting relationships: For each power station, c (u) = 0; For each consumer, p (u) = 0; For each dispatch station, p (u) = c (U) = 0.
There is at most one wire connection between the two nodes U and V in the power grid. From node u to node v transmission L (u,v) of electrical Energy, 0≤l (u,v) ≤lmax (u,v). Define con as the sum of C (U) that represents the total consumption of electricity in the grid. The aim of the subject is to find the maximum value of con.
<-is excerpted from the theory, implementation and application of graph theory algorithm->
Exercises
Add a super source point and a super sink point, link a pmax arc between the source and each power station, and connect a cmax arc between the consumption and the meeting point. For the ternary group (U,V) z given in the topic, the arc from Vertex u with a capacity of Z to Vertex v. And then ask for the maximum flow.
Dinic algorithm, the point too much can not save the adjacency matrix, using an array to save the edge, and then with the current ARC optimization, I took this optimization to remove the timeout.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Queue> #include <vector> using namespace std;
const int MAXN=100+10;
const int maxm=maxn*maxn*2;
const int inf=0x3f3f3f3f;
struct ARC {int u,v,cap;
Arc () {};
Arc (int u,int v,int cap): U (U), V (v), Cap (CAP) {};
}; ARC edge[maxm];//Storage Edge Information vector<int> pos[maxn];//Store adjacent edges of each point in the edge array int level[maxn];//hierarchical network int current[maxn];/
/current arc int cnt,n,s,e;
void Addedge (int u,int v,int cap) {pos[u].push_back (CNT);
Edge[cnt++]=arc (U,V,CAP);
Pos[v].push_back (CNT);
Edge[cnt++]=arc (v,u,0);
int BFS () {queue<int> que;
Que.push (S);
Memset (level) (level,-1,sizeof);
level[s]=0;
while (!que.empty ()) {int U=que.front ();
Que.pop ();
for (int v=0;v<pos[u].size (); v++) {Arc &e=Edge[pos[u][v]];
if (e.cap>0&&level[e.v]<0)//has a residual and unallocated hierarchy {level[e.v]=level[u]+1;
Que.push (E.V);
}} if (level[e]>0) return 1;
return 0; } int DFS(int x,int Low)
{if (x==e) return is low;
for (int i=current[x];i<pos[x].size (); i++) {current[x]=i;//Current arc arc &e=edge[pos[x][i]];//references int a=0;
if (level[e.v]==level[x]+1&&e.cap>0&& (A=dfs (E.v,min (LOW,E.CAP))) {e.cap-=a;
Edge[pos[x][i]^1].cap+=a;
return A;
} return 0;
int dinic () {int ans=0;
while (BFS ()) {memset (current,0,sizeof (current));
int tans;
while (Tans=dfs (S,inf)) Ans+=tans;
return ans;
int main () {//freopen ("In.txt", "R", stdin);
Freopen ("OUT.txt", "w", stdout);
int n,np,nc,m; while (scanf ("%d%d%d%d", &n,&np,&nc,&m)!=eof) {n=n+2; S=n-2;
e=n-1;
cnt=0;
for (int i=0;i<n;i++) {pos[i].clear ();
for (int i=0;i<n;i++) {pos[i].clear ();
for (int i=0;i<m;i++) {int u,v,z;
scanf ("(%d,%d)%d", &u,&v,&z);
Addedge (U,V,Z);
for (int i=0;i<np;i++) {int u,z;
scanf ("(%d)%d", &u,&z);
Addedge (S,U,Z); for (int i=0;i<nc;i++) {int u,z;
scanf ("(%d)%d", &u,&z);
Addedge (U,E,Z); }/* for (int i=0;i<cnt;i++) {cout<<edge[i].u<< "" <<Edge[i].v<< "" <<Edge[i].cap<
<endl;
} * * Cout<<dinic () <<endl;
return 0; }