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.不好直接求解的一类计 ...
随机推荐
- Memcached(四)Memcached的CAS协议
1. 什么是CAS协议很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS是 ...
- python 常用模块(转载)
转载地址:http://codeweblog.com/python-%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97/ adodb:我们领导推荐的数据库连接组件bsddb3:B ...
- JAVA自学之-----FileInputStream类
1, FileInputStream类函数创建: package coreJava; import java.io.FileInputStream; import java.io.IOExceptio ...
- POJ 1961 Period(KMP)
http://poj.org/problem?id=1961 题意 :给你一个字符串,让你输出到第几个字符时,循环结的个数. 思路 :这个题和2409差不多,稍微修改一下,加一个循环就行了,用的也是K ...
- 李洪强iOS开发之-环信03_集成 SDK 基础功能
李洪强iOS开发之-环信03_集成 SDK 基础功能 集成 SDK 基础功能 在您阅读此文档时,我们假定您已经具备了基础的 iOS 应用开发经验,并能够理解相关基础概念. SDK 同步/异步方法区分 ...
- MS提供的虚拟机IE测试
https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/
- Android SurfaceView实现全屏播放例子
public class Mymedia extends Activity implements OnBufferingUpdateListener, OnCompletionListener, Me ...
- Bamboo简介
前言 前面介绍了JIRA管理平台,那么本篇就来介绍关于自动编译项目的工具-Bamboo. 正题 1.简介 Atlassian Bamboo 是一款持续集成构建服务器软件(Build Serve ...
- [OpenJudge] 平方和
平方和 总时间限制: 3000ms 内存限制: 65536kB 描述 给出n(1<=n<=500000)个数字,下标从1开始 执行m(1<=m<=500000)次操作,操作可分 ...
- uiview scale
http://stackoverflow.com/questions/3946797/cgaffinetransformmakescale-makes-uiview-jump-to-original- ...