Secret Milking Machine
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12324   Accepted: 3589

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. 7 9 2
  2. 1 2 2
  3. 2 3 5
  4. 3 7 5
  5. 1 4 1
  6. 4 3 1
  7. 4 5 7
  8. 5 7 1
  9. 1 6 3
  10. 6 7 3

Sample Output

  1. 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

————————————————————————————————————

题意:给定一张无向图,有n个节点p条边,要求在图中从1到n找到t条路径,并且使这t条路径中的最长边最小,输出这个最小的最长边
思路:我们可以二分枚举最小的最长边的长度,然后建图,长度小于等于枚举值的边可以连上,容量为1,建完之后跑最大流,此时最大流的意义是从1到n有几条路径,因此我们二分搜索加最大流便可以求出最长边的最小值。
注意:重边时不能用邻接矩阵保存边值,此题中也不能只取最小值,而是全部保存
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <map>
  8. #include <set>
  9. #include <stack>
  10. #include <queue>
  11. #include <vector>
  12. #include <bitset>
  13.  
  14. using namespace std;
  15.  
  16. #define LL long long
  17. const int INF = 0x3f3f3f3f;
  18. #define MAXN 500
  19.  
  20. struct node
  21. {
  22. int u, v, next, cap;
  23. } edge[MAXN*MAXN],mp[MAXN*MAXN];
  24. int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN],p[MAXN];
  25. int cnt;
  26. int ct;
  27. int n,m,k;
  28.  
  29. int N;
  30. void init()
  31. {
  32. cnt = 0;
  33. memset(s, -1, sizeof(s));
  34. }
  35.  
  36. void add(int u, int v, int c)
  37. {
  38. edge[cnt].u = u;
  39. edge[cnt].v = v;
  40. edge[cnt].cap = c;
  41. edge[cnt].next = s[u];
  42. s[u] = cnt++;
  43. edge[cnt].u = v;
  44. edge[cnt].v = u;
  45. edge[cnt].cap = c;
  46. edge[cnt].next = s[v];
  47. s[v] = cnt++;
  48. }
  49.  
  50. bool BFS(int ss, int ee)
  51. {
  52. memset(d, 0, sizeof d);
  53. d[ss] = 1;
  54. queue<int>q;
  55. q.push(ss);
  56. while (!q.empty())
  57. {
  58. int pre = q.front();
  59. q.pop();
  60. for (int i = s[pre]; ~i; i = edge[i].next)
  61. {
  62. int v = edge[i].v;
  63. if (edge[i].cap > 0 && !d[v])
  64. {
  65. d[v] = d[pre] + 1;
  66. q.push(v);
  67. }
  68. }
  69. }
  70. return d[ee];
  71. }
  72.  
  73. int DFS(int x, int exp, int ee)
  74. {
  75. if (x == ee||!exp) return exp;
  76. int temp,flow=0;
  77. for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
  78. {
  79. int v = edge[i].v;
  80. if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
  81. {
  82. edge[i].cap -= temp;
  83. edge[i ^ 1].cap += temp;
  84. flow += temp;
  85. exp -= temp;
  86. if (!exp) break;
  87. }
  88. }
  89. if (!flow) d[x] = 0;
  90. return flow;
  91. }
  92.  
  93. int Dinic_flow(int mid)
  94. {
  95. init();
  96. for(int i=1; i<=n; i++)
  97. for(int j=p[i]; ~j; j=mp[j].next)
  98. if(mp[j].cap<=mid)
  99. add(mp[j].u,mp[j].v,1);
  100. int ss=1,ee=n;
  101. int ans = 0;
  102. while (BFS(ss, ee))
  103. {
  104. for (int i = 0; i <=ee; i++) nt[i] = s[i];
  105. ans+= DFS(ss, INF, ee);
  106. }
  107. return ans;
  108. }
  109.  
  110. int main()
  111. {
  112. int u,v,c;
  113. while(~scanf("%d%d%d",&n,&m,&k))
  114. {
  115. ct=0;
  116. memset(p,-1,sizeof p);
  117. for(int i=0; i<m; i++)
  118. {
  119. scanf("%d%d%d",&u,&v,&c);
  120. mp[ct].u=u;
  121. mp[ct].v=v;
  122. mp[ct].cap=c;
  123. mp[ct].next=p[u];
  124. p[u]=ct++;
  125. }
  126. int l=0,r=INF;
  127. int ans=0;
  128. while(l<=r)
  129. {
  130. int mid=(l+r)>>1;
  131. if(Dinic_flow(mid)>=k) ans=mid,r=mid-1;
  132. else l=mid+1;
  133. }
  134. printf("%d\n",ans);
  135. }
  136. return 0;
  137. }

  

