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
题意:
给你n个点,m条边,k条可以免费的边。让你从1~n的所有路径中选择一条路径使得删除k条边之后该路径上的最大值最小。
思路:一开始完全没有什么想法,不知道怎么做。然后看了一下别人,瞬间就知道了,原来二分还可以这么用的,因为剩下的图中所有边的最大值越小,删的边相应的就越多,越容易达到k。这样我们可以二分答案(mid)。check的时候对于大于mid的边我们记为边权为1,其余的为0.求一次1~n的最短路之后,看dis[n]是不是小于等于k就可以了。小于等于k说明满足条件,这时让mid减小,否则让mid增加。神奇的二分。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int MAXN = 1e3+5;
const int inf = 1e9;
int n,m,k;
struct node
{
int v,w;
node(int a,int b){v = a; w = b;}
};
vector<node>head[MAXN];
int dis[MAXN];
bool vis[MAXN];
bool check(int mid)
{
for(int i = 1;i <= n; ++i)
{
vis[i] = 0;
dis[i] = inf;
}
deque<int>q;
int cnt = 1,sum = 0;
q.push_back(1);
dis[1] = 0;
while(!q.empty())
{
int u = q.front();
q.pop_front();
if(dis[u]*cnt > sum)
{
q.push_back(u);
continue;
}
vis[u] = 0;
cnt--,sum -= dis[u];
for(int i = 0,l = head[u].size(); i < l; ++i)
{
int v = head[u][i].v;
int w = head[u][i].w;
w = w>mid?1:0;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(!vis[v])
{
vis[v] = 1;
cnt++,sum += dis[v];
if(q.empty() || dis[v] < dis[q.front()])q.push_front(v);
else q.push_back(v);
}
}
}
}
if(dis[n] <= k)return 1;
return 0;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
int u,v,w;
for(int i = 0; i < m; ++i)
{
scanf("%d%d%d",&u,&v,&w);
head[u].push_back(node(v,w));
head[v].push_back(node(u,w));
}
int low = 0,high = 1000000;
int ans = -1,mid;
while(low <= high)
{
mid = (low+high)>>1;
if(check(mid))
{
ans = mid;
high = mid - 1;
}
else low = mid + 1;
}
printf("%d\n",ans);
return 0;
}