poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)

时间:2024-05-31 10:37:26

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 ( ≤ N ≤ ,) forlorn telephone poles conveniently numbered ..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 ( ≤ Li ≤ ,,) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole  is already connected to the phone system, and pole N is at the farm. Poles  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 ( ≤ 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  if he does not need any additional cables.

Determine the minimum amount that Farmer John must pay.

Input

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

Output

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

Sample Input


Sample Output


Source

二分枚举每条边的长度l,然后以这条边的长度l为界限重新建立地图,大于l的赋值为1,小于的赋值为0,然后跑一个dijkstra求出大于l的有多少个,然后继续二分枚举。
注意一开始边要排序!!!
注意一开始要先建立个以0为界限的图,判断是否可以到达,或者0的特判!!!
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
#define inf 1<<30
#define N 10006
#define M 1006
int n,p,k;
struct Node{
int u,v,l;
}node[N];
int mp[M][M];
void build_map(int length){//建图
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
mp[i][j]=inf;
}
}
for(int i=;i<p;i++){
int a=node[i].u;
int b=node[i].v;
int c=node[i].l;
if(c>length){
mp[a][b]=mp[b][a]=;
}
else{
mp[a][b]=mp[b][a]=;
}
} } int dijkstra(int st){//dijkstra求从1到n的距离
int vis[M];
int dis[M];
for(int i=;i<=n;i++){
vis[i]=;
dis[i]=inf;
}
vis[st]=;
dis[st]=;
int x=n;
while(x--){
for(int i=;i<=n;i++){
if(dis[st]+mp[st][i]<dis[i]){
dis[i]=dis[st]+mp[st][i];
}
}
int minn=inf;
for(int i=;i<=n;i++){
if(!vis[i] && dis[i]<minn){
minn=dis[i];
st=i;
}
}
vis[st]=;
}
return dis[n];
}
bool solve(int mid){//二分搜索的判断函数
build_map(node[mid].l);
int ans=dijkstra();
if(ans<=k) return true;
return false;
}
void go(){//二分搜索
int low=;
int high=p;
while(low<high){
int mid=(low+high)>>;
if(solve(mid)){
high=mid;
}
else{
low=mid+;
}
}
printf("%d\n",node[low].l);
}
bool cmp(Node a,Node b){//排序函数!!!
return a.l<b.l;
}
int main()
{
while(scanf("%d%d%d",&n,&p,&k)==){
for(int i=;i<p;i++){
scanf("%d%d%d",&node[i].u,&node[i].v,&node[i].l);
}
sort(node,node+p,cmp);//二分搜索一定要先排序!!!???
build_map();//先以0为界限建立图
int ans=dijkstra();
//printf("%d\n",ans);
if(ans==inf){//如果不能到达,输出-1
printf("-1\n");
}
else{
if(ans<=k){//如果一开始就不用付出,则输出0
printf("0\n");
}
else{
go();//二分搜索
}
}
}
return ;
}