Secret Milking Machine
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7457 | Accepted: 2272 |
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.
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.
* 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.
Huge input data,scanf is recommended.
View Code
1 /* 2 POJ 2455 3 808k 235ms 4 5 题目要求找出网络中从给定起点到终点的不少于t条的路径,使得路径中相邻结点间的最 6 长路最短。一般的,解决多结点间的多重最值问题,尤其是最短路中的最长路等问题,都 7 要使用网络流做模型,利用最大流来判断方案的存在性,而其中的最值问题则可能会使用 8 最短路算法或者是贪心,poj2391就用到了最短路,而本题则是用到了“贪心”,即必须先 9 对所有的路按其长度递增排序。然后二分,取标号在mid之前的所有路建图,每条边的容 10 量均为1,求最大流,若最大流的大于等于t,则标号为mid的边的权值即为所求。 11 */ 12 #include <iostream> 13 #include <cstdio> 14 #include <cstring> 15 #include <algorithm> 16 #define INF 1000000000 17 #define MAX 205 18 19 using namespace std; 20 21 int N,P,T; 22 int dis[MAX]; 23 int gap[MAX]; 24 int cnt[MAX][MAX]; 25 26 struct ROAD 27 { 28 int from,to,val; 29 }; 30 31 ROAD road[MAX * MAX]; 32 33 bool cmp(ROAD a,ROAD b) 34 { 35 return a.val < b.val; 36 } 37 38 void reset(int middle) 39 { 40 memset(cnt,0,sizeof(cnt)); 41 for(int i=0; i<=middle; i++) 42 { 43 cnt[road[i].from][road[i].to]++; 44 cnt[road[i].to][road[i].from]++; 45 } 46 } 47 48 int dfs(int cur,int cur_val) 49 { 50 if(cur == N) 51 return cur_val; 52 int min_dis = N - 1, temp_val = cur_val; 53 for(int i=1; i<=N; i++) 54 { 55 if(cnt[cur][i]) 56 { 57 if(dis[i] + 1 == dis[cur]) 58 { 59 int val = min(temp_val,cnt[cur][i]); 60 val = dfs(i,val); 61 temp_val -= val; 62 cnt[cur][i] -= val; 63 cnt[i][cur] += val; 64 if(dis[1] >= N) 65 return cur_val - temp_val; 66 if(temp_val == 0) 67 break; 68 } 69 if(min_dis > dis[i]) 70 min_dis = dis[i]; 71 } 72 } 73 if(cur_val == temp_val) 74 { 75 --gap[dis[cur]]; 76 if(gap[dis[cur]] == 0) 77 dis[1] = N; 78 dis[cur] = min_dis + 1; 79 ++gap[dis[cur]]; 80 } 81 return cur_val - temp_val; 82 } 83 84 int sap() 85 { 86 memset(dis,0,sizeof(dis)); 87 memset(gap,0,sizeof(gap)); 88 int ret = 0; 89 gap[1] = N; 90 while(dis[1] < N) 91 ret += dfs(1,INF); 92 return ret; 93 } 94 95 int main() 96 { 97 while(~scanf("%d%d%d",&N,&P,&T)) 98 { 99 for(int i=0; i<P; i++) 100 scanf("%d%d%d",&road[i].from,&road[i].to,&road[i].val); 101 sort(road,road+P,cmp); 102 int low = 0,high = P-1; 103 int ans = 0; 104 while(low <= high) 105 { 106 int mid = (low+high) >> 1; 107 reset(mid); 108 int t = sap(); 109 if(t >= T) 110 { 111 ans = road[mid].val; 112 high = mid - 1; 113 } 114 else 115 low = mid + 1; 116 } 117 printf("%d\n",ans); 118 } 119 return 0; 120 }