Time Limit: 2000MS | Memory Limit: 32768K | |
Total Submissions: 23459 | Accepted: 12284 |
Description
An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.
Input
Output
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
Sample Output
15 6
Hint
Source
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define N 110
#define inf 0x3f3f3f3f
using namespace std;
struct Edge
{
int from,to,cap,flow;
};
struct Dinic
{
int n,m,s,t;
vector<Edge>edges;
vector<int> G[N];
bool vis[N];
int d[N];
int cur[N];
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int>Q;
Q.push(s);
d[s]=0;
vis[s]=1;
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(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==0)
return a;
int flow=0,f;
for(int &i=cur[x]; i<G[x].size(); i++)
{
Edge &e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a)
break;
}
}
return flow;
}
int Maxflow(int s,int t)
{
this->s=s,this->t=t;
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,inf);
}
return flow;
}
void add_edge(int from,int to,int cap)
{
edges.push_back((Edge)
{
from,to,cap,0
});
edges.push_back((Edge)
{
to,from,0,0
});
int m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
};
int main()
{
int n,np,nc,l,a,b,w,i;
char c[10];
while(~scanf("%d%d%d%d",&n,&np,&nc,&l))
{
Dinic D;
D.s=n+1,D.t=n+2;
for(i=1; i<=l; i++)
{
getchar();
scanf("%1s%d,%d%1s%d",c,&a,&b,c,&w);
D.add_edge(a,b,w);
}
for(i=0; i<np; i++)
{
getchar();
scanf("%1s%d%1s%d",c,&a,c,&b);
D.add_edge(D.s,a,b);
}
for(i=0; i<nc; i++)
{
getchar();
scanf("%1s%d%1s%d",c,&a,c,&b);
D.add_edge(a,D.t,b);
}
int flow=D.Maxflow(D.s,D.t);
printf("%d\n",flow);
}
return 0;
}