Description
YYD为了减肥,他来到了瘦海,这是一个巨大的海,海中有n个小岛,小岛之间有m座桥连接,两个小岛之间不会有两座桥,并且从一个小岛可以到另外任意一个小岛。现在YYD想骑单车从小岛1出发,骑过每一座桥,到达每一个小岛,然后回到小岛1。霸中同学为了让YYD减肥成功,召唤了大风,由于是海上,风变得十分大,经过每一座桥都有不可避免的风阻碍YYD,YYD十分ddt,于是用泡芙贿赂了你,希望你能帮他找出一条承受的最大风力最小的路线。
Input
输入:第一行为两个用空格隔开的整数n(2<=n<=1000),m(1<=m<=2000),接下来读入m行由空格隔开的4个整数a,b(1<=a,b<=n,a<>b),c,d(1<=c,d<=1000),表示第i+1行第i座桥连接小岛a和b,从a到b承受的风力为c,从b到a承受的风力为d。
Output
输出:如果无法完成减肥计划,则输出NIE,否则第一行输出承受风力的最大值(要使它最小)
Sample Input
4 4
1 2 2 4
2 3 3 4
3 4 4 4
4 1 5 4
Sample Output
4
HINT
注意:通过桥为欧拉回路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2147483647
#define LL long long
#define clr(x) memset(x, 0, sizeof x)
#define ms(a, x) memset(x, a, sizeof x)
#define digit (ch < '0' || ch > '9')
#define maxx(a, b, c) max(a, max(b, c))
using namespace std;
template <class T> inline void read(T &x) {
int flag = 1; x = 0;
register char ch = getchar();
while( digit) { if(ch == '-') flag = -1; ch = getchar(); }
while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
x *= flag;
}
const int maxm = 2005;
int n,m;
int a[maxm],b[maxm],c[maxm],d[maxm];
int fa[maxm],size[maxm],cnt,maxn;
struct edge { int to,f,nxt; } e[1001001];
int head[maxm],dpt[maxm],tot = 1;
int find(int x) {
if(!fa[x]) fa[x] = x, size[x] = 1;
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
inline void unionn(int x, int y) {
x = find(x); y = find(y);
if(x == y) return ;
if(size[x] > size[y]) swap(x, y);
fa[x] = y; size[y] += size[x]; cnt--;
}
inline void add(int x, int y, int z) { e[++tot].to = y; e[tot].f = z; e[tot].nxt = head[x]; head[x] = tot; }
bool bfs() {
int q[maxm], r = 0, h = 0;
ms(-1, dpt); q[++r] = 0; dpt[0] = 1;
while(r != h) {
int x = q[++h];
for(int i = head[x]; i; i = e[i].nxt) if(e[i].f && !~dpt[e[i].to]) {
dpt[e[i].to] = dpt[x]+1, q[++r] = e[i].to;
if(e[i].to == maxm-1) return true;
}
}
return false;
}
int dinic(int x, int flow) {
int left = flow;
if(x == maxm-1) return flow;
for(int tmp, i = head[x]; i && left; i = e[i].nxt) if(e[i].f && dpt[e[i].to] == dpt[x]+1)
tmp = dinic(e[i].to, min(left, e[i].f)), left -= tmp, e[i].f -= tmp, e[i^1].f += tmp;
if(left) dpt[x] = -1;
return flow-left;
}
inline bool check(int x) {
int degree[maxm]; cnt = n; tot = 1;
clr(head); clr(fa); clr(degree);
for(int i = 1; i <= m; i++)
if(d[i] <= x) unionn(a[i], b[i]), degree[a[i]]++, degree[b[i]]--, add(a[i], b[i], 1), add(b[i], a[i], 0);
else if(c[i] <= x) unionn(a[i], b[i]), degree[a[i]]--, degree[b[i]]++;
if(cnt >= 2) return false;
for(int i = 1; i <= n; i++) {
if(degree[i]&1) return false;
if(degree[i] > 0) add(0, i, degree[i]>>1), add(i, 0, 0);
else add(i, maxm-1, -degree[i]>>1), add(maxm-1, i, 0);
}
while(bfs()) dinic(0, INF);
for(int i = head[0]; i; i = e[i].nxt) if(e[i].f) return false;
return true;
}
int bisection(int l, int r) {
while(l+1 < r) {
int mid = l+r>>1;
if(check(mid)) r = mid;
else l = mid;
}
return check(l) ? l : r;
}
int main() {
read(n); read(m);
for(register int i = 1; i <= m; i++) {
read(a[0]); read(b[0]); read(c[0]); read(d[0]);
if(c[0] > d[0]) swap(a[0], b[0]), swap(c[0], d[0]);
a[i] = a[0]; b[i] = b[0];
c[i] = c[0]; d[i] = d[0];
maxn = maxx(maxn, c[0], d[0]);
}
int ans = bisection(1, maxn+1);
printf(ans == maxn+1 ? "NIE" : "%d",ans);
return 0;
}