首页 > 代码库 > UVA 10330 Power Transmission(网络最大流)
UVA 10330 Power Transmission(网络最大流)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1271
Power Transmission
Power Transmission |
The Problem
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 aim are to divert power through several outlets without any loss.
Each such regulator has different capacity. It means if a regulator gets 100 unit power and it‘s capacity is 80 unit then remaining 20 unit power will be lost. Moreover each unidirectional link( Connectors among regulators) has a certain capacity. A link with capacity 20 unit cannot transfer power more than 20 unit. Each regulator can distribute the input power among the outgoing links so that no link capacity is overflown. 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.)
The Input
The input will start with a postive integer N ( 1<=N<=100 ) indicates the number of regulators. The next few lines contain N positive integers indicating the capacity of each regulator from 1 to N. The next line contains another positive integer M which is the number of links available among the regulators. Following M lines contain 3 positive integers ( i j C) each. ‘i‘ and ‘j‘ is the regulator index ( 1<=i,j<=N) and C is the capacity of the link. Power can transfer from i‘th regulator to j‘th regulator. The next line contains another two positive integers B and D. 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. Simmilarly 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.
Input is terminated by EOF.
The Output
For each test case show the maximum amount of power which can be transferred to Dhaka from Barisal. Use a seperate line for each test case.
Sample Input
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
Sample Output
37 50
Md. Kamruzzaman
题意:
电网中有n个变压器,每个变压器有容量限制,有m条电缆(有向),每条电缆也有容量限制;b个变压器和发电厂相连,d个和用户相连,求电网中最大输电流。
分析:
这是个顶点上也有限制的网络最大流,处理方法是将这个点拆成两个点,这两个点之间连一条边(注意方向:入—>出),边的容量是原来点的容量。发电站是源点,与b个变压器连容量是无穷大的边;用户是汇点,d个变压器和用户连容量是无穷大的边。然后用Dinic算法愉快地处理。
#include<cstdio> #include<iostream> #include<cstdlib> #include<algorithm> #include<ctime> #include<cctype> #include<cmath> #include<string> #include<cstring> #include<stack> #include<queue> #include<list> #include<vector> #include<map> #include<set> #define sqr(x) ((x)*(x)) #define LL long long #define itn int #define INF 0x3f3f3f3f #define PI 3.1415926535897932384626 #define eps 1e-10 #define maxm 50007 #define maxn 207 using namespace std; int fir[maxn]; int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm]; int e_max; int q[maxn],lv[maxn],iter[maxn]; void add_edge(int _u,int _v,int _w) { int e; e=e_max++; u[e]=_u;v[e]=_v;cap[e]=_w; nex[e]=fir[u[e]];fir[u[e]]=e; e=e_max++; u[e]=_v;v[e]=_u;cap[e]=0; nex[e]=fir[u[e]];fir[u[e]]=e; } void dinic_bfs(int s) { int f,r; memset(lv,-1,sizeof lv); q[f=r=0]=s; lv[s]=0; while (f<=r) { int x=q[f++]; for (int e=fir[x];~e;e=nex[e]) { if (lv[v[e]]<0 && cap[e]>flow[e]) { lv[v[e]]=lv[u[e]]+1; q[++r]=v[e]; } } } } int dinic_dfs(int _u,int t,int _f) { if (_u==t) return _f; for (int &e=iter[_u];~e;e=nex[e]) { if (cap[e]>flow[e] && lv[_u]<lv[v[e]]) { int _d=dinic_dfs(v[e],t,min(_f,cap[e]-flow[e])); if (_d>0) { flow[e]+=_d; flow[e^1]-=_d; return _d; } } } return 0; } int max_flow(int s,int t) { memset(flow,0,sizeof flow); int total_flow=0; for (;;) { dinic_bfs(s); if (lv[t]<0) return total_flow; memcpy(iter,fir,sizeof fir); int _f; while ((_f=dinic_dfs(s,t,INF))>0) total_flow+=_f; } return total_flow; } int main() { #ifndef ONLINE_JUDGE freopen("/home/fcbruce/文档/code/t","r",stdin); #endif // ONLINE_JUDGE int n,m,b,d,_u,_v,_w; while (~scanf("%d",&n)) { memset(fir,-1,sizeof fir); e_max=0; for (int i=1;i<=n;i++) { scanf("%d",&_w); add_edge(i,i+n,_w); } scanf("%d",&m); for (int i=0;i<m;i++) { scanf("%d %d %d",&_u,&_v,&_w); add_edge(_u+n,_v,_w); } scanf("%d %d",&b,&d); int s=0,t=n<<1|1; for (int i=0;i<b;i++) { scanf("%d",&_u); add_edge(s,_u,INF); } for (int i=0;i<d;i++) { scanf("%d",&_u); add_edge(_u+n,t,INF); } printf("%d\n",max_flow(s,t)); } return 0; }