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 ;
}

poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)的更多相关文章

  1. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  2. poj 3662 Telephone Lines dijkstra+二分搜索

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5696   Accepted: 2071 D ...

  3. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  4. poj 3662 Telephone Lines spfa算法灵活运用

    意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...

  5. poj 3662 Telephone Lines

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7115   Accepted: 2603 D ...

  6. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  7. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  8. POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)

    题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...

  9. POJ - 3662 Telephone Lines (Dijkstra+二分)

    题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...

随机推荐

  1. hdu 5410 CRB and His Birthday(混合背包)

    Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...

  2. pyqt一个小例子

    # -*- coding: utf-8 -*- __author__ = 'Administrator' from PyQt4 import Qt,QtCore,QtGui import sys,ra ...

  3. AJAX上传文件

    function up_files() { var fileSelect = document.getElementById('file-select'); var files = fileSelec ...

  4. 【Nginx】事件和连接

    不同的操作系统相应不同的事件驱动机制.在Linux 2.6之后使用epoll机制.相应的事件驱动模块是ngx_epoll_module.Nginx的ngx_event_core_module模块依据操 ...

  5. Hacker(19)----检测Windows系统漏洞

    想完全掌握Windows中存在的漏洞需要使用专业的漏洞扫描软件.目前常用的有MBSA(MircosoftBaselineSecurityAnalyzer).360安全卫士等. 一.使用MBSA检测系统 ...

  6. Hacker(17)----认识Windows系统漏洞

    Windows系统是迄今为止使用频率最高的操作系统,虽然其安全性随着版本的更新不断提高,但由于人为编写的缘故始终存在漏洞和缺陷.但Mircosoft公司通过发布漏洞补丁来提高系统的安全性,使Windo ...

  7. OC基础 单例

    #undef  AS_SINGLETON   #define AS_SINGLETON( __class ) \       + (__class *)sharedInstance;      #un ...

  8. DTO学习系列之AutoMapper(四)

    本篇目录: Mapping Inheritance-映射继承 Queryable Extensions (LINQ)-扩展查询表达式 Configuration-配置 Conditional Mapp ...

  9. python字符串的encode和decode

    原文 decode的作用是将其他编码的字符串转换成unicode编码. str1.decode('gb2312') #表示将gb2312编码的字符串转换成unicode编码 encode的作用是将un ...

  10. c#类似单片机的8bit或运算

    1.正确 PWMSUBM0 &= (byte)(PWMSUBM0 | 0xfc); PWMSUBM0 &= (byte)(PWMSUBM0 | (byte)0xfc); 2.不能编译的 ...