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->b2:bn-x
b1->a2:x
交换b1和b2之后,一定有
a1->a2:an-x
b2->b1:bn-x
因为这是双向边。那么如果此时仍然满流,说明a1->b1:x,a2可以向b1流x的流量,结合第一个a1可以向b2流x的流量,那么如果将第一次的a1向b2流的流量,流向b1,那么就合法了。
还有一个小问题,SovietPower提到的。
代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<cctype> #include<set> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-‘0‘;return x*f; } const int N = 105, INF = 1e9; struct Edge{ int to, nxt, cap; } e[10005]; int head[N], dis[N], q[N], cur[N]; char s[N][N]; int En, S, T, n, a1, a2, an, b1, b2, bn; inline void add_edge(int u,int v,int w) { ++En; e[En].to = v, e[En].cap = w, e[En].nxt = head[u]; head[u] = En; ++En; e[En].to = u, e[En].cap = 0, e[En].nxt = head[v]; head[v] = En; } bool bfs() { for (int i = 0; i <= T; ++i) dis[i] = -1, cur[i] = head[i]; int L = 1, R = 0; q[++R] = S, dis[S] = 0; while (L <= R) { int u = q[L ++]; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (dis[v] == -1 && e[i].cap > 0) { dis[v] = dis[u] + 1; q[++R] = v; if (v == T) return 1; } } } return 0; } int dfs(int u,int flow) { if (u == T) return flow; int used = 0; for (int &i = cur[u]; i; i = e[i].nxt) { int v = e[i].to; if (dis[v] == dis[u] + 1 && e[i].cap > 0) { int tmp = dfs(v, min(flow - used, e[i].cap)); if (tmp > 0) { e[i].cap -= tmp, e[i ^ 1].cap += tmp; used += tmp; if (used == flow) break; } } } if (used != flow) dis[u] = -1; return used; } int dinic() { int ans = 0; while (bfs()) ans += dfs(S, INF); return ans; } void build() { En = 1; memset(head, 0, sizeof(head)); add_edge(S, a1, an);add_edge(S, b1, bn); add_edge(a2, T, an);add_edge(b2, T, bn); for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) if (s[i][j] == ‘O‘) add_edge(i, j, 1), add_edge(j, i, 1); else if (s[i][j] == ‘N‘) add_edge(i, j, INF), add_edge(j, i, INF); } void solve() { S = 0, T = n + 1; a1 = read() + 1, a2 = read() + 1, an = read(); b1 = read() + 1, b2 = read() + 1, bn = read(); for (int i = 1; i <= n; ++i) scanf("%s", s[i] + 1); build(); if (dinic() != an + bn) { puts("No"); return ; } swap(b1, b2); build(); if (dinic() != an + bn) { puts("No"); return ; } puts("Yes"); } int main() { while (~scanf("%d", &n)) solve(); return 0; }
原文地址:https://www.cnblogs.com/mjtcn/p/10339497.html
时间: 2024-11-15 14:26:26