http://115.28.76.232/contest?cid=1125#overview
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear reactor to produce plutonium for the nuclear bomb they are planning to create. Being the wicked computer genius of this group, you are responsible for developing the cooling system for the reactor.
The cooling system of the reactor consists of the number of pipes that special cooling liquid flows by. Pipes are connected at special points, called nodes, each pipe has the starting node and the end point. The liquid must flow by the pipe from its start point to its end point and not in the opposite direction.
Let the nodes be numbered from 1 to N . The cooling system must be designed so that the liquid is circulating by the pipes and the amount of the liquid coming to each node (in the unit of time) is equal to the amount of liquid leaving the node. That is, if we designate the amount of liquid going by the pipe from i-th node to j -th as fi,j, (put fi,j = 0 if there is no pipe from node i to node j ), for each i the following condition must hold:
Each pipe has some finite capacity, therefore for each i and j connected by the pipe must be fi,j ≤ ci,j where ci,j is the capacity of the pipe. To provide sufficient cooling, the amount of the liquid flowing by the pipe going from i-th to j -th nodes must be at least li,j , thus it must be fi,j ≥ li,j. Given ci,j and li,j for all pipes, find the amount fi,j, satisfying the conditions specified above
4 6 1 2 1 2 2 3 1 2 3 4 1 2 4 1 1 2 1 3 1 2 4 2 1 2 4 6 1 2 1 3 2 3 1 3 3 4 1 3 4 1 1 3 1 3 1 3 4 2 1 3
NO YES 1 2 3 2 1 1
注:因为还是满流,每条边满足的流量在残留网络的反向边中。
#include<cstdio> #include<iostream> #include<string.h> #include<vector> using namespace std; const int oo=1e9; const int mm=111111; const int mn=999; int node,src,dest,edge; int ver[mm],flow[mm],next[mm]; int head[mn],work[mn],dis[mn],q[mn]; void prepare(int _node,int _src,int _dest) { node=_node,src=_src,dest=_dest; for(int i=0; i<node; ++i)head[i]=-1; edge=0; } void addedge(int u,int v,int c) { ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } bool Dinic_bfs() { int i,u,v,l,r=0; for(i=0; i<node; ++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=next[i]) if(flow[i]&&dis[v=ver[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0; } int Dinic_dfs(int u,int exp) { if(u==dest) return exp; for(int &i=work[u],v,tmp; i>=0; i=next[i]) if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0; } int Dinic_flow() { int i,ret=0,delta; while(Dinic_bfs()) { for(i=0; i<node; ++i)work[i]=head[i]; while(delta=Dinic_dfs(src,oo))ret+=delta; } return ret; } int n,m; vector <int>_edge,_low; int main() { while(~scanf("%d%d",&n,&m)) { int flow2=0; prepare(n+2,0,n+1); _edge.clear(); _low.clear(); for(int i=0; i<m; i++) { int u,v,low,high; scanf("%d%d%d%d",&u,&v,&low,&high); flow2+=low; addedge(0,v,low); addedge(u,n+1,low); _edge.push_back(edge); _low.push_back(low); addedge(u,v,high-low); } int flow1=Dinic_flow(); if(flow1!=flow2) { printf("NO\n"); continue; } printf("YES\n"); int _size=_edge.size(); for(int i=0; i<_size; i++) printf("%d\n",flow[_edge[i]^1]+_low[i]); } return 0; }