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【拆点网络流】的更多相关文章

  1. uva 10330 - Power Transmission(网络流)

    uva 10330 - Power Transmission 题目大意:最大流问题. 解题思路:増广路算法. #include <stdio.h> #include <string. ...

  2. Light OJ 1114 Easily Readable 字典树

    题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...

  3. Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖

    题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...

  4. Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖

    标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...

  5. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  6. light oj 1007 Mathematically Hard (欧拉函数)

    题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...

  7. Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖

    题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...

  8. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  9. Jan's light oj 01--二分搜索篇

    碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...

随机推荐

  1. SVN服务器使用(一)

    源代码版本控制软件很多,像VSS,SVN还有其他的软件,各有优缺点.Subversion是优秀的版本控制工具,下面主要介绍这个软件的使用. Subversion下载地址: http://subvers ...

  2. 安全测试常见的10个问题 ZT

    1, 问题:没有被验证的输入     测试方法:     数据类型(字符串,整型,实数,等)    允许的字符集    最小和最大的长度    是否允许空输入    参数是否是必须的    重复是否允 ...

  3. [HDOJ - 5282] Senior's String 【DP】

    题目链接:BZOJ - 5282 题目分析 LCS 就是用经典的 O(n^2) DP 解决,f[i][j] 表示 x 串前 i 个字符与 y 串前 j 个字符的 LCS 长度. f[i][j] = m ...

  4. [CC150] Get all permutations of a string

    Problem: Compute all permutations of a string of unique characters. 此题用循环的方式不好做,下面是一种递归的思路: 把给的字符串看成 ...

  5. Wp8开发环境搭建总结

    原地址:http://blog.csdn.net/lixing732100721/article/details/8564985 注意:技术发展太快  此文章年代已久  请大家酌情参照 系统要求(来自 ...

  6. MySQL JDBC的setFetchSize

    MySQL JDBC的setFetchSize http://uuhorse.iteye.com/blog/2163582 http://blog.sina.com.cn/s/blog_6706203 ...

  7. Android 多屏幕适配

    问题: 测试时,发现应用在不同的显示器上显示效果不同(部分文本不能显示完全),自然想到屏幕适配的问题. 按照思路整理如下: (一) 几个概念 1, Screen size 屏幕的尺寸,即对角线长度(单 ...

  8. RPGJS 进阶分析之 如何使用RMXP导出的数据

    首先启动RMXP 在主脚本编辑区粘贴官方提供的导出数据脚本. 之后启动并进入游戏之后按F8键就可以调出导出Map和Animation的菜单 导出后的数据保存在工程目录下的RpgJs目录下. Datab ...

  9. [NYOJ 37] 回文字符串

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当 ...

  10. (转载) mysql中,option是保留字段么?

    (转载)http://book.77169.org/101/50364.htm update thread set active=0,option='lock',manager='书生' where ...