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

1155 - Power Transmission

  PDF (English) Statistics Forum
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;
}

  

时间: 2024-10-29 19:11:08

light oj 1155 - Power Transmission【拆点网络流】的相关文章

uva 10330 Power Transmission (最大流 + 拆点)

uva 10330 Power Transmission 如果对最大流不熟悉的话可以先去看看这个 题目大意:先来讲解一下INPUT.首先读入一个正整数N, 接下来N个数据是N个调节器的容量:然后读入一个正整数M, 接下来M组数据代表的是M条调节器与调解器之间的线路(调节器u, 调节器v, 容量);最后的一组数据先是两个正整数a和b, 接下来的a个数据代表的是初始的调节器,最后的b个数据代表的是终结的调节器. 综上,求最大流. 解题思路:源点或汇点不唯一的时候,记得要增加超级源点或超级汇点来使得源

UVA 10330 Power Transmission(网络最大流)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1271  Power Transmission  The Problem DESA is taking a new project to transfer power. Power is generated by the newly established plant in Barisal.

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

Light OJ 1411 Rip Van Winkle`s Code 线段树成段更新

题目来源:Light OJ 1411 Rip Van Winkle`s Code 题意:3中操作 1种查询 求区间和 其中每次可以把一段区间从左到右加上1,2,3,...或者从右到左加上...3,2,1 或者把某个区间的数都置为v 思路:我是加了6个域 add是这段区间每个数都要加上add  add是这么来的 对与123456...这个等差数列 可能要分为2个区间 那么我就分成123和123 两个右边的等差数列每个数还应该加上3 所以右区间add加3 v是这个区间都要置为v 他的优先级最高 b是

Light OJ 1168 Wishing Snake 强连通缩点+哈密顿通路

题目来源:Light OJ 1168 Wishing Snake 题意:有点难看懂题意 看了一个小时再加别人的代码才懂意思 从0开始 输入的那些每一对u v 都要经过 就是从0到到达那些点 思路:首先缩点 每一个强连通分量里面的点都是可达的 缩点后的图是有向无环图 如果从0这个强连通分量可以出去到2个强连通分量 那么这两个强连通分量是不可能相互可达 所以可行的方案就是所有的强连通分量连成一线 一个一个串起来 除了第一个 出度是1入度是0和最后一个出度是0入度是1 其他都是入度等于出度是1 特判只

Jan&#39;s light oj 01--二分搜索篇

碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计数问题,利用二分直接枚举可能的结果,再检查是否符合题目要求. 4.区间求解,即利用两次二分分别查找有序序列左右上下限,再求差算出总个数. 题型知识补充: 1. 三分的一般写法: 1 double thfind(double left,double right) 2 { 3 double midmid

light oj 1422 - Halloween Costumes (区间dp)

1422 - Halloween Costumes PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is planning to attend as many parties as he can. Since it's Ha

Light OJ 1341 Aladdin and the Flying Carpet

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a magical cave, led by the evil sorcerer who disguised hi