loj 1155(最大流)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26869
思路:题目还是比较水的,由于点也有容量,则必须拆点,然后跑最大流。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 #define MAXN 222 8 #define MAXM 222222 9 #define inf 1<<30 10 #define FILL(a,b) memset(a,b,sizeof(a)) 11 12 struct Edge{ 13 int v,cap,next; 14 }edge[MAXM]; 15 16 int n,m,vs,vt,NV,NE,Cap[MAXN]; 17 int head[MAXN]; 18 19 void Insert(int u,int v,int cap) 20 { 21 edge[NE].v=v; 22 edge[NE].cap=cap; 23 edge[NE].next=head[u]; 24 head[u]=NE++; 25 26 edge[NE].v=u; 27 edge[NE].cap=0; 28 edge[NE].next=head[v]; 29 head[v]=NE++; 30 } 31 32 int level[MAXN],gap[MAXN]; 33 void bfs(int vt) 34 { 35 FILL(level,-1); 36 FILL(gap,0); 37 queue<int>que; 38 que.push(vt); 39 level[vt]=0; 40 gap[0]++; 41 while(!que.empty()){ 42 int u=que.front(); 43 que.pop(); 44 for(int i=head[u];i!=-1;i=edge[i].next){ 45 int v=edge[i].v; 46 if(level[v]!=-1)continue; 47 level[v]=level[u]+1; 48 gap[level[v]]++; 49 que.push(v); 50 } 51 } 52 } 53 54 int pre[MAXN],cur[MAXN]; 55 int SAP(int vs,int vt) 56 { 57 bfs(vt); 58 memcpy(cur,head,sizeof(head)); 59 int u=pre[vs]=vs,aug=inf,maxflow=0; 60 gap[0]=NV; 61 while(level[vs]<NV){ 62 bool flag=false; 63 for(int &i=cur[u];i!=-1;i=edge[i].next){ 64 int v=edge[i].v; 65 if(edge[i].cap>0&&level[u]==level[v]+1){ 66 flag=true; 67 aug=min(aug,edge[i].cap); 68 pre[v]=u; 69 u=v; 70 if(v==vt){ 71 maxflow+=aug; 72 for(u=pre[u];v!=vs;v=u,u=pre[u]){ 73 edge[cur[u]].cap-=aug; 74 edge[cur[u]^1].cap+=aug; 75 } 76 aug=inf; 77 } 78 break; 79 } 80 } 81 if(flag)continue; 82 int minlevel=NV; 83 for(int i=head[u];i!=-1;i=edge[i].next){ 84 int v=edge[i].v; 85 if(edge[i].cap>0&&level[v]<minlevel){ 86 minlevel=level[v]; 87 cur[u]=i; 88 } 89 } 90 if(--gap[level[u]]==0)break; 91 level[u]=minlevel+1; 92 gap[level[u]]++; 93 u=pre[u]; 94 } 95 return maxflow; 96 } 97 98 int main() 99 { 100 int _case,u,v,k1,k2,cap,t=1; 101 scanf("%d",&_case); 102 while(_case--){ 103 scanf("%d",&n); 104 NE=0; 105 FILL(head,-1); 106 vs=0,vt=2*n+1,NV=vt+1; 107 for(int i=1;i<=n;i++)scanf("%d",&Cap[i]); 108 scanf("%d",&m); 109 while(m--){ 110 scanf("%d%d%d",&u,&v,&cap); 111 Insert(u+n,v,cap); 112 } 113 for(int i=1;i<=n;i++)Insert(i,i+n,Cap[i]); 114 scanf("%d%d",&k1,&k2); 115 while(k1--){ 116 scanf("%d",&u); 117 Insert(vs,u,inf); 118 } 119 while(k2--){ 120 scanf("%d",&u); 121 Insert(u+n,vt,inf); 122 } 123 printf("Case %d: %d\n",t++,SAP(vs,vt)); 124 } 125 return 0; 126 }