Title Link: http://poj.org/problem?id=1459
Test instructions: There are N nodes, NP power stations, NC consumers, M power lines. Next is the information of the M-bar (U,v) Cost,cost represents the maximum flow of the edge (U,V); The information of a power station (U) cost,cost represents the maximum flow rate that the power station u can provide; the information of the B-User (V) cost,cost indicates the maximum amount of traffic per user V to accept.
Idea: Add 1 source points s and meeting point T in the diagram, connect s with each power station, and the weight of the edge is the maximum flow that the power station can provide; connecting each user to T, the weight of the edge is the maximum amount of traffic that each user can accept. This translates into a general maximum network flow problem, which is then solved.
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#include<math.h>#defineN 1100#defineINF 0x3f3f3f3ftypedefintll;using namespaceStd;ll N,np,nc,m,tt,dis[n],head[n];structnode{ll X,y,w,next;} eg[n*N];voidinit () {TT=0; memset (Head,-1,sizeof(head));}voidAddintXxintYyintww) {eg[tt].x=xx; Eg[tt].y=yy; EG[TT].W=ww; Eg[tt].next=HEAD[XX]; HEAD[XX]=tt++; eg[tt].x=yy; Eg[tt].y=xx; EG[TT].W=0; Eg[tt].next=Head[yy]; HEAD[YY]=tt++;}BOOLBFsintSinte) {memset (DIS,-1,sizeof(DIS)); Dis[s]=0; Queue<int>P; Q.push (s); while(!Q.empty ()) { intHasQ.front (); Q.pop (); for(intI=HEAD[FA]; i!=-1; I=Eg[i].next) { intv=eg[i].y; if(dis[v]==-1&&EG[I].W) {Dis[v]=dis[fa]+1; Q.push (v); } } } if(dis[e]>0) return true; return false;}intDinic (intSintmaxt) { if(s==n+1)returnMaxt; inta,sum=Maxt; for(intI=head[s]; i!=-1; I=Eg[i].next) { intv=eg[i].y; if(dis[v]==dis[s]+1&&eg[i].w>0) {a=dinic (V,min (SUM,EG[I].W)); EG[I].W-=A; Eg[i+1].w+=A; Sum-=A; } } returnmaxt-sum;}intMain () {ll xx,yy,ww; while(SCANF ("%d%d%d%d", &n,&np,&nc,&m)! =EOF) {init (); while(m--) { while(GetChar ()! ='(') ; scanf ("%d,%d)%d",&xx,&yy,&ww); Add (xx+1, yy+1, WW); } for(intI=0; i<np; i++) { while(GetChar ()! ='(') ; scanf ("%d)%d",&xx,&yy); Add (0, xx+1, yy); } for(intI=0; i<nc; i++) { while(GetChar ()! ='(') ; scanf ("%d)%d",&xx,&yy); Add (xx+1, n+1, yy); } ll ans=0; while(BFS (0, n+1) ) {ans+=dinic (0, INF); } printf ("%d\n", ans); } return 0;}
Poj1459:power Network (Dinic)