uva 10330 Power Transmission (网络流)



 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.

( 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

Md. Kamruzzaman




一题裸的网络流,已知了起点与汇点,还有线路容量,求最大流量,唯一不同的是要拆点,因为点有容量,所以必须拆成一条线。


代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

const int maxn=300;
const int inf=1<<30;
struct edge{
	int u,v,next,f;
	edge(int u0=0,int v0=0,int f0=0,int next0=0){
		u=u0,v=v0,f=f0,next=next0;
	}
}e[maxn*maxn];
int head[maxn*2],visited[maxn*2],path[maxn*2];
int cnt,from,to,marked,n;

void initial(){
	cnt=0;
	marked=1;
	memset(head,-1,sizeof(head));
	memset(visited,0,sizeof(visited));
}

void adde(int u,int v,int f){
	e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++;
	e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++;
}

void input(){
    from=0;
    to=2*n+1;
    int u,v,f,m,ns,nd;
    for(int i=1;i<=n;i++){
        scanf("%d",&f);
        adde(i,i+n,f);
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&u,&v,&f);
        adde(u+n,v,f);
    }
    scanf("%d%d",&ns,&nd);
    for(int i=0;i<ns;i++){
        scanf("%d",&v);
        adde(from,v,inf);
    }
    for(int i=0;i<nd;i++){
        scanf("%d",&u);
        adde(u+n,to,inf);
    }
}

bool bfs(){
    int s=from,d;
    queue <int> q;
    q.push(s);
    marked++;
    visited[s]=marked;
    while(!q.empty()){
        s=q.front();
        q.pop();
        for(int i=head[s];i!=-1;i=e[i].next){
            d=e[i].v;
            if(visited[d]!=marked && e[i].f>0){
                visited[d]=marked;
                path[d]=i;
                q.push(d);
                if(d==to) return true;
            }
        }
    }
    return false;
}

int maxf(){
    int maxflow=0;
    while(bfs() ){
        int offflow=inf;
     	for(int i=to;i!=from;i=e[path[i]].u){
     		offflow=min(e[path[i]].f,offflow);
        }
        for(int i=to;i!=from;i=e[path[i]].u){
     		e[path[i]].f-=offflow;
         	e[path[i]^1].f+=offflow;
        }
        maxflow+=offflow;
     }
     return maxflow;
}

void solve(){
    cout<<maxf()<<endl;
}

int main(){
	while(scanf("%d",&n)!=EOF){
		initial();
		input();
		solve();
	}
	return 0;
}



posted @ 2014-04-15 22:14  炒饭君  阅读(231)  评论(0编辑  收藏  举报