奥妙重重的网络流

149人阅读 评论(0)

#include <bits/stdc++.h>
#define v edge[i].to
#define RepG(i,x) for(int i = head[x];~ i;i = edge[i].next)
#define fl edge[i].f
#define vfl edge[i ^ 1].f
#define Rep(i,n) for(int i = 1;i <= n;++ i)
using namespace std;
const int inf = 1 << 29;
const int N = 20005;
const int M = 400005;
int S = 0,T = N - 1,head[N],cnt,dep[N],q[N],ans,n;
struct Edge{int next,to,f;}edge[M];
bool bfs()
{
int h,t,x;
memset(dep,-1,sizeof(dep));
dep[q[h = t = 0] = S] = 0;
while(h <= t)
{
x = q[h ++];
RepG(i,x)if(dep[v] == -1 && fl)dep[q[++ t] = v] = dep[x] + 1;
}
return dep[T] > 0;
}
int dfs(int x,int f)
{
if(x == T)return f;
int s = 0,ff;
RepG(i,x)if(dep[v] == dep[x] + 1 && f && fl && (ff = dfs(v,min(f,fl))))
s += ff,fl -= ff,vfl += ff,f -=ff;
if(!s)dep[x] = -1;
return s;
}
int mxf(){int s = 0;for(;bfs();s += dfs(S,inf));return s;}
int ax[N],ay[N],m;
void ReBuild(int ans)
{
Rep(i,n)save(i,T,ans);
Rep(i,m)
{
save(S,n + i, 1);
save(n + i,ax[i], 1);
save(n + i,ay[i], 1);
}
}
bool check(){return mxf() < m;}
int main()
{
scanf("%d%d",&n,&m);
Rep(i,m)scanf("%d%d",&ax[i],&ay[i]);
int l = 0,r = 10000,mid;
while(l < r)
{
mid = l + r >> 1;
ReBuild(mid);
if(check())l = mid + 1;
else r = mid;
}
printf("%d\n",l);
return 0;
}
//slow
#include <bits/stdc++.h>
#define v edge[i].to
#define RepG(i,x) for(int i = head[x];~ i;i = edge[i].next)
#define fl edge[i].f
#define vfl edge[i ^ 1].f
#define Rep(i,n) for(int i = 1;i <= n;++ i)
using namespace std;
const int inf = 1 << 29;
const int N = 20005;
const int M = 400005;
int S = 0,T = N - 1,head[N],cnt,dep[N],q[N],ans,n;
struct Edge{int next,to,f;}edge[M];
bool bfs()
{
int h,t,x;
memset(dep,-1,sizeof(dep));
dep[q[h = t = 0] = S] = 0;
while(h <= t)
{
x = q[h ++];
RepG(i,x)if(dep[v] == -1 && fl)dep[q[++ t] = v] = dep[x] + 1;
}
return dep[T] > 0;
}
int dfs(int x,int f)
{
if(x == T)return f;
int s = 0,ff;
RepG(i,x)if(dep[v] == dep[x] + 1 && f && fl && (ff = dfs(v,min(f,fl))))
s += ff,fl -= ff,vfl += ff,f -=ff;
if(!s)dep[x] = -1;
return s;
}
int mxf(){int s = 0;for(;bfs();s += dfs(S,inf));return s;}
int ax[N],ay[N],m;
void ReBuild(int ans)
{
Rep(i,n)save(S, i,ans);
Rep(i,m)
{
save(n + i, T, 1);
save(ax[i], n + i, 1);
save(ay[i], n + i, 1);
}
}
bool check(){return mxf() < m;}//答案会比它大。
int main()
{
scanf("%d%d",&n,&m);
Rep(i,m)scanf("%d%d",&ax[i],&ay[i]);
int l = 0,r = 10000,mid;
while(l < r)
{
mid = l + r >> 1;
ReBuild(mid);
if(check())l = mid + 1;
else r = mid;
}
printf("%d\n",l);
return 0;
}
//fast

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：43421次
• 积分：1686
• 等级：
• 排名：千里之外
• 原创：128篇
• 转载：3篇
• 译文：0篇
• 评论：41条
单向膜拜