Power Transmission
The Problem
DESA is taking a new project to transfer power. Power is generated by the newly established plant in Barisal. The main aim of this project is to transfer Power in Dhaka. As Dhaka is a megacity with almost 10 million people DESA wants to transfer maximum amount of power through the network. But as always occurs in case of power transmission it is tough to resist loss. So they want to use some regulators whose main aim are to divert power through several outlets without any loss.
Each such regulator has different capacity. It means if a regulator gets 100 unit power and it's capacity is 80 unit then remaining 20 unit power will be lost. Moreover each unidirectional link( Connectors among regulators) has a certain capacity. A link with capacity 20 unit cannot transfer power more than 20 unit. Each regulator can distribute the input power among the outgoing links so that no link capacity is overflown. DESA wants to know the maximum amount of power which can be transmitted throughout the network so that no power loss occurs. That is the job you have to do.
![](http://uva.onlinejudge.org/external/103/p10330.jpg)
( Do not try to mix the above description with the real power transmission.)
The Input
The input will start with a postive integer N ( 1<=N<=100 ) indicates the number of regulators. The next few lines contain N positive integers indicating the capacity of each regulator from 1 to N. The next line contains another positive integer M which is the number of links available among the regulators. Following M lines contain 3 positive integers ( i j C) each. 'i' and 'j' is the regulator index ( 1<=i,j<=N) and C is the capacity of the link. Power can transfer from i'th regulator to j'th regulator. The next line contains another two positive integers B and D. B is the number of regulators which are the entry point of the network. Power generated in Barisal must enter in the network through these entry points. Simmilarly D is the number of regulators connected to Dhaka. These links are special and have infinite capacity. Next line will contain B+D integers each of which is an index of regulator. The first B integers are the index of regulators connected with Barisal. Regulators connected with Barisal are not connected with Dhaka.
Input is terminated by EOF.
The Output
For each test case show the maximum amount of power which can be transferred to Dhaka from Barisal. Use a seperate line for each test case.
Sample Input
4 10 20 30 40 6 1 2 5 1 3 10 1 4 13 2 3 5 2 4 7 3 4 20 3 1 1 2 3 4 2 50 100 1 1 2 100 1 1 1 2
Sample Output
37 50
题意:给你一个图,其中有一些点是源点,一些点是汇点,求汇点的和的最大值。
思路:加上两个点,0和n+1,分别与源点和汇点相连。那么问题就变成了最基础的最大流了。用了两种写法,dfs和bfs的。
DFS代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,cap[110][110],a[110],numa,numb,ans,INF=1e9;
bool vis[110];
int dfs(int s,int t,int f)
{
int i,j,k,u,v,d;
if(s==t)
return f;
vis[s]=1;
for(v=0;v<=n+1;v++)
{
if(!vis[v] && cap[s][v]>0)
{
d=dfs(v,t,min(f,cap[s][v]));
if(d>0)
{
cap[s][v]-=d;
cap[v][s]+=d;
return d;
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int flow=0,f;
while(true)
{
memset(vis,0,sizeof(vis));
f=dfs(s,t,INF);
if(f==0)
return flow;
flow+=f;
}
}
int main()
{
int i,j,k,u,v,w;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
memset(cap,0,sizeof(cap));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
cap[u][v]+=w;
cap[u][v]=min(cap[u][v],min(a[u],a[v]));
}
scanf("%d%d",&numa,&numb);
for(i=1;i<=numa;i++)
{
scanf("%d",&u);
cap[0][u]=a[u];
}
for(i=1;i<=numb;i++)
{
scanf("%d",&u);
cap[u][n+1]=a[u];
}
ans=max_flow(0,n+1);
printf("%d\n",ans);
}
}
BFS代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,m,cap[110][110],a[110],numa,numb,ans,INF=1e9;
int F=0,mi[110],p[110],flow[110][110];
bool vis[110];
queue<int> qu;
void EK()
{
int u,v;
memset(flow,0,sizeof(flow));
memset(p,-1,sizeof(p));
F=0;
while(true)
{
memset(mi,0,sizeof(mi));
mi[0]=INF;
qu.push(0);
while(!qu.empty())
{
u=qu.front();
qu.pop();
for(v=0;v<=n+1;v++)
if(!mi[v] && cap[u][v]>flow[u][v])
{
p[v]=u;
qu.push(v);
mi[v]=min(mi[u],cap[u][v]-flow[u][v]);
}
}
if(mi[n+1]==0)
break;
for(u=n+1;u!=0;u=p[u])
{
flow[p[u]][u]+=mi[n+1];
flow[u][p[u]]-=mi[n+1];
}
F+=mi[n+1];
//printf("F %d\n",F);
}
}
int main()
{
int i,j,k,u,v,w;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
memset(cap,0,sizeof(cap));
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
cap[u][v]+=w;
cap[u][v]=min(cap[u][v],min(a[u],a[v]));
}
scanf("%d%d",&numa,&numb);
for(i=1;i<=numa;i++)
{
scanf("%d",&u);
cap[0][u]=a[u];
}
for(i=1;i<=numb;i++)
{
scanf("%d",&u);
cap[u][n+1]=a[u];
}
EK();
printf("%d\n",F);
}
}