light oj 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【拆点网络流】的更多相关文章
- uva 10330 - Power Transmission(网络流)
uva 10330 - Power Transmission 题目大意:最大流问题. 解题思路:増广路算法. #include <stdio.h> #include <string. ...
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
- Jan's light oj 01--二分搜索篇
碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...
随机推荐
- BZOJ Tyvj 1729 文艺平衡树
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 ...
- iOS上绘制自然的签名-b
这里有一篇很棒的文章写如何在Android上获取流畅的签名:Smoother Signatures:https://corner.squareup.com/2012/07/smoother-signa ...
- AJax跨域请求百度音乐接口数据展示页面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [XJOI NOI02015训练题7] B 线线线 【二分】
题目链接:XJOI - NOI2015-07 - B 题目分析 题意:过一个点 P 的所有直线,与点集 Q 的最小距离是多少?一条直线与点集的距离定义为点集中每个点与直线距离的最大值. 题解:二分答案 ...
- js amd
http://www.ruanyifeng.com/blog/2012/10/asynchronous_module_definition.html http://www.adequatelygood ...
- 转一贴,今天实在写累了,也看累了--【Python异步非阻塞IO多路复用Select/Poll/Epoll使用】
下面这篇,原理理解了, 再结合 这一周来的心得体会,整个框架就差不多了... http://www.haiyun.me/archives/1056.html 有许多封装好的异步非阻塞IO多路复用框架, ...
- [jobdu]把数组排成最小的数
这道题见过,就是把相加的结果作为比较来排序就行了.注意的是comp函数里面要用const引用.而且c++里的字符串直接操作(读入和相加)也很方便. #include <iostream> ...
- WCF - Architecture
WCF - Architecture WCF has a layered architecture that offers ample support for developing various d ...
- GL_INTERFACE
prompt ****************************************************************************** 总账接口主要完成其他模块的总 ...
- POI导出数据内存溢出问题
POI之前的版本不支持大数据量处理,如果数据过多则经常报OOM错误,有时候调整JVM大小效果也不是太好.3.8版本的POI新出来了SXSSFWorkbook,可以支持大数据量的操作,只是SXSSFWo ...