bzoj 3504: [Cqoi2014]危桥

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cstring>
  4 #define M 100009
  5 #define inf 2139062143
  6 using namespace std;
  7 int n,a1,a2,an,b1,b2,bn,tot,cnt=1,T,ans,head[M],d[M],q[2*M],next[10*M],u[10*M],v[10*M];
  8 char ch[60][60];
  9 bool bfs()
 10 {
 11     memset(d,0,sizeof(int)*(T+1));
 12     int h=0,t=1;
 13     q[1]=0;
 14     d[0]=1;
 15     for(;h<t;)
 16       {
 17         h++;
 18         int p=q[h];
 19         for(int i=head[p];i;i=next[i])
 20           if(!d[u[i]]&&v[i])
 21             {
 22                 d[u[i]]=d[p]+1;
 23                 if(d[T])
 24                   return 1;
 25                 t++;
 26                 q[t]=u[i];
 27             }
 28       }
 29     return 0;
 30 }
 31 int dinic(int s,int f)
 32 {
 33     if(s==T)
 34       return f;
 35     int rest=f;
 36     for(int i=head[s];i&&rest;i=next[i])
 37       if(v[i]&&d[u[i]]==d[s]+1)
 38         {
 39             int now=dinic(u[i],min(rest,v[i]));
 40             if(!now)
 41               d[u[i]]=0;
 42             v[i]-=now;
 43             v[i^1]+=now;
 44             rest-=now;
 45         }
 46     return f-rest;
 47 }
 48 void jia1(int a1,int a2,int a3)
 49 {
 50     cnt++;
 51     next[cnt]=head[a1];
 52     head[a1]=cnt;
 53     u[cnt]=a2;
 54     v[cnt]=a3;
 55     return;
 56 }
 57 void jia(int a1,int a2,int a3)
 58 {
 59     jia1(a1,a2,a3);
 60     jia1(a2,a1,0);
 61     return;
 62 }
 63 void build(int a1,int a2,int an,int b1,int b2,int bn)
 64 {
 65     T=n+1;
 66     cnt=1;
 67     memset(head,0,sizeof(int)*(T+1));
 68     jia(0,a1,2*an);
 69     jia(a2,T,2*an);
 70     jia(0,b1,2*bn);
 71     jia(b2,T,2*bn);
 72     for(int i=1;i<=n;i++)
 73       for(int j=1;j<=n;j++)
 74         {
 75           if(ch[i][j]==‘O‘)
 76             jia(i,j,2);
 77           if(ch[i][j]==‘N‘)
 78             jia(i,j,inf);
 79         }
 80     return;
 81 }
 82 int main()
 83 {
 84     for(;scanf("%d%d%d%d%d%d%d",&n,&a1,&a2,&an,&b1,&b2,&bn)==7;)
 85     {
 86     a1++;
 87     a2++;
 88     b1++;
 89     b2++;
 90     for(int i=1;i<=n;i++)
 91       scanf("%s",ch[i]+1);
 92     build(a1,a2,an,b1,b2,bn);
 93     ans=0;
 94     for(;bfs();)
 95       ans+=dinic(0,inf);
 96     if(ans==2*an+2*bn)
 97       {
 98         build(a1,a2,an,b2,b1,bn);
 99         ans=0;
100         for(;bfs();)
101           ans+=dinic(0,inf);
102         if(ans==2*an+2*bn)
103           printf("Yes\n");
104         else
105           printf("No\n");
106       }
107     else
108       printf("No\n");}
109     return 0;
110 }

网络流。

时间: 2024-08-28 00:58:42

bzoj 3504: [Cqoi2014]危桥的相关文章

3504: [Cqoi2014]危桥

3504: [Cqoi2014]危桥 链接 分析: 首先往返的可以转化为全是“往”,那么只要将容量除以2即可. 然后S向a1连边容量为an(除以2之前为2*an),S向a2连边容量为an,b1,b2向T连边容量为bn.原图上的边,建双向边保存. 这样会存在从a1流向b2的流量,当然也有b1流向a2的流量,考虑如何判断这种情况. 将b1,b2交换,然后重新跑一遍,判断是否满流即可. 第一遍最大流的时候,假设a1->b2流了x的流量,那么有 a1->a2:an-x, a1->b2:x, b1

bzoj千题计划137:bzoj [CQOI2014]危桥

