poj2455Secret Milking Machine(二分+最大流)

时间:2023-03-09 04:27:45
poj2455Secret Milking Machine(二分+最大流)

链接

二分距离,小于当前距离的边容量+1,使最后流>=t

注意 会有重边

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 205
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int path[N],flow[N],gh[N][N],st,en;
int w[N][N];
struct node
{
int u,v,c;
}q[N*N];
int bfs()
{
int i;
memset(path,-,sizeof(path));
for(i = ; i <= en ; i++)
flow[i] = INF;
queue<int>q;
q.push();
while(!q.empty())
{
int tk = q.front();
q.pop();
if(tk==en)
break;
for(i = ; i <= en ; i++)
{
if(path[i]==-&&gh[tk][i])
{
path[i] = tk;
flow[i] = min(flow[tk],gh[tk][i]);
q.push(i);
}
}
}
if(path[en]==-)
return -;
return flow[en];
}
int EK()
{
int now,pre,sum=,k;
while((k=bfs())!=-)
{
sum+=k;
now = en;
while(now!=st)
{
pre = path[now];
gh[pre][now]-=k;
gh[now][pre]+=k;
now = pre;
}
}
return sum;
}
int main()
{
int n,p,i,m;
while(scanf("%d%d%d",&n,&p,&m)!=EOF)
{
for(i = ; i <= p ;i++)
{
scanf("%d%d%d",&q[i].u,&q[i].v,&q[i].c);
q[i].u++,q[i].v++;
}
st = ;
en = n+;
int low = ,high = ,mid;
while(low<=high)
{
mid = (low+high)>>;
memset(gh,,sizeof(gh));
gh[st][] = m;
gh[n+][en] = m;
for(i = ;i <= p ; i++)
if(q[i].c<=mid)
{
gh[q[i].u][q[i].v]+=;
gh[q[i].v][q[i].u]+=;
}
if(EK()==m)
high = mid-;
else
low =mid+;
}
cout<<low<<endl;
}
return ;
}