POJ2455 Secret Milking Machine的更多相关文章

  1. POJ2455 Secret Milking Machine【二分,最大流】

    题目大意:N个点P条边,令存在T条从1到N的路径,求路径上的边权的最大值最小为多少 思路:做了好多二分+最大流的题了,思路很好出 二分出最大边权后建图,跑dinic 问题是....这题是卡常数的好题! ...

  2. POJ 2455 Secret Milking Machine(搜索-二分,网络流-最大流)

    Secret Milking Machine Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9658   Accepted: ...

  3. POJ 2455 Secret Milking Machine(最大流+二分)

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...

  4. 【bzoj1733】[Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 二分+网络流最大流

    题目描述 Farmer John is constructing a new milking machine and wishes to keep it secret as long as possi ...

  5. [bzoj1733][Usaco2005 feb]Secret Milking Machine 神秘的挤奶机_网络流

    [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 题目大意:约翰正在制造一台新型的挤奶机,但他不希望别人知道.他希望尽可能久地隐藏这个秘密.他把挤奶机藏在他的农 ...

  6. BZOJ 1733: [Usaco2005 feb]Secret Milking Machine 神秘的挤奶机 网络流 + 二分答案

    Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...

  7. 【poj2455】 Secret Milking Machine

    http://poj.org/problem?id=2455 (题目链接) 题意 给出一张n个点,p条边的无向图,需要从1号节点走到n号节点一共T次,每条边只能经过1次,问T次经过的最大的边最小是多少 ...

  8. poj 2455 Secret Milking Machine 二分+最大流 sap

    题目:p条路,连接n个节点,现在需要从节点1到节点n,不重复走过一条路且走t次,最小化这t次中连接两个节点最长的那条路的值. 分析:二分答案,对于<=二分的值的边建边,跑一次最大流即可. #in ...

  9. POJ 2455 - Secret Milking Machine

    原题地址:http://poj.org/problem?id=2455 题目大意:给出一个N个点的无向图,中间有P条边,要求找出从1到n的T条通路,满足它们之间没有公共边,并使得这些通路中经过的最长的 ...

随机推荐

  1. Python全栈开发记录_第二篇(文件操作及三级菜单栏增删改查)

    python3文件读写操作(本篇代码大约100行) f = open(xxx.txt, "r", encoding="utf-8") 不写“r”(只读)默认是只 ...

  2. Windows下文件加固

    今天学到一种Windows下简单的文件加固方法,可以防止文件被(普通)删除. CMD找到要加固的文件. 例:C盘下有个 1516.txt 文本文件需要加固. 然后 copy 该文件.(注意:这里并非普 ...

  3. 62.纯 CSS 创作一只蒸锅(感觉不好看呀)

    原文地址:https://segmentfault.com/a/1190000015389338 HTML code: <!-- steamer: 蒸锅: lid: 盖子: pot: 锅 --& ...

  4. json&pickle&shelve模块

    之前我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了 ...

  5. Ado.net之对数据库的增删改查

    一.了解Command对象 1.Command对象:封装了所有对外部数据源的操作,包括增删改查和执行存储过程,并在执行完成后返回合适的结果,同Connection一样,对于不同的数据源,Ado.net ...

  6. JAVA程序调试

    调试 步骤1:设置断点(不能在空白处设置断点) 步骤2:启动调试 步骤3:调试代码(F6单步跳过)笔记本Fn+F6(F5) 步骤4:结束调试 掌握调试的好处? l  很清晰的看到,代码执行的顺序 l  ...

  7. 反射与特性与Tool编写

    大多数程序都是用来处理数据的,他们读,写,操作和显示数据,图形也是一种数据. 程序员为某种目的创建和使用一些类型,因此,在设计时必须理解所使用类型的特性. 有关程序及其类型的数据被称为元数据,他们保存 ...

  8. php 获取数组深度的key

    1.数组 深度遍历 function fun($a,&$b) { foreach ($a as $k=>$val) { if (is_array($val)) { $b[]=$k; fu ...

  9. jieba分词/jieba-analysis(java版)

    简介 支持分词模式Search模式,用于对用户查询词分词Index模式,用于对索引文档分词特性支持多种分词模式全角统一转成半角用户词典功能conf 目录有整理的搜狗细胞词库因为性能原因,最新的快照版本 ...

  10. EL表达式与标签库

    https://blog.csdn.net/panhaigang123/article/details/78428567