2095: [Poi2010]Bridges
Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 811 Solved: 279
[ Submit][ Status][ Discuss]
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
1 2 2 4
2 3 3 4
3 4 4 4
4 1 5 4
Sample Output
HINT
注意:通过桥为欧拉回路
Source
显然要先二分答案,,不过之后就不会了。。。。。。
假设当前二分的答案为ans,那么对于原图的桥,可能等价于一条无向边,也可能等价于一条有向边
那么现在,问题就转变成,如何判断一张混合图是否存在欧拉回路
混合图存在欧拉回路,前提是它的基图存在欧拉回路
也就是说,先不考虑答案,每座桥看成无向边,统计每个顶点的度数
无向图存在欧拉回路的充要条件,是图连通且顶点数为偶数
题目保证了所给图连通,因此只要读入时统计一下每个顶点的度数,就能判掉NIE了
然后,有向图存在欧拉回路的充要条件,是所有顶点出度 == 入度
对于混合图,假设存在一条欧拉回路,把它画出来,所有的边的方向定成这条回路上的边的方向
那么,对于判断混合图是否存在欧拉回路,就等价于判断是否存在一个给无向边定向的方案,使得得到的有向图存在一条欧拉回路,这个问题,可以用网络流解决
先给所有无向边随机定向,统计每个顶点的出度和入度
假设基图的时候,第i个顶点的度数为du[i],那么,定向完成后这个顶点的出度和入度一定要等于du[i] / 2
显然,RP好到随机定向都能搞出一条欧拉回路是小概率事件,
对于第i个点,定向完成后,如果它的入度>du[i] / 2,那么从源点向它连容量为入度 - du[i] / 2的边
因为这个点一定要从它连通的无向边中选出 入度 - du[i] / 2 条边改成连出去
同理,如果出度 > du[i] / 2,那么从它向汇点连容量为出度 - du[i] / 2的边
最后,对于一条无向边连通的两个点,从随机定向的终点向起点连一条容量为1的边
如果有流量通过,代表调整这条边的指向
建好图跑一遍最大流,看流量是否平衡就行了
如果要输出方案,,检查每条无向边的流量通过情况就行啦
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1010;
const int maxm = 20020;
const int INF = ~0U>>1;
struct E{
int to,cap,flow; E(){}
E(int to,int cap,int flow):
to(to),cap(cap),flow(flow){}
}edgs[maxm];
int n,m,cnt,t1,t2,S,T,in[maxn],out[maxn],du[maxn]
,a[maxm],b[maxm],c[maxm],d[maxm],L[maxn],cur[maxn];
bool vis[maxn];
queue <int> Q;
vector <int> v[maxn],v2[maxn];
bool Build(int now)
{
for (int i = 1; i <= m; i++)
{
int x,y;
if (c[i] <= now && d[i] <= now)
{
int z = rand() % 2;
if (z) x = a[i],y = b[i];
else x = b[i],y = a[i];
v[y].push_back(x);
}
else if (c[i] <= now) x = a[i],y = b[i];
else if (d[i] <= now) x = b[i],y = a[i];
else return 0;
++in[y]; ++out[x];
}
return 1;
}
void Add(int x,int y,int cap)
{
v2[x].push_back(cnt); edgs[cnt++] = E(y,cap,0);
v2[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}
bool BFS()
{
for (int i = 1; i <= T; i++) L[i] = 0;
L[S] = 1; Q.push(S);
while (!Q.empty())
{
int k = Q.front(); Q.pop();
for (int i = 0; i < v2[k].size(); i++)
{
E e = edgs[v2[k][i]];
if (e.cap == e.flow || L[e.to]) continue;
L[e.to] = L[k] + 1; Q.push(e.to);
}
}
return L[T];
}
int Dinic(int x,int a)
{
if (x == T) return a; int flow = 0;
for (int &i = cur[x]; i < v2[x].size(); i++)
{
E &e = edgs[v2[x][i]];
if (e.cap == e.flow || L[e.to] != L[x] + 1) continue;
int f = Dinic(e.to,min(a,e.cap - e.flow));
if (!f) continue; flow += f; e.flow += f;
a -= f; edgs[v2[x][i]^1].flow -= f;
if (!a) return flow;
}
if (!flow) L[x] = -1; return flow;
}
void Clear()
{
for (int i = 1; i <= T; i++)
{
v[i].clear(); v2[i].clear();
in[i] = out[i] = 0;
}
cnt = t1 = t2 = 0;
memset(vis,0,sizeof(vis));
}
bool Judge(int now)
{
Clear(); if (!Build(now)) return 0;
for (int i = 1; i <= n; i++)
{
if (in[i] < du[i])
t1 += (du[i] - in[i]),Add(i,T,du[i] - in[i]);
else if (out[i] < du[i])
t2 += (du[i] - out[i]),Add(S,i,du[i] - out[i]);
for (int j = 0; j < v[i].size(); j++) Add(i,v[i][j],1);
}
if (t1 != t2) return 0; int MaxFlow = 0;
while (BFS())
{
for (int i = 1; i <= T; i++) cur[i] = 0;
MaxFlow += Dinic(S,INF);
}
return MaxFlow == t1;
}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint(); m = getint(); S = n + 1; T = S + 1;
for (int i = 1; i <= m; i++)
{
a[i] = getint(); b[i] = getint();
c[i] = getint(); d[i] = getint();
++du[a[i]]; ++du[b[i]];
}
for (int i = 1; i <= n; i++)
if (du[i]&1) {puts("NIE"); return 0;}
else du[i] >>= 1;
int L = 1,R = 1000;
while (R - L > 1)
{
int mid = (L + R) >> 1;
if (Judge(mid)) R = mid;
else L = mid;
}
cout << (Judge(L) ? L : R);
return 0;
}