zoukankan      html  css  js  c++  java
  • 51nod1442 士兵的旅行

    裸网络流拆点就可以了。。。

    #include<cstdio>
    #include<cstring>
    #include<cctype>
    #include<algorithm>
    using namespace std;
    #define rep(i,s,t) for(int i=s;i<=t;i++)
    #define dwn(i,s,t) for(int i=s;i>=t;i--)
    #define clr(x,c) memset(x,c,sizeof(x))
    #define qwq(x) for(edge *o=head[x];o;o=o->next)
    int read(){
    	int x=0;char c=getchar();
    	while(!isdigit(c)) c=getchar();
    	while(isdigit(c)) x=x*10+c-'0',c=getchar();
    	return x;
    }
    const int nmax=1e3+5;
    const int inf=0x7f7f7f7f;
    struct edge{
    	int to,cap;edge *next,*rev;
    };
    edge es[nmax],*pt=es,*head[nmax];
    void add(int u,int v,int d){
    	pt->to=v;pt->cap=d;pt->next=head[u];head[u]=pt++;
    	pt->to=u;pt->cap=0;pt->next=head[v];head[v]=pt++;
    	head[u]->rev=head[v];head[v]->rev=head[u];
    }
    int cnt[nmax],h[nmax];
    edge *cur[nmax],*p[nmax];
    int maxflow(int s,int t,int n){
    	clr(cnt,0),cnt[0]=n;clr(h,0);
    	int flow=0,a=inf,x=s;edge *e;
    	while(h[s]<n){
    		for(e=cur[x];e;e=e->next) if(e->cap>0&&h[x]==h[e->to]+1) break;
    		if(e){
    			a=min(a,e->cap);cur[x]=p[e->to]=e;x=e->to;
    			if(x==t){
    				while(x!=s) p[x]->cap-=a,p[x]->rev->cap+=a,x=p[x]->rev->to;
    				flow+=a,a=inf;
    			}
    		}else{
    			if(!--cnt[h[x]]) break;
    			h[x]=n;
    			for(e=head[x];e;e=e->next) if(e->cap>0&&h[x]>h[e->to]+1) cur[x]=e,h[x]=h[e->to]+1;
    			cnt[h[x]]++;
    			if(x!=s) x=p[x]->rev->to;
    		}
    	}
    	return flow;
    }
    int main(){
    	int n=read(),m=read(),u,v,d,s=0,t=n+n+1,sa=0,sb=0;
    	rep(i,1,n) u=read(),add(i,i+n,u),add(s,i,u),sa+=u;
    	rep(i,1,n) u=read(),add(i+n,t,u),sb+=u;
    	rep(i,1,m) u=read(),v=read(),add(u,n+v,inf),add(v,n+u,inf);
    	if(sa!=sb){
    		printf("NO
    ");
    		return 0;
    	}
    	if(maxflow(s,t,t+1)==sb) printf("YES
    ");
    	else printf("NO
    ");
    	return 0;
    }
    

      

    题目来源: CodeForces
    基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
     收藏
     关注

    在某个国家有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 4
    1 2 6 3
    3 5 3 1
    1 2
    2 3
    3 4
    4 2
    Output示例
    YES
  • 相关阅读:
    Python 远程开机
    Python 爬虫利器 Selenium
    Python爬虫——Python 岗位分析报告
    Python 爬虫入门(二)——爬取妹子图
    Python 爬虫入门(一)——爬取糗百
    边缘计算—你了解吗?
    关于图片适配不同尺寸的image View(实战)
    HTML页面转换为Sharepoint母版页(实战)
    SharePoint中你不知道的图片库(实战)
    Sharepoint 2013搜索服务配置总结(实战)
  • 原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5861195.html
Copyright © 2011-2022 走看看