首页 > 其他分享 >P3163 [CQOI2014] 危桥

P3163 [CQOI2014] 危桥

时间:2023-12-10 16:58:24浏览次数:24  
标签:fir cnt int P3163 add 危桥 CQOI2014 include define

题意

给定一张无向图。其中某些边只能走 \(2\) 次。

你要从 \(a_1\) 走到 \(a_2\) \(a_n\) 次,\(b_1\) 走到 \(b_2\) \(b_n\) 次。

问是否能实现。

Sol

不难想到连边跑网络流。

但是,只能走 \(2\) 次的限制无法满足。

注意到是无向图,所以我们交换其中一个起点终点。

不难发现这样过后,矛盾的贡献就不产生了。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#define int long long
#define pii pair <int, int>
using namespace std;
// #ifdef ONLINE_JUDGE

// #define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
// char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

// #endif
int read() {
    int p = 0, flg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') flg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        p = p * 10 + c - '0';
        c = getchar();
    }
    return p * flg;
}
void write(int x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}
#define fi first
#define se second

const int N = 205, M = 5e3 + 5, inf = 2e18;

namespace G {

array <int, N> fir;
array <int, M> nex, to, cap;
int cnt = 1;
void add(int x, int y, int z) {
    cnt++;
    nex[cnt] = fir[x];
    to[cnt] = y;
    cap[cnt] = z;
    fir[x] = cnt;
}
void _add(int x, int y, int z) {
    add(x, y, z);
    add(y, x, z);
}

}


namespace Mfl {

array <int, N> dis, cur;
queue <int> q;

bool bfs(pii st) {
    dis.fill(-1), dis[st.fi] = 0;
    q.push(st.fi);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = G::fir[u]; i; i = G::nex[i]) {
            if (G::cap[i] <= 0 || ~dis[G::to[i]]) continue;
            dis[G::to[i]] = dis[u] + 1; q.push(G::to[i]);
        }
    }
    return ~dis[st.se];
}

int dfs(int x, int augcd, pii st) {
    if (x == st.se) return augcd;
    int augc = augcd;
    for (int &i = cur[x]; i; i = G::nex[i]) {
        int flow = G::cap[i];
        if (flow <= 0 || dis[G::to[i]] <= dis[x]) continue;
        int del = dfs(G::to[i], min(augc, flow), st);
        augc -= del;
        G::cap[i] -= del, G::cap[i ^ 1] += del;
        if (augc <= 0) break;
    }
    return augcd - augc;
}

int dinic(pii st) {
    int ans = 0;
    while (bfs(st)) {
        copy(G::fir.begin(), G::fir.end(), cur.begin());
        // cur = G::fir;
        ans += dfs(st.fi, inf, st);
    }
    return ans;
}

}

char strbuf[N][N];

signed main() {
    // while ()
    int n, a1, a2, a3, b1, b2, b3;
    while (~scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &a1, &a2, &a3, &b1, &b2, &b3)) {
        a1++, a2++, b1++, b2++;
        G::fir.fill(0), G::cnt = 1;
        pii st = make_pair(n + 1, n + 2);
        for (int i = 1; i <= n; i++) {
            scanf("%s", strbuf[i] + 1);
            for (int j = i + 1; j <= n; j++) {
                if (strbuf[i][j] == 'X') continue;
                if (strbuf[i][j] == 'O') G::_add(i, j, 1);
                else G::_add(i, j, inf);
            }
        }
        G::_add(st.fi, a1, a3), G::_add(a2, st.se, a3);
        G::_add(st.fi, b1, b3), G::_add(b2, st.se, b3);
        int ans = Mfl::dinic(st);
        G::fir.fill(0), G::cnt = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                if (strbuf[i][j] == 'X') continue;
                if (strbuf[i][j] == 'O') G::_add(i, j, 1);
                else G::_add(i, j, inf);
            }   
        }
        G::_add(st.fi, a1, a3), G::_add(a2, st.se, a3);
        G::_add(st.fi, b2, b3), G::_add(b1, st.se, b3);
        if (Mfl::dinic(st) == ans && ans == a3 + b3) puts("Yes");
        else puts("No");
    }

    return 0;
}

标签:fir,cnt,int,P3163,add,危桥,CQOI2014,include,define
From: https://www.cnblogs.com/cxqghzj/p/17892858.html

相关文章

  • Luogu P3167 [CQOI2014]通配符匹配
    [CQOI2014]通配符匹配题目描述几乎所有操作系统的命令行界面(CLI)中都支持文件名的通配符匹配以方便用户。最常见的通配符有两个,一个是星号(”“'),可以匹配0个及以上的任意字符:另一个是问号(”?“),可以匹配恰好一个任意字符。现在需要你编写一个程序,对于给定的文件名列表和一个包......
  • 倾角测量仪在危房危桥中监测中的使用
    2009年6月27日,​​上海​​​的一栋竣工未交付使用的高楼整体倒覆,官方以两次堆土施工为原由,遭网友抨击,故得此称号。2011年,河南郑州再现​​楼脆脆​​事件。随着城市化进程......
  • P3167 [CQOI2014]通配符匹配 题解
    想了两种做法,第一种拿到了10分的好成绩。而第二种做法不用前缀和,而且还跑的飞快。目前最优解第三尝试卡进最优解未果。不得不说这是一道好题,做完对KMP有了更深的理解......
  • BZOJ 3503([Cqoi2014]和谐矩阵-gauss消元)
    Description我们称一个由0和1组成的矩阵是和谐的,当且仅当每个元素都有偶数个相邻的1。一个元素相邻的元素包括它本身,及他上下左右的4个元素(如果存在)。给定矩阵的行数和......
  • [CQOI2014]通配符匹配
    好久没有做过字符串哈希的题,没想到竟然调了这么久。首先我们可以母串根据?或者星号分为几段,这里有一个小技巧,可以给母串前面加一个?后面加一个a,然后在要匹配的串前后各加一......
  • 做题记录:P3166 [CQOI2014]数三角形
    题目链接题意:给定 (n+1)(m+1)(n+1)(m+1) 个点的网格图,任意投三个点,求三角形的个数。首先,不考虑三点共线的情况,方案数可以很轻松的得出来。在 (n+1)(m+1)(n+1)(m+1) ......