写在前面

又是好久没写博客(
主要是最近忙着算法语法复活 没什么时间也没什么实力刷OJ
现在总算是有几道题能写

Description

Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.

There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.

The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.

As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.

Determine the minimum amount that Farmer John must pay.

Input

  • Line 1: Three space-separated integers: N, P, and K
  • Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li

Output

  • Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.

Sample Input

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

Sample Output

4

Source

USACO 2008 January Silver

题解

这题问的有点像最短路
Michael_Bryant曾经出过一道问路径最大值最小的题
那道题无非就是把spfadijkstra+heap的松弛操作从加和变成取max就行
但是这道题还有k个权值为0的边
所以这样做显然行不通
考虑二分一个边权mid 然后我们让边权大于mid的边权值变为1 小于或者等于mid的边权值变为0 这样我们跑一个最短路之后去看1-n的最短路是否小于等于k 显然若小于等于k那么这个mid就是合理的
这样的话我们通过二分就可以找到答案了
在做最短路的时候我们可以采用0-1最短路进行求解
思路类似于dijkstra+heap 只不过我们用一个双端队列 而后我们将该边权值是0的松弛推至队头 权值是1的松弛推至队尾 这样相当于也维护了一个堆一样的东西 并且保证了每次更新的时候答案是最短的 所以当我们第一次到达的时候就一定是答案了

代码