码迷,mamicode.com
首页 > 其他好文 > 详细

poj2455Secret Milking Machine【二分 + 最大流】

时间:2014-06-07 05:34:18      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

Secret Milking Machine
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9129   Accepted: 2742

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

 
大意:
有跟人发明了一种机器但是不想被发现,他需要将机器从起点到终点运送T次,问路径上最长路径的最小值(不是和,而是两点之间距离) 
思路:
二分 + 最大流
low = min_len , high = max_len
while(low < high) {
  mid = (low + high) >> 1;
  if(MaxFlow == T) 
    high = mid - 1;
  else
    low = mid + 1;
}
END
代码:
bubuko.com,布布扣
  1 #include <iostream>
  2 #include <queue>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <vector>
  6 using namespace std;
  7 
  8 const int maxn = 40005;
  9 const int INF = 1000000000;
 10 
 11 struct Edge {
 12     int from, to, cap, flow;
 13 };
 14 
 15 struct Dinic
 16 {
 17     int n, m, s, t;
 18     vector<Edge> edges;
 19     vector<int> G[maxn];
 20     bool vis[maxn];
 21     int d[maxn];
 22     int cur[maxn];
 23 
 24     void ClearAll(int n) {
 25         for(int i = 0; i <= n; i++) G[i].clear();
 26         edges.clear();
 27     }
 28 
 29     void AddEdge(int from, int to, int cap) {
 30         edges.push_back((Edge) { from, to, cap, 0});
 31         edges.push_back((Edge) { to, from, 0, 0} );
 32         m = edges.size();
 33         G[from].push_back(m - 2);
 34         G[to].push_back(m - 1);
 35     }
 36 
 37     bool BFS() {
 38         memset(vis, 0, sizeof(vis));
 39         queue<int> Q;
 40         Q.push(s);
 41         d[s] = 0;
 42         vis[s] = 1;
 43         while(!Q.empty()) {
 44             int x = Q.front(); Q.pop();
 45             for(int i = 0; i < G[x].size(); i++) {
 46                 Edge& e = edges[G[x][i]];
 47                 if(!vis[e.to] && e.cap > e.flow) {
 48                     vis[e.to] = 1;
 49                     d[e.to] = d[x] + 1;
 50                     Q.push(e.to);
 51                 }
 52             }
 53         }
 54         return vis[t];
 55     }
 56 
 57     int DFS(int x, int a) {
 58         if(x == t || a == 0) return a;
 59         int flow = 0, f;
 60         for(int& i = cur[x]; i < G[x].size(); i++) {
 61             Edge& e = edges[G[x][i]];
 62             if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
 63                 e.flow += f;
 64                 edges[G[x][i]^1].flow -= f;
 65                 flow += f;
 66                 a -= f;
 67                 if(a == 0) break;
 68             }
 69         }
 70         return flow;
 71     }
 72 
 73     int MaxFlow(int s, int t) {
 74         this -> s = s; this -> t = t;
 75         int flow = 0;
 76         while(BFS()) {
 77             memset(cur, 0, sizeof(cur));
 78             flow += DFS(s, INF);
 79         }
 80         return flow;
 81     }
 82 };
 83 
 84 Dinic g;
 85 
 86 int N, P, T, s, t;
 87 
 88 struct Point {
 89     int x, y, z;
 90 }point[maxn];
 91 
 92 bool check(int mid) {
 93     //printf("*%d\n",mid);
 94     g.ClearAll(maxn);
 95     g.AddEdge(s, 1, T);
 96     g.AddEdge(N, t, T);
 97     for(int i = 0; i < P; i++) {
 98         if(point[i].z <= mid) {
 99             g.AddEdge(point[i].x, point[i].y, 1);
100             g.AddEdge(point[i].y, point[i].x, 1);
101             //printf("#%d %d %d\n",point[i].x, point[i].y, point[i].z);
102         }
103     }
104     int ans = g.MaxFlow(s, t);
105     //printf("%d\n",ans);
106     if(ans != T) return true;
107     else return false;
108 }
109 
110 int main() {
111     //freopen("a.txt","r",stdin);
112     while(EOF != scanf("%d %d %d",&N, &P, &T)) {
113         s = 0; t = N + 1;
114         //printf("%d %d\n",s, t);
115         int max_num = -INF, min_num = INF;
116         for(int i = 0; i < P; i++) {
117             scanf("%d %d %d",&point[i].x, &point[i].y, &point[i].z);
118             if(max_num < point[i].z) max_num = point[i].z;
119             if(min_num > point[i].z) min_num = point[i].z;
120         }
121 
122         int low = min_num; int high = max_num;
123         while(low <= high) {
124             int mid = (low + high) >> 1;
125             //printf("*%d\n",mid);
126             if(check(mid))
127                 low = mid + 1;
128             else
129                 high = mid - 1;
130         }
131         printf("%d\n",high + 1);
132     }
133     return 0;
134 }
View Code

 

poj2455Secret Milking Machine【二分 + 最大流】,布布扣,bubuko.com

poj2455Secret Milking Machine【二分 + 最大流】

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/zhanzhao/p/3755754.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!