51nod 1442 士兵的旅行
Posted 树的种子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了51nod 1442 士兵的旅行相关的知识,希望对你有一定的参考价值。
拆点,因为只能走一步,那么u->v 后就不能到k了,这样,建图就能保证只走一步;
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 105*2; 6 const int INF = 0x3f3f3f3f; 7 8 struct Edge { 9 int from,to,cap,flow; 10 }; 11 12 struct Dinic 13 { 14 int n,m,s,t; 15 vector<Edge> edge; 16 vector<int> G[maxn]; 17 bool vis[maxn]; 18 int d[maxn]; 19 int cur[maxn]; 20 21 void init() 22 { 23 for(int i=0;i<maxn;i++) 24 G[i].clear(); 25 edge.clear(); 26 memset(d,0,sizeof(d)); 27 memset(vis,0,sizeof(vis)); 28 memset(cur,0,sizeof(cur)); 29 } 30 31 void addEdge (int from,int to,int cap) 32 { 33 edge.push_back((Edge){from,to,cap,0}); 34 edge.push_back((Edge){to,from,0,0}); 35 m = edge.size(); 36 G[from].push_back(m-2); 37 G[to].push_back(m-1); 38 } 39 40 bool BFS() 41 { 42 memset(vis,0,sizeof(vis)); 43 queue<int> Q; 44 Q.push(s); 45 d[s] = 0; 46 vis[s] = 1; 47 while(!Q.empty()) 48 { 49 int x = Q.front(); 50 Q.pop(); 51 for(int i=0; i<G[x].size(); i++) 52 { 53 Edge & e = edge[G[x][i]]; 54 if(!vis[e.to]&&e.cap>e.flow) 55 { 56 vis[e.to] = 1; 57 d[e.to] = d[x] + 1; 58 Q.push(e.to); 59 } 60 } 61 } 62 return vis[t]; 63 } 64 65 int DFS(int x,int a) 66 { 67 if(x==t||a==0) return a; 68 int flow = 0,f; 69 for(int & i = cur[x]; i<G[x].size(); i++) 70 { 71 Edge & e = edge[G[x][i]]; 72 if(d[x] + 1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0) 73 { 74 e.flow +=f; 75 edge[G[x][i]^1].flow -=f; 76 flow +=f; 77 a-=f; 78 if(a==0) break; 79 } 80 } 81 return flow; 82 } 83 84 int Maxflow (int s,int t) { 85 this->s = s;this->t = t; 86 int flow = 0; 87 while(BFS()) { 88 memset(cur,0,sizeof(cur)); 89 flow+=DFS(s,INF); 90 } 91 return flow; 92 } 93 94 }sol; 95 96 97 int main() 98 { 99 int n,m; 100 scanf("%d%d",&n,&m); 101 102 int s = 0; 103 int t = 2*n+1; 104 105 sol.init(); 106 int sum = 0; 107 for(int i=1;i<=n;i++) { 108 int a; 109 scanf("%d",&a); 110 sum +=a; 111 sol.addEdge(s,i,a); 112 sol.addEdge(i,i+n,INF); 113 } 114 115 int sum2 = 0; 116 for(int i=1;i<=n;i++) { 117 int a; 118 scanf("%d",&a); 119 sum2+=a; 120 sol.addEdge(i+n,t,a); 121 } 122 123 for(int i=0;i<m;i++) { 124 int u,v; 125 scanf("%d%d",&u,&v); 126 sol.addEdge(u,v+n,INF); 127 sol.addEdge(v,u+n,INF); 128 } 129 130 int ans = sol.Maxflow(s,t); 131 if(ans==sum&&sum2==sum) 132 puts("YES"); 133 else puts("NO"); 134 135 return 0; 136 }
以上是关于51nod 1442 士兵的旅行的主要内容,如果未能解决你的问题,请参考以下文章