POJ2455

来源:互联网 发布:mysql源码包下载地址 编辑:程序博客网 时间:2024/05/16 10:07

Secret Milking Machine
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 10308 Accepted: 3028

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 21 2 22 3 53 7 51 4 14 3 14 5 75 7 11 6 36 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.

二分 + 网络流


题目大意为:给定N个点P条边,要求找T条从1点到N点的路径,图中每条边只能使用一次,求满足条件的路径中的最长边的最小值。

观察到每条边只能使用一次,我们考虑建立网络流模型,再二分最长边的值 Limit。

将源点和1连边,N和汇点连边,容量均为T;将原图中权值 <= Limit 的边容量定为1,否则定为0.

求最大流,看流量是否为T,若为T则最长边权值 <= Limit则可以继续二分。

PS:原图为无向图,只需在网络中建立两条边,且不会存在着这两条边对总流量贡献为2的情况。

若对流量贡献为2,则在最大流中该边会经过两次,那么完全不需要经过该边,也就是该路径贡献2的流量跟该边无关。


代码如下:

/* * @Author: duyixian* @Date:   2015-04-20 18:40:15* @Last Modified by:   duyixian* @Last Modified time: 2015-04-20 19:26:31*/#include "cstdio"#include "cstdlib"#include "iostream"#include "algorithm"#include "queue"#include "cstring"using namespace std;#define MAX_SIZE 305#define INF 0x3F3F3F3Fstruct Path{int From, To, Value;}Paths[MAX_SIZE * MAX_SIZE];struct Edge{int To, Next, C;}Edges[MAX_SIZE * MAX_SIZE * 4];int Total, N, P, t, Limit, S, T;int Front[MAX_SIZE], Distance[MAX_SIZE];inline void Add_Edge(int From, int To, int C){++Total;Edges[Total].To = To;Edges[Total].C = C;Edges[Total].Next = Front[From];Front[From] = Total;}inline void Add_Edges(int From, int To, int C){Add_Edge(From, To, C);Add_Edge(To, From, 0);}inline bool BFS(){memset(Distance, 0, sizeof(Distance));Distance[S] = 1;queue<int> Queue;Queue.push(S);while(!Queue.empty()){int Now = Queue.front();Queue.pop();for(int temp = Front[Now]; temp; temp = Edges[temp].Next){if(!Distance[Edges[temp].To] && Edges[temp].C){Distance[Edges[temp].To] = Distance[Now] + 1;Queue.push(Edges[temp].To);}}}return Distance[T];}int DFS(int Now, int In){if(Now == T)return In;int Rest = In;for(int temp = Front[Now]; temp; temp = Edges[temp].Next){if(Edges[temp].C && Distance[Edges[temp].To] == Distance[Now] + 1){int Increment;Increment = DFS(Edges[temp].To, min(Rest, Edges[temp].C));Rest -= Increment;Edges[temp].C -= Increment;Edges[temp ^ 1].C += Increment;if(!Rest)return In;}}if(Rest == In)Distance[Now] = 0;return In - Rest;}inline void Build(){memset(Front, 0, sizeof(Front));Total = 1;S = 0;T = N + 1;Add_Edges(S, 1, t);Add_Edges(N, T, t);for(int i = 1; i <= P; ++i){if(Paths[i].Value <= Limit){Add_Edges(Paths[i].From, Paths[i].To, 1);Add_Edges(Paths[i].To, Paths[i].From, 1);}}}int Max_Flow(){int Ans = 0;Build();while(BFS()){Ans += DFS(S, INF);}return Ans;}int main(){freopen("input.txt", "r", stdin);freopen("output.txt", "w",stdout);cin >> N >> P >> t;for(int i = 1; i <= P; ++i)scanf("%d%d%d", &Paths[i].From, &Paths[i].To, &Paths[i].Value);int Left = 0, Right = 1000005, Mid = (Left + Right) / 2;while(Mid != Left && Mid != Right){int Flow;Limit = Mid;Flow = Max_Flow();if(Flow == t){Right = Mid;Mid = (Left + Right) / 2;}else{Left = Mid;Mid = (Left + Right) / 2;}}cout << Right;fclose(stdout);fclose(stdin);return 0;}


0 0
原创粉丝点击