• [CQOI2014]危桥


    嘟嘟嘟

    看到这个数据范围。就很容易想到网络流。
    首先对于正常的桥,这条边的容量相当于(INF),对于危桥,容量为(2)。然后按这个方式建无向图就行。
    对于在(a1)(a2)之间往返(an)次,相当于(a1)(a2)流了至少(2 * an)的流量。那么就从源点向(a1)连一条容量为(2 * an)的边,从(a2)向汇点连一条容量为(2 * an)的边;对于(b)也同理。最后判断总流量是否为(2 * (an + bn))即可。
    但这个算法不一定对,因为可能从(a1)流到(b2),所以我们再重建图,把(a1)(b2)都连到源点,(a2)(b1)都连向汇点。这样就保证不会从(a1)流到(b2)了。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 55;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, a1, a2, an, b1, b2, bn;
    char a[maxn][maxn];
    
    int s, t;
    struct Edge
    {
      int nxt, from, to, cap, flow;
    }e[(maxn * maxn) << 1];
    int head[maxn], ecnt = -1;
    void init()
    {
      Mem(head, -1); ecnt = -1;
    }
    void addEdge(int x, int y, int w)
    {
      e[++ecnt] = (Edge){head[x], x, y, w, 0};
      head[x] = ecnt;
      e[++ecnt] = (Edge){head[y], y, x, 0, 0};
      head[y] = ecnt;
    }
    
    int dis[maxn];
    bool bfs()
    {
      Mem(dis, 0); dis[s] = 1;
      queue<int> q; q.push(s);
      while(!q.empty())
        {
          int now = q.front(); q.pop();
          for(int i = head[now], v; i != -1; i = e[i].nxt)
    	{
    	  v = e[i].to;
    	  if(!dis[v] && e[i].cap > e[i].flow)
    	    {
    	      dis[v] = dis[now] + 1;
    	      q.push(v);
    	    }
    	}
        }
      return dis[t];
    }
    int cur[maxn];
    int dfs(int now, int res)
    {
      if(now == t || res == 0) return res;
      int flow = 0, f;
      for(int& i = cur[now], v; i != -1; i = e[i].nxt)
        {
          v = e[i].to;
          if(dis[v] == dis[now] + 1 && (f = dfs(v, min(res, e[i].cap - e[i].flow))) > 0)
    	{
    	  e[i].flow += f; e[i ^ 1].flow -= f;
    	  flow += f; res -= f;
    	  if(res == 0) break;
    	}
        }
      return flow;
    }
    
    int maxflow()
    {
      int flow = 0;
      while(bfs())
        {
          memcpy(cur, head, sizeof(head));
          flow += dfs(s, INF);
        }
      return flow;
    }
    
    int main()
    {
      while(scanf("%d%d%d%d%d%d%d", &n, &a1, &a2, &an, &b1, &b2, &bn) != EOF)
        {
          init();
          bool flg = 0;
          s = n; t = n + 1;
          for(int i = 0; i < n; ++i) scanf("%s", a[i]);
          for(int i = 0; i < n; ++i)
    	for(int j = 0; j < n; ++j)
    	  if(a[i][j] == 'O') addEdge(i, j, 2);
    	  else if(a[i][j] == 'N') addEdge(i, j, INF);
          addEdge(s, a1, an << 1); addEdge(s, b1, bn << 1);
          addEdge(a2, t, an << 1); addEdge(b2, t, bn << 1);
          if(maxflow() >= ((an + bn) << 1)) flg = 1;
          if(flg)
    	{
    	  init();
    	  for(int i = 0; i < n; ++i)
    	    for(int j = 0; j < n; ++j)
    	      if(a[i][j] == 'O') addEdge(i, j, 2);
    	      else if(a[i][j] == 'N') addEdge(i, j, INF);
    	  addEdge(s, a1, an << 1); addEdge(s, b2, bn << 1);
    	  addEdge(a2, t, an << 1); addEdge(b1, t, bn << 1);	 
    	  if(maxflow() < ((an + bn) << 1)) flg = 0;
    	}
          puts(flg ? "Yes" : "No");
        }
      return 0;
    }
    
  • 相关阅读:
    html5 中的 css样式单 的 两种调用方式的区别
    互联网公司的相关人员及业务简介
    require.js 入门学习-备
    IOS修改webView背景透明以及IOS调用前台js的方法
    Javascript AMD模块化规范-备用
    Meta 的两个 相关属性
    <meta http-equiv="pragma" content="no-cache"/>
    css:中文词不断开,整体换行
    linux驱动开发---导出内核符号
    web html 防盗链
  • 原文地址:https://www.cnblogs.com/mrclr/p/9958312.html
Copyright © 2020-2023  润新知