# [二分-最小化最大值+最短路]poj-3662

Telephone Lines
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8159 Accepted: 2942

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 {AiBi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and 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: NP, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: AiBi, 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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e3+10;
int n,p,k;
struct Edge {
int to,val;
Edge(int _to=0,int _val=0):to(_to),val(_val){}
};
vector<Edge> way[maxn],g[maxn];
bool vis[maxn];
int dist[maxn];
void spfa()
{
memset(vis,0,sizeof(vis));
for(int i=0;i<maxn;i++) dist[i] = inf;
dist[1] = 0;vis[1] = 1;
queue<int> qu;
qu.push(1);
while(!qu.empty())
{
int u = qu.front();qu.pop();
for(int i=0;i<g[u].size();i++) {
Edge e = g[u][i];
if(dist[u] + e.val < dist[e.to]) {
dist[e.to] = dist[u] + e.val;
if(!vis[e.to]) {
vis[e.to] = 1;
qu.push(e.to);
}
}
}
vis[u] = 0;
}
}
void add_edge(int u,int v,int l) {
Edge edge(v,l);
way[u].push_back(edge);
g[u].push_back(edge);
Edge edge1(u,l);
way[v].push_back(edge1);
g[v].push_back(edge1); /// 这里写错了..调了巨多遍
}
void init() {
for(int i=0;i<maxn;i++) way[i].clear(),g[i].clear();
}
bool check(int mid) {
for(int i=1;i<=n;i++) {
for(int j=0;j<way[i].size();j++) {
if(way[i][j].val > mid) g[i][j].val = 1;
else g[i][j].val = 0;
}
}
spfa();
return dist[n] <= k;
}
int main()
{
while(~scanf("%d%d%d",&n,&p,&k)) {
init();
for(int i=0;i<p;i++) {
int u,v,l;scanf("%d%d%d",&u,&v,&l);
}
spfa();
if(dist[n] == inf) {printf("-1\n");continue;}

int left = 0,right = 1e6+10;
while(left <= right) {
int mid = (left + right)>>1;
if(check(mid)) right = mid-1;
else left = mid + 1;
}
printf("%d\n",left);
}
return 0;
}


• 评论

• 上一篇
• 下一篇