# 【BZOJ2095】[Poi2010]Bridges

darkbzoj

## 代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 1005;
struct Graph { int to, cap, next; } e[MAX_N << 5];
int fir[MAX_N], e_cnt;
void clearGraph() { memset(fir, -1, sizeof(fir)); e_cnt = 0; }
void Add_Edge(int u, int v, int c) {
e[e_cnt] = (Graph){v, c, fir[u]}, fir[u] = e_cnt++;
e[e_cnt] = (Graph){u, 0, fir[v]}, fir[v] = e_cnt++;
}
struct Edge { int u, v, a, b; } c[MAX_N << 1];
int N, M, deg[MAX_N];
int level[MAX_N];
bool bfs(int s, int t) {
queue<int> que;
for (int i = 0; i <= N + 1; i++) level[i] = -1;
level[s] = 0, que.push(s);
while (!que.empty()) {
int x = que.front(); que.pop();
for (int i = fir[x]; ~i; i = e[i].next) {
int v = e[i].to; if (!e[i].cap || ~level[v]) continue;
level[v] = level[x] + 1, que.push(v);
}
}
return level[t] != -1;
}
int iter[MAX_N];
int dfs(int x, int t, int f) {
if (x == t) return f;
for (int &i = iter[x]; ~i; i = e[i].next) {
int v = e[i].to;
if (e[i].cap && level[v] > level[x]) {
int d = dfs(v, t, min(f, e[i].cap));
if (d) {
e[i].cap -= d;
e[i ^ 1].cap += d;
return d;
}
}
}
level[x] = -1;
return 0;
}
int dinic(int s, int t) {
int res = 0;
while (bfs(s, t)) {
for (int i = 0; i <= N + 1; i++) iter[i] = fir[i];
int f;
while ((f = dfs(s, t, 1e9))) res += f;
}
return res;
}
bool check(int mid) {
clearGraph();
for (int i = 1; i <= N; i++) deg[i] = 0;
for (int i = 1; i <= M; i++) {
if (c[i].a <= mid) --deg[c[i].u], ++deg[c[i].v];
if (c[i].b <= mid) Add_Edge(c[i].v, c[i].u, 1);
}
for (int i = 1; i <= N; i++) if (deg[i] & 1) return 0;
int cnt = 0, s = 0, t = N + 1;
for (int i = 1; i <= N; i++) {
if (deg[i] > 0) Add_Edge(s, i, deg[i] >> 1), cnt += deg[i] >> 1;
if (deg[i] < 0) Add_Edge(i, t, -deg[i] >> 1);
}
return dinic(s, t) == cnt;
}
int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
N = gi(), M = gi();
int l = 1e9, r = 1;
for (int i = 1; i <= M; i++) {
c[i] = (Edge){gi(), gi(), gi(), gi()};
if (c[i].a > c[i].b) swap(c[i].u, c[i].v), swap(c[i].a, c[i].b);
l = min(l, c[i].a), r = max(r, c[i].b);
}
if (!check(r)) return puts("NIE") & 0;
int ans = r;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
printf("%d\n", ans);
return 0;
}
posted @ 2019-10-14 22:35  heyujun  阅读(200)  评论(0编辑  收藏  举报