1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 236 Solved: 150
[ Submit][ Status][ Discuss]
Description
Farmer John is constructing a new milking machine and wishes to keep it secret as long as possible. He has hidden in it deep within his farm and needs to be able to get to the machine without being detected. He must make a total of T (1 <= T <= 200) trips to the machine during its construction. He has a secret tunnel that he uses only for the return trips. The farm comprises N (2 <= N <= 200) landmarks (numbered 1..N) connected by P (1 <= P <= 40,000) bidirectional trails (numbered 1..P) and with a positive length that does not exceed 1,000,000. Multiple trails might join a pair of landmarks. To minimize his chances of detection, FJ knows he cannot use any trail on the farm more than once and that he should try to use the shortest trails. Help FJ get from the barn (landmark 1) to the secret milking machine (landmark N) a total of T times. Find the minimum possible length of the longest single trail that he will have to use, subject to the constraint that he use no trail more than once. (Note well: The goal is to minimize the length of the longest trail, not the sum of the trail lengths.) It is guaranteed that FJ can make all T trips without reusing a trail.
Input
* Line 1: Three space-separated integers: N, P, and T * Lines 2..P+1: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, indicating that a trail connects landmark A_i to landmark B_i with length L_i.
Output
* Line 1: A single integer that is the minimum possible length of the longest segment of Farmer John's route.
Sample Input
1 2 2
2 3 5
3 7 5
1 4 1
4 3 1
4 5 7
5 7 1
1 6 3
6 7 3
Sample Output
约翰选择1-2-3-7和1-6-7两条路线.这些路线中最长路的最小长度是5
HINT
Source
题解:二分+最大流
二分一个最长路中的最小长度x,然后将所有长度<=x的边加入网络流的图中,每条边的容量为1.判断最后的答案是否>=k
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define inf 1000000000
#define N 1000003
using namespace std;
int point[N],v[N],nxt[N],remain[N],deep[N],cur[N];
int x[N],y[N],len[N],n,m,k,tot,last[N],num[N];
void add(int x,int y,int z)
{
tot++; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
tot++; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
int addflow(int s,int t)
{
int ans=inf; int now=t;
while (now!=s) {
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s) {
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
void bfs(int s,int t)
{
for (int i=1;i<=n;i++) deep[i]=t;
queue<int> p; p.push(t);
deep[t]=0;
while (!p.empty()) {
int now=p.front(); p.pop();
for (int i=point[now];i!=-1;i=nxt[i])
if (deep[v[i]]==t&&remain[i^1])
deep[v[i]]=deep[now]+1,p.push(v[i]);
}
}
int isap(int s,int t)
{
int now=s; int ans=0;
bfs(s,t);
for (int i=1;i<=n;i++) num[deep[i]]++;
for (int i=1;i<=n;i++) cur[i]=point[i];
while (deep[s]<t) {
if (now==t) {
ans+=addflow(s,t);
now=s;
}
bool pd=false;
for (int i=cur[now];i!=-1;i=nxt[i])
if (deep[v[i]]+1==deep[now]&&remain[i]) {
cur[now]=i;
last[v[i]]=i;
pd=true;
now=v[i]; break;
}
if (!pd) {
int minn=t;
for (int i=point[now];i!=-1;i=nxt[i])
if (remain[i]) minn=min(minn,deep[v[i]]);
if (!--num[deep[now]]) break;
num[deep[now]=minn+1]++;
cur[now]=point[now];
if (now!=s) now=v[last[now]^1];
}
}
return ans;
}
bool pd(int x1)
{
tot=-1;
memset(num,0,sizeof(num));
memset(point,-1,sizeof(point));
for (int i=1;i<=m;i++)
if (len[i]<=x1) add(x[i],y[i],1),add(y[i],x[i],1);
return isap(1,n)>=k;
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
scanf("%d%d%d",&n,&m,&k);
int mx=0;
for (int i=1;i<=m;i++)
scanf("%d%d%d",&x[i],&y[i],&len[i]),mx=max(mx,len[i]);
int l=0; int r=1e6+1; int ans=r;
while (l<=r) {
int mid=(l+r)/2;
if (pd(mid)) ans=min(ans,mid),r=mid-1;
else l=mid+1;
}
printf("%d\n",ans);
}