http://www.lydsy.com/JudgeOnline/problem.php?id=3504 往返n遍,即单向2*n遍 危桥流量为2,普通桥流量为inf 原图跑一遍最大流 交换b1,b2再跑一遍最大流 如果两次的结果都等于(an+bn)*2 则可以 证明参见http://www.cnblogs.com/chenyushuo/p/5139556.html #include<queue> #include<cstdio> #include<cstring> #i

Luogu3163 [CQOI2014]危桥 ---- 网络流 及 一个细节的解释

Luogu3163 [CQOI2014]危桥 题意 有$n$个点和$m$条边,有些边可以无限次数的走,有些边这辈子只能走两次,给定两个起点和终点$a_1 --> a_2$(起点 --> 终点)和$b_1 --> b_2$(起点 --> 终点),询问是否可以让$a_1 --> a_2$往返$a_n$次,让$b_1 --> b_2$往返$b_n$次 题解 思路 思路还是比较好想的,就是原图连双向边,然后炒鸡源汇连$a_n*2$和$b_n*2$判断满流是否为$(a_n+b_n

【BZOJ 3504】[Cqoi2014]危桥

Description Alice和Bob居住在一个由N座岛屿组成的国家,岛屿被编号为0到N-1.某些岛屿之间有桥相连,桥上的道路是双 向的,但一次只能供一人通行.其中一些桥由于年久失修成为危桥,最多只能通行两次.Alice希望在岛屿al和a2之间往返an次(从al到a2再从a2 到al算一次往返).同时,Bob希望在岛屿bl和b2之间往返bn次.这个过程中,所有危桥最多通行两次,其余的桥可以无限次通行.请问Alice和 Bob能完成他们的愿望吗? Input 本题有多组测试数据. 每组数据第一

P3163 [CQOI2014]危桥

传送门 我是看不出这玩意儿和网络流有什么关系-- 我们把图中的所有边都当成无向边加入图中,容量为\(inf\) 危桥的容量为\(2\) 从源点到\(a1,b1\)连边容量为\(an*2\),\(a2,b2\)到汇点连边容量\(bn*2\),相当于一次把两边都走完 然后跑一遍看看是否满流即可 然而这样会有一个问题,就是最终求得的最大流是\(a1\)流向\(b2\)或\(a2\)流向\(b1\) 于是再从源点向\(a1\)和\(b2\)连边,\(a2\)和\(b1\)向汇点连边,再跑一遍看看是否满流

bzoj3504: [Cqoi2014]危桥--最大流

题目大意:给张无向图,有两个人a,b分别从各自的起点走向各自的终点,走A,B个来回,图里有些边只能走两次,求问是否能满足a,b的需求 按照题目给的表建图 S连a1,b1 a2,b2连T 跑最大流看是否满流 为了防止a1跑到b2的情况 第二遍 S连a1,b2 a2,b1连T 若还是满流说明没问题 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<queue> 5

bzoj3504: [Cqoi2014]危桥 网络流

一种网络流建图的思路吧,改天最好整理一波网络流建图思路 1 #include <bits/stdc++.h> 2 using namespace std; 3 int n,h,t,a1,a2,an,b1,b2,bn,flow,now;char ch; 4 int dis[52],l[52],d[52][52];char c[52][52]; 5 char getch() 6 { 7 for(ch=getchar();ch!='O' && ch!='N' && c

bzoj3504: [Cqoi2014]危桥

题意:给出一个图,有的边可以无限走,有的只能走两次(从一头到另一头为一次),给定两个起点以及对应的终点以及对应要走几个来回,求判断是否能完成. 先来一个NAIVE的建图:直接限制边建为容量1,无限制为INF,按照原图连,然后跑最大流就可以了. 可惜这样还不够,因为有可能有一部分流量不是对应的起点流过来的,即两条路有流量交换,这样就不一定可以满足题意了. 解决方法是:再跑一遍网络流,但是建图要改变一下,即将a路线的起点终点调换一下(当然b也可以),再接着跑,如果仍然满足则是真的有解. 证明看了网上

[Cqoi2014]危桥 (两遍网络流)

题目链接 1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 inline int read() 5 { 6 int x=0,f=1;char ch=getchar(); 7 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 8 while(ch>='0'&&ch<='9'){x=x*10+ch-'0