码迷,mamicode.com
首页 > 其他好文 > 详细

light oj 1155 - Power Transmission【拆点网络流】

时间:2015-11-11 13:22:54      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

1155 - Power Transmission
Time Limit: 2 second(s) Memory Limit: 32 MB

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 aims are to divert power through several outlets without any loss.

Each such regulator has different capacity. It means if a regulator gets 100 units of power and its capacity is 80 units then remaining 20 units of power will be lost. Moreover each unidirectional link (connectors among regulators) has a certain capacity. A link with capacity 20 units cannot transfer power more than 20 units. Each regulator can distribute the input power among the outgoing links so that no link capacity is over flown. 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.)

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

The input will start with a positive integer N (1 ≤ N ≤ 100) indicates the number of regulators. The next line contains N positive integers indicating the capacity of each regulator from 1 to N. All the given capacities will be positive and not greater than 1000. The next line contains another positive integer M which is the number of links available among the regulators. Each of the following M lines contains three positive integers i j C‘i‘ and ‘j‘ are the regulator index (1 ≤ i, j ≤ N, i ≠ j, 1 ≤ C ≤ 1000) and C is the capacity of the link. Power can be transferred from ith regulator to jth regulator. From a regulator i to another regulator j, there can be at most one link.

The next line contains two positive integers B and D (1 ≤ B, D and B + D ≤ N)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. Similarly 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.

Output

For each case of input, print the case number and the maximum amount of power which can be transferred from Barisal to Dhaka.

Sample Input

Output for Sample Input

2

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

Case 1: 37

Case 2: 50

 

 

题意:给n个中转站以及这些中转站的容量,再给出m条边,每条边表示两个中转站相连,并给出这天路线的容量,最后给出b个起点,和d个终点,求最大能传输多少电力

题解:建图跑一遍最大流

建图:此题要进行拆点,即将所有站点拆为左站点和右站点,原因:因为通过每个站点的流量应该是一定的,即每个站点的容量,如果不拆点,直接建图的话,由于两个站点之间也有连接,假设x站点连接y站点,如果源点到x站点的流量流满,(即不能再从x站点流出流量,)而此时由于x站点和y站点相连,所以还可以通过y站点流向x站点,拆点之后,当x站点流量流满,其左站点也流满,由于左站点之间并没有站点之间的连接,就不会出现上边说的情况

1、所有站点的左点连接右点

2、所有路线的起点的右站点连接路线终点的左站点

3、超级源点连接起始站点的左点

4、所有终站点的右点连接超级汇点

 

#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<algorithm>
#define MAX 10010
#define MAXM 100100
#define INF 0x7fffff
using namespace std;
int n,m,b,d;
struct node
{
	int from,to,cap,flow,next;
}edge[MAXM];
int dis[MAX],vis[MAX];
int cur[MAX];
int ans,head[MAX];
int station[MAX];
int yuan,hui;
void init()
{
	ans=0;
	memset(head,-1,sizeof(head));
}
void add(int u,int v,int w)
{
	edge[ans]={u,v,w,0,head[u]};
	head[u]=ans++;
	edge[ans]={v,u,0,0,head[v]};
	head[v]=ans++;
}
void getmap()
{
	int i;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&station[i]);
		add(i,i+n,station[i]); 
	}
	
	scanf("%d",&m);
	int x,y,z;
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		add(x+n,y,z);
	}
	scanf("%d%d",&b,&d);
	for(i=1;i<=b;i++)
	{
		scanf("%d",&yuan);
		add(0,yuan,station[yuan]);
	}
	for(i=1;i<=d;i++)
	{
		scanf("%d",&hui);
		add(hui+n,2*n+1,station[hui]);
	}
}

int bfs(int beg,int end)
{
    queue<int>q;
    memset(vis,0,sizeof(vis));
    memset(dis,-1,sizeof(dis));
    while(!q.empty()) q.pop();
    vis[beg]=1;
    dis[beg]=0;
    q.push(beg);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            node E=edge[i];
            if(!vis[E.to]&&E.cap>E.flow)
            {
                dis[E.to]=dis[u]+1;
                vis[E.to]=1;
                if(E.to==end) return 1;
                q.push(E.to);
            }
        }
    }
    return 0;
}
int dfs(int x,int a,int end)
{
    if(x==end||a==0)
    return a;
    int flow=0,f;
    for(int& i=cur[x];i!=-1;i=edge[i].next)
    {
        node& E=edge[i];
        if(dis[E.to]==dis[x]+1&&(f=dfs(E.to,min(a,E.cap-E.flow),end))>0)
        {
            E.flow+=f;
            edge[i^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
int maxflow(int beg,int end)
{
    int flow=0;
    while(bfs(beg,end))
    {
        memcpy(cur,head,sizeof(head));
        flow+=dfs(beg,INF,end);
    }
    return flow;
}
int main()
{
	int t,k;
	k=1;
	scanf("%d",&t);
	while(t--)
	{
		init();
	    getmap();
	    printf("Case %d: %d\n",k++,maxflow(0,2*n+1));
	}
	return 0;
}

  

light oj 1155 - Power Transmission【拆点网络流】

标签:

原文地址:http://www.cnblogs.com/tonghao/p/4955718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!