# [POJ 2455] Secret Milking Machine【二分答案+网络流】

• 2018-01-18
• 0
• 0

## Problem:

 Time Limit: 1000MS Memory Limit: 65536K

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

```7 9 2
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

`5`

Hint

Farmer John can travel trails 1 - 2 - 3 - 7 and 1 - 6 - 7. None of the trails travelled exceeds 5 units in length. It is impossible for Farmer John to travel from 1 to 7 twice without using at least one trail of length 5.

Huge input data,scanf is recommended.

Source

USACO 2005 February Gold

## Code: O(N4logN) [1284K, 219MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline void getint(int &num){
char ch;
while(!isdigit(ch = getchar()));
num = ch - '0';
while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
}

inline int min(const int &a, const int &b) {return a < b ? a : b;}

int N, P, T;
int cap;

struct Trail{
int s, t, l;

inline bool operator < (const Trail &ed2) const {return l < ed2.l;}
} ed;

int q, fr, re;
int lev, arc;

inline bool Dinic_BFS(){
memset(lev, -1, sizeof(lev));
fr = re = 0, q[re++] = 1, lev = 0;
while(fr != re){
int u = q[fr++];
for(register int i = 1; i <= N; i++)
if(cap[u][i] && lev[i] < 0)
lev[i] = lev[u] + 1, q[re++] = i;
}
return lev[N] > -1;
}

inline int Dinic_DFS(int u, int curflow){
if(u == N) return curflow;
int remflow = curflow;
for(register int i = arc[u]; i <= N; i++)
if(cap[u][i]){
arc[u] = i;
if(lev[i] == lev[u] + 1){
int dflow = Dinic_DFS(i, min(remflow, cap[u][i]));
if(dflow){
cap[u][i] -= dflow, cap[i][u] += dflow;
remflow -= dflow;
}
}
}
return curflow - remflow;
}

inline int Dinic(){
int maxflow = 0;
while(Dinic_BFS()){
for(register int i = 1; i <= N; i++) arc[i] = 1;
maxflow += Dinic_DFS(1, 0x3f3f3f3f);
}
return maxflow;
}

inline bool check(int keyid){
memset(cap, 0, sizeof(cap));
for(register int i = 1; i <= keyid; i++)
cap[ed[i].s][ed[i].t]++, cap[ed[i].t][ed[i].s]++;
int maxflow = Dinic();
return maxflow >= T;
}

int main(){
getint(N), getint(P), getint(T);
for(register int i = 1; i <= P; i++)
getint(ed[i].s), getint(ed[i].t), getint(ed[i].l);
sort(ed + 1, ed + P + 1);
int lft = 1, rt = P;
while(lft < rt){
int mid = lft + rt >> 1;
if(check(mid)) rt = mid;
else lft = mid + 1;
}
printf("%d\n", ed[lft].l);
return 0;
}
```

#### 评论

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai