1155-power Transmission
|
PDF (中文版) |
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 was to transfer Power in Dhaka. As Dhaka is a megacity with almost million people DESA wants to transfer maximum amount of power through the network. But as always the occurs in case of power transmission it's tough to resist loss. So they want to use some regulators whose main aims is to divert power through several outlets without any loss.
Each such regulator have different capacity. It means if a regulator gets units of power and its capacity are units then remaining units of power would be lost . Moreover each unidirectional link (connectors among regulators) has a certain capacity. A link with capacity-units cannot transfer power more than. Each regulator can distribute the input power among the outgoing links so, no link capacity is over flown. DESA wants to know the maximum amount of power which can is transmitted throughout the network so, no power loss occur S. That is the job of the.
(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 would start with a positive integerN (1≤n≤100)Indicates the number of regulators. The next line containsNPositive integers indicating the capacity of each regulator from1ToN. All the given capacities is positive and not greater than +. The next line contains another positive integerMWhich is the number of links available among the regulators. Each of the followingMLines contains three positive integersI J C.' I 'and' J 'is the regulator index(1≤i, J≤n, I≠j, 1≤c≤1000)andCIs the capacity of the link. Power can be transferred fromithRegulator tojthRegulator. From a regulatorIto another regulatorJ, there can is at the most one link.
The next line contains the positive integers b and D (1≤b, D and B + d≤n). B is the number of regulators which was 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 is special and has infinite capacity. Next line would contain b+d integers each of the which are an index of regulator. The first B integers is the index of regulators connected with Barisal. Regulators connected with Barisal is not connected with Dhaka.
Output
For each case of input, print the case number and the maximum amount of power which can is transferred from Barisal to Dha Ka.
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 |
Test instructions: To n a transit station and the capacity of these transfer stations, and then give the M-edge, each side represents two transit stations connected, and give the capacity of this day, and finally give the B starting point, and the D end point, the maximum can transmit how much power
Puzzle: Build a chart run over the maximum flow
Map: This topic to be split, will be all sites are split to the left site and right site, reason: because the traffic through each site should be certain, that is, the capacity of each site, if not split, directly build the diagram, because there are two sites also connected, assuming X site connection y site, if the source point to X site traffic flow full, (That is, can no longer flow from the X site,) and at this time due to the X site and y site connected, so you can also flow through the Y site X site, after the split, when the X site traffic flow full, the left site is full, because the left site and there is no connection between sites, it will not appear above said situation
1, the left point of all sites connected to the right point
2. Left site of the right site connection route endpoint for the start of all routes
3, the super source point to connect the start site's left point
4, the right point of all terminal sites connected to the Super meeting point
#include <stdio.h> #include <string.h> #include <queue> #include <stack> #include <algorithm > #define MAX 10010#define maxm 100100#define INF 0x7fffffusing namespace Std;int n,m,b,d;struct node{int from,to,cap,f Low,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 "split network Flow"