Description
A power network consists of nodes (power stations, consumers and dispatchers) connected by power transport lines. A node u may be supplied with an amount s(u) >= 0 of power, may produce an amount 0 <= p(u) <= pmax(u) of power, may consume an amount 0 <= c(u) <= min(s(u),cmax(u)) of power, and may deliver an amount d(u)=s(u)+p(u)-c(u) of power. The following restrictions apply: c(u)=0 for any power station, p(u)=0 for any consumer, and p(u)=c(u)=0 for any dispatcher. There is at most one power transport line (u,v) from a node u to a node v in the net; it transports an amount 0 <= l(u,v) <= lmax(u,v) of power delivered by u to v. Let Con=Σuc(u) be the power consumed in the net. The problem is to compute the maximum value of Con.
An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.
An example is in figure 1. The label x/y of power station u shows that p(u)=x and pmax(u)=y. The label x/y of consumer u shows that c(u)=x and cmax(u)=y. The label x/y of power transport line (u,v) shows that l(u,v)=x and lmax(u,v)=y. The power consumed is Con=6. Notice that there are other possible states of the network but the value of Con cannot exceed 6.
Input
There are several data sets in the input. Each data set encodes a power network. It starts with four integers: 0 <= n <= 100 (nodes), 0 <= np <= n (power stations), 0 <= nc <= n (consumers), and 0 <= m <= n^2 (power transport lines). Follow m data triplets (u,v)z, where u and v are node identifiers (starting from 0) and 0 <= z <= 1000 is the value of lmax(u,v). Follow np doublets (u)z, where u is the identifier of a power station and 0 <= z <= 10000 is the value of pmax(u). The data set ends with nc doublets (u)z, where u is the identifier of a consumer and 0 <= z <= 10000 is the value of cmax(u). All input numbers are integers. Except the (u,v)z triplets and the (u)z doublets, which do not contain white spaces, white spaces can occur freely in input. Input data terminate with an end of file and are correct.
Output
For each data set from the input, the program prints on the standard output the maximum amount of power that can be consumed in the corresponding network. Each result has an integral value and is printed from the beginning of a separate line.
Sample Input
2 1 1 2 (0,1)20 (1,0)10 (0)15 (1)20 7 2 3 13 (0,0)1 (0,1)2 (0,2)5 (1,0)1 (1,2)8 (2,3)1 (2,4)7 (3,5)2 (3,6)5 (4,2)7 (4,3)5 (4,5)1 (6,0)5 (0)5 (1)2 (3)2 (4)1 (5)4
Sample Output
15 6
这题读起来烦了点,做还是蛮好做的,建一个超级源点,一个超级汇点。建出图之后就是个裸最大流了- -。。
用E_K做用了620+
EK
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int INF=(1<<30)-1; 8 const int N=110; 9 int cap[N][N],flow[N]; 10 int pre[N]; 11 queue<int>q; 12 int minn(int a,int b) 13 { 14 return a<b?a:b; 15 } 16 int bfs(int s,int t) 17 { 18 int i; 19 memset(pre,-1,sizeof(pre)); 20 while(!q.empty()) 21 q.pop(); 22 pre[s]=0; 23 flow[s]=INF; 24 q.push(s); 25 while(!q.empty()) 26 { 27 int u=q.front(); 28 q.pop(); 29 if(u==t) break; 30 for(i=0;i<=t&&pre[t]==-1;i++) 31 { 32 if(cap[u][i]>0&&pre[i]==-1) 33 { 34 pre[i]=u; 35 q.push(i); 36 flow[i]=minn(cap[u][i],flow[u]); 37 } 38 } 39 } 40 if(pre[t]==-1||t==s) 41 return -1; 42 else 43 return flow[t]; 44 } 45 int E_K(int s,int t) 46 { 47 int i,sum=0,increase=0; 48 while((increase=bfs(s,t))!=-1) 49 { 50 int u; 51 for(u=t;u!=s;u=pre[u]) 52 { 53 cap[pre[u]][u]-=increase; 54 cap[u][pre[u]]+=increase; 55 } 56 sum+=increase; 57 } 58 return sum; 59 } 60 int main() 61 { 62 int n,np,nc,m; 63 int u,v,w,i; 64 while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF) 65 { 66 memset(cap,0,sizeof(cap)); 67 for(i=0;i<m;i++) 68 { 69 scanf(" (%d,%d)%d",&u,&v,&w); 70 u++,v++; 71 cap[u][v]+=w; 72 } 73 for(i=0;i<np;i++) 74 { 75 scanf(" (%d)%d",&u,&w); 76 u++; 77 cap[0][u]+=w; 78 } 79 for(i=0;i<nc;i++) 80 { 81 scanf(" (%d)%d",&u,&w); 82 u++; 83 cap[u][n+1]+=w; 84 } 85 printf("%d\n",E_K(0,n+1)); 86 } 87 return 0; 88 }
后来学了一下dinic,用了94。。。=。=
dinic
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 const int INF=1<<29; 8 const int N=110; 9 struct eg 10 { 11 int to; 12 int w; 13 int next; 14 }p[50000]; 15 int head[N],dis[N],q[N],work[N]; 16 int cnt,n; 17 int minn(int a,int b) 18 { 19 return a<b?a:b; 20 } 21 void addedge(int u,int v,int w) 22 { 23 p[cnt].to=v,p[cnt].w=w,p[cnt].next=head[u],head[u]=cnt++; 24 p[cnt].to=u,p[cnt].w=0,p[cnt].next=head[v],head[v]=cnt++; 25 } 26 bool bfs(int s,int t) 27 { 28 int i,v,l,r=0; 29 for(i=0;i<=n+2;i++) 30 dis[i]=-1; 31 dis[s]=0; 32 q[r++]=s; 33 for(l=0;l<r;l++) 34 { 35 int u=q[l]; 36 if(u==t) return 1; 37 for(i=head[u];i!=-1;i=p[i].next) 38 { 39 if(p[i].w>0&&dis[v=p[i].to]==-1) 40 { 41 dis[v]=dis[u]+1; 42 q[r++]=v; 43 if(v==t) return 1; 44 } 45 } 46 } 47 return dis[t]!=-1; 48 } 49 int dfs(int u,int t,int flow) 50 { 51 if(u==t) return flow; 52 int sum=flow; 53 int i,v,a; 54 for(i=work[u];i!=-1&&flow;i=p[i].next) 55 { 56 if(p[i].w>0&&dis[v=p[i].to]==dis[u]+1) 57 { 58 a=dfs(v,t,minn(flow,p[i].w)); 59 if(!a) dis[v]=-1; 60 else 61 { 62 p[i].w-=a; 63 p[i^1].w+=a; 64 flow-=a; 65 } 66 } 67 } 68 return sum-flow; 69 } 70 int dinic(int s,int t) 71 { 72 int sum=0,i,increase=0; 73 while(bfs(s,t)) 74 { 75 for(i=0;i<=n+2;i++) work[i]=head[i]; 76 while((increase=dfs(s,t,INF))) 77 sum+=increase; 78 } 79 return sum; 80 } 81 void init() 82 { 83 int i; 84 for(i=0;i<=n+2;i++) 85 head[i]=-1; 86 cnt=0; 87 } 88 int main() 89 { 90 int np,nc,m; 91 int u,v,w,i; 92 while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF) 93 { 94 init(); 95 for(i=0;i<m;i++) 96 { 97 scanf(" (%d,%d)%d",&u,&v,&w); 98 addedge(++u,++v,w); 99 } 100 for(i=0;i<np;i++) 101 { 102 scanf(" (%d)%d",&u,&w); 103 addedge(0,++u,w); 104 } 105 for(i=0;i<nc;i++) 106 { 107 scanf(" (%d)%d",&u,&w); 108 addedge(++u,n+1,w); 109 } 110 printf("%d\n",dinic(0,n+1)); 111 } 112 return 0; 113 }