zoukankan      html  css  js  c++  java
  • 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 #define INF 0x7fffffff
     6 using namespace std;
     7 const int maxe = 5050, maxn = 55;
     8 int n,a1,a2,A,b1,b2,B;
     9 char st[1000];
    10 struct node{
    11     int to[maxe],next[maxe],flow[maxe];
    12     int head[maxn],h[maxn],tot,s,t;
    13     void init(){s=0; t=n+1; memset(head,-1,sizeof(head)); tot=-1;}
    14     void insert(int u, int v, int f){
    15         to[++tot]=v;
    16         next[tot]=head[u];
    17         flow[tot]=f;
    18         head[u]=tot;
    19         to[++tot]=u;
    20         next[tot]=head[v];
    21         flow[tot]=0;
    22         head[v]=tot;
    23     }
    24     bool bfs(){
    25         memset(h,-1,sizeof(h));
    26         queue<int> Q;
    27         Q.push(s);
    28         h[s]=0;
    29         while (!Q.empty()){
    30             int now=Q.front();
    31             Q.pop();
    32             for (int i=head[now]; i!=-1; i=next[i]){
    33                 int v=to[i];
    34                 if (flow[i] && h[v]==-1){
    35                     h[v]=h[now]+1;
    36                     Q.push(v);
    37                 }
    38             }
    39         }
    40         if (h[t]==-1) return 0; return 1;
    41     }
    42     int dfs(int x, int mx){
    43         if (x==t) return mx;
    44         int res=0;
    45         for (int i=head[x]; i!=-1; i=next[i]){
    46             int v=to[i];
    47             if (flow[i] && h[v]==h[x]+1){
    48                 int f=dfs(v,min(flow[i],mx));
    49                 flow[i]-=f; flow[i^1]+=f;
    50                 res+=f; mx-=f;
    51                 if (!mx) break;
    52             }
    53         }
    54         if (!res) h[x]=-1;
    55         return res;
    56     }    
    57     int maxflow(){
    58         int ans=0;
    59         while (bfs()) ans+=dfs(s,INF);
    60         return ans;
    61     }
    62 }x,y;
    63 int main(){
    64     while (scanf("%d%d%d%d%d%d%d", &n, &a1, &a2, &A, &b1, &b2, &B)!=EOF){
    65         a1++; a2++; b1++; b2++;
    66         A*=2; B*=2; x.init();
    67         for (int i=1; i<=n; i++){
    68             scanf("%s", st+1);
    69             for (int j=1; j<=n; j++){
    70                 if (st[j]=='O') x.insert(i,j,2);
    71                 else if (st[j]=='N') x.insert(i,j,INF);
    72             }
    73         }
    74         y=x;
    75         int s=0, t=n+1;
    76         x.insert(s,a1,A); x.insert(a2,t,A); x.insert(s,b1,B); x.insert(b2,t,B);
    77         y.insert(s,a1,A); y.insert(a2,t,A); y.insert(s,b2,B); y.insert(b1,t,B);
    78         if (x.maxflow()==A+B && y.maxflow()==A+B) printf("Yes
    "); else printf("No
    ");
    79     }
    80     return 0;
    81 }
  • 相关阅读:
    [TCP IP详解:学习笔记]UDP:用户数据协议
    [TCP IP详解:学习笔记]IP选路
    [TCP IP详解:学习笔记]IP:网络协议
    [TCP IP详解:学习笔记]TCP连接的建立与终止
    [TCP IP详解:学习笔记]ICMP:Internet控制报文协议
    [TCP/IP详解:学习笔记]链路层
    [TCP/IP详解:学习笔记]ARP:地址解析协议
    [TCP IP详解:学习笔记]TCP:传输控制协议
    [TCP IP详解:学习笔记]广播和多播
    [TCP IP详解:学习笔记]IGMP:Internet组管理协议
  • 原文地址:https://www.cnblogs.com/mzl0707/p/5532774.html
Copyright © 2011-2022 走看看