Time Limit: 2000MS | Memory Limit: 32768K | |
Total Submissions: 27572 | Accepted: 14333 |
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
網上找的,第一次做最大流,還不是太懂
題目大意是:有n個點,其中nc個點是發電站,有np個點是用戶,其他的是中轉站,求最大流。
問題中存在着多源點和多匯點,我們可以把這些源點和匯點當作中轉站,在虛擬一個超級源點和超級匯點
//不用cin,cout,queue 還是挺快的282MS
//EK
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
#define inf 0x3f3f3f3f
int mp[150][150],v[150],pre[150];
int bfs(int s,int t)
{
memset(pre,-1,sizeof(pre));
memset(v,0,sizeof(v));
int q[11000];
int in=0,out=0;
q[in++]=s;
v[s]=1;
pre[s]=s;
while(in>out)
{
int x=q[out++];
for(int i=1;i<=t;i++)
{
if(!v[i] && mp[x][i]>0)
{
pre[i]=x;
v[i]=1;
if(i==t)return 1;
q[in++]=i;
}
}
}
return 0;
}
int EK(int s,int t)
{
int flow=0,d;
while(bfs(s,t))
{
d=inf;
for(int i=t;i!=s;i=pre[i])
{
if(d>mp[pre[i]][i]) //pre[4]=2;
d=mp[pre[i]][i];
}
flow+=d;
for(int i=t;i!=s;i=pre[i])
{
mp[pre[i]][i]-=d;
mp[i][pre[i]]+=d;
}
}
return flow;
}
int main()
{
int n,m,i,j,np,nc;
int u,v,w;
int x,y,z;
while(~scanf("%d%d%d%d",&n,&np,&nc,&m))
{
memset(mp,0,sizeof(mp));
while(m--)
{
scanf(" (%d,%d)%d",&u,&v,&w);
mp[u+2][v+2]+=w;
}
while(np--)
{
scanf(" (%d)%d",&v,&w);
mp[1][v+2]+=w;
}
while(nc--)
{
scanf(" (%d)%d",&u,&w);
mp[u+2][n+2]+=w;
}
printf("%d\n",EK(1,n+2));
}
return 0;
}