在某个国家有n个城市,他们通过m条无向的道路相连。每个城市有一支军队。第i个城市的军队有ai个士兵。现在士兵开始移动。每个士兵可以呆在原地,或者走到和他所在城市直接相邻的城市,即设每一条边长度为1的话,他离开之后不能距离原来城市大于1。
判断移动之后,能不能使得第i个城市恰好有bi个士兵。
Input单组测试数据。第一行有两个整数n和m(1 ≤ n ≤ 100, 0 ≤ m ≤ 200)。第二行包含n个整数 a1, a2, ..., an (0 ≤ ai ≤ 100)。第三行包含n个整数b1, b2, ..., bn (0 ≤ bi ≤ 100)。接下来m行,每行给出两个整数 p 和q (1 ≤ p, q ≤ n, p ≠ q),表示p和q之间有一条无向的道路。每两个城市之间最多有一条无向的道路。Output如果能够调整成功,输出YES,否则输出NO。Input示例4 41 2 6 33 5 3 11 22 33 44 2Output示例YES构造源点和汇点----然后直接最大流救过了--------------没有考虑只能到相邻点--------数据有点弱
代码:
#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;struct node{int to,cost,cap,ffv;}OP;vector<node>V[120];int n,m,dist[110],PRe1[110],pre2[110],aa[110];void add_edge(int a,int b,int c){ OP.to=b;OP.cost=1;OP.cap=c;OP.ffv=V[b].size(); V[a].push_back(OP); OP.to=a;OP.cost=-1;OP.cap=0;OP.ffv=V[a].size()-1; V[b].push_back(OP);}bool tyuyi(int s,int t,int F){ int INF=55555; while (F) { fill(dist,dist+n+1,INF); fill(pre1,pre1+n+1,-1); fill(pre2,pre2+n+1,-1); dist[s]=0; bool update=true; while (update) { update=false; for (int i=1;i<=n;i++) { if (dist[i]==INF) continue; for (int j=0;j<V[i].size();j++) { if (V[i][j].cap>0&&dist[i]+V[i][j].cost<dist[V[i][j].to]) { update=true; dist[V[i][j].to]=dist[i]+V[i][j].cost; pre1[V[i][j].to]=i;pre2[V[i][j].to]=j; } } } } if (dist[t]==INF) return false; F--;int k=t,h; while (pre1[k]!=-1) { h=pre2[k]; k=pre1[k]; V[k][h].cap--; V[V[k][h].to][V[k][h].ffv].cap++; } } return true;}int main(){ int a,b,c,s1,s2; scanf("%d%d",&n,&m); s1=s2=0; for (int i=1;i<=n;i++) { scanf("%d",&c); s1+=c; aa[i]=c; add_edge(n+1,i,c); } for (int i=1;i<=n;i++) { scanf("%d",&c); s2+=c; add_edge(i,n+2,c); } while (m--) { scanf("%d%d",&a,&b); add_edge(a,b,aa[a]); add_edge(b,a,aa[b]); } n+=2; if (s1!=s2) { printf("NO/n"); return 0; } bool fafe=tyuyi(n-1,n,s1); if (fafe) printf("YES/n"); else printf("NO/n"); return 0;}
新闻热点
疑难解答