匪徒准备从一个车站转移毒品到另一个车站,警方准备进行布控. 对于每个车站进行布控都需要一定的代价,现在警方希望使用最小的代价控制一些车站,使得去掉这些车站后,匪徒无法从原定的初始点到达目标点
标签:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL inf=0x3f3f3f3f; 5 const LL maxn=600; 6 const LL maxm=100000; 7 struct Edge{ 8 LL to;//这条边通向的点 9 LL rest;//这条变的权值 10 LL next;//与这条边相连的上一条边 11 }; 12 Edge edge[maxm];//edge[i]代表一条有向边 13 LL head[maxm]; 14 LL ecnt=1; 15 inline void AddEdge(LL x,LL y,LL r){ 16 edge[++ecnt].to=y; edge[ecnt].rest=r; edge[ecnt].next=head[x]; head[x]=ecnt; 17 edge[++ecnt].to=x; edge[ecnt].rest=0; edge[ecnt].next=head[y]; head[y]=ecnt; 18 } 19 LL N,M,S,T; 20 LL dis[maxn]; 21 22 inline bool BFS(){ 23 memset(dis,0,sizeof(dis)); 24 dis[S]=1; 25 static queue<LL> Q; 26 while(Q.size()>0) Q.pop(); 27 Q.push(S); 28 while(Q.size()>0){ 29 LL x=Q.front(); Q.pop(); 30 for(LL e=head[x] ; e ; e=edge[e].next){//e表示的是边,不是点 31 LL d=edge[e].to; 32 if(edge[e].rest&&dis[d]==0){//边权不为零且这条边通向的点的 33 dis[d]=dis[x]+1; 34 Q.push(d); 35 if(d==T) return true; 36 } 37 } 38 } 39 return false; 40 } 41 42 inline LL DFS(LL x,LL flow){ 43 if(x==T) return flow; 44 LL now=0,tmp; 45 for(LL e=head[x]; e ;e=edge[e].next){ 46 if(edge[e].rest&&dis[edge[e].to]==dis[x]+1){ 47 tmp=DFS(edge[e].to,min(flow-now,edge[e].rest)); 48 edge[e].rest-=tmp; 49 edge[e ^ 1].rest+=tmp; 50 now+=tmp; 51 if(now==flow) return now; 52 } 53 } 54 return now; 55 } 56 inline LL dinic(){ 57 LL ans=0; 58 while(BFS()){ 59 ans+=DFS(S,inf); 60 } 61 return ans; 62 } 63 LL money[maxn]; 64 LL tot; 65 int main(){ 66 cin>>N>>M; 67 cin>>S>>T; 68 T+=N; 69 for(LL i=1;i<=N;i++){ 70 LL v; 71 cin>>v; 72 AddEdge(i,i+N,v); 73 } 74 for(LL i=1;i<=M;i++){ 75 LL u,v; 76 cin>>u>>v; 77 AddEdge(u+N,v,inf); 78 AddEdge(v+N,u,inf); 79 } 80 cout<<dinic(); 81 return 0; 82 }
第二种:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const LL inf=0x3f3f3f3f; 5 const LL maxn=600; 6 const LL maxm=100000; 7 struct Edge{ 8 LL to;//这条边通向的点 9 LL rest;//这条变的权值 10 LL next;//与这条边相连的上一条边 11 }; 12 Edge edge[maxm];//edge[i]代表一条有向边 13 LL head[maxm]; 14 LL ecnt=1; 15 inline void AddEdge(LL x,LL y,LL r){ 16 edge[++ecnt].to=y; edge[ecnt].rest=r; edge[ecnt].next=head[x]; head[x]=ecnt; 17 edge[++ecnt].to=x; edge[ecnt].rest=0; edge[ecnt].next=head[y]; head[y]=ecnt; 18 } 19 LL N,M,S,T; 20 LL dis[maxn]; 21 22 inline bool BFS(){ 23 memset(dis,0,sizeof(dis)); 24 dis[S]=1; 25 static queue<LL> Q; 26 while(Q.size()>0) Q.pop(); 27 Q.push(S); 28 while(Q.size()>0){ 29 LL x=Q.front(); Q.pop(); 30 for(LL e=head[x] ; e ; e=edge[e].next){//e表示的是边,不是点 31 LL d=edge[e].to; 32 if(edge[e].rest&&dis[d]==0){//边权不为零且这条边通向的点的 33 dis[d]=dis[x]+1; 34 Q.push(d); 35 if(d==T) return true; 36 } 37 } 38 } 39 return false; 40 } 41 42 inline LL DFS(LL x,LL flow){ 43 if(x==T) return flow; 44 LL now=0,tmp; 45 for(LL e=head[x]; e ;e=edge[e].next){ 46 if(edge[e].rest&&dis[edge[e].to]==dis[x]+1){ 47 tmp=DFS(edge[e].to,min(flow-now,edge[e].rest)); 48 edge[e].rest-=tmp; 49 edge[e ^ 1].rest+=tmp; 50 now+=tmp; 51 if(now==flow) return now; 52 } 53 } 54 return now; 55 } 56 inline LL dinic(){ 57 LL ans=0; 58 while(BFS()){ 59 ans+=DFS(S,inf); 60 } 61 return ans; 62 } 63 LL money[maxn]; 64 LL tot; 65 int main(){ 66 cin>>N>>M; 67 cin>>S>>T; 68 for(LL i=1;i<=N;i++) scanf("%d",&money[i]); 69 for(LL i=1;i<=N;i++){ 70 tot++; 71 AddEdge(tot,tot+1,money[i]); 72 tot++; 73 } 74 for(LL i=1;i<=M;i++){ 75 LL r,l; 76 cin>>r>>l; 77 LL rr,ll; 78 rr=(r-1)*2+2;//出点 79 ll=(l-1)*2+1;//入点 80 AddEdge(rr,ll,inf); 81 ll++; 82 rr--; 83 AddEdge(ll,rr,inf); 84 } 85 S=(S-1)*2+1; 86 T=(T-1)*2+2; 87 cout<<dinic(); 88 return 0; 89 }
标签:
原文地址:http://www.cnblogs.com/CXCXCXC/p/4700866.html