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

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

这题意有毒  题意是
n个农场  每个农场通电话需要w[i]长度的电话线  其中k根电话线免费 
费用是这样定义的  除去这k根免费的电话线 剩下的最长的电话线作为花费的费用 问求最少花费的费用是多少 就是求第k+1大线的最小
我们二分答案  x;
用dijkstra算法  假如 边值>x  边值=1  否则等于0
所以我们只有统计这这条路径大于x的个数  如果大于k个 证明x还可以变大
否则x变小
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
struct node{
int to,next,w;
bool operator<(const node &a)const{
return w>a.w;
}
}edge[N*];
int t;
int head[N];
int vis[N],dis[N];
int n;
void init(){
memset(vis,,sizeof(vis));
memset(head,-,sizeof(head));
t=;
for(int i=;i<N;i++)dis[i]=INF;
}
void add(int u,int v,int w){
edge[t].to=v;
edge[t].w=w;
edge[t].next=head[u];
head[u]=t++;
}
int Dijkstra(int x,int y){
memset(dis,INF,sizeof(dis));
dis[x]=;
node t1,t2;
t1.to=x;
priority_queue<node>q;
q.push(t1);
memset(vis,,sizeof(vis));
while(!q.empty()){
//cout<<2<<endl;
t1=q.top();
q.pop();
int u=t1.to;
if(vis[u]==)continue;
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
int tt=;
if(edge[i].w>=y)tt=;
if(vis[v]==&&dis[v]>dis[u]+tt){
dis[v]=dis[u]+tt;
t2.to=v;
t2.w=dis[v];
q.push(t2);
}
}
}
return dis[n];
}
int main(){
int k,p;
while(scanf("%d%d%d",&n,&p,&k)!=EOF){
init();
int maxx=;
for(int i=;i<p;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
maxx=max(maxx,w);
}
int low=;
int high=maxx;
int ans=;
while(low<=high){
int mid=(low+high)>>;
//cout<<Dijkstra(1,mid)<<endl;
if(Dijkstra(,mid)<=k){
high=mid-;
}
else{
ans=mid;
low=mid+;
} }
if(low>maxx){cout<<-<<endl;continue;}
cout<<ans<<endl;
//for(int i=1;i<=n;i++)cout<<dis[i]<<endl;
}
}
 

poj 3662 Telephone Lines(最短路+二分)的更多相关文章

  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 (dijstra+二分)

    题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少. 分析: 1.二分临界线(符合的情况的点在右边),找可能的 ...

  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. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...

  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+二分搜索

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

  9. POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】

    <题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...

随机推荐

  1. CSS3设计炫目字体

    阴影 .text-shadow{ text-shadow:#FF0000 0 0 10px; color:white; font-size:60px } 描边 <style> .text- ...

  2. GatewayWorker + LayIM实现即时聊天

    一.程序目录结构 二.代码展示 附LayIM开发文档:https://www.layui.com/doc/modules/layim.html 1.前端代码 <!DOCTYPE html> ...

  3. Android—修改button属性

    一般安卓里的普通按钮控件灰灰的,比较单调,我们可以给按钮加上背景图片,或者自定义按钮的圆角,颜色等属性. 下面用代码举例: <Button android:id="@+id/reset ...

  4. Vue(八)全局变量和全局方法

    一.在main.js同级目录建立一个common.js文件 // 全局变量 const globalObj = {}; // 定义公共变量 globalObj.name = '小明'; // 定义公共 ...

  5. iview表单密码自定义验证

    From中定义   ref="passwordForm" 获取dom节点  :model="passwordForm" 关联表单数据对象 :rules=&quo ...

  6. JavaScript day2(变量)

    变量(variable) 允许计算机以一种动态的形式来存储和操作数据,通过操作指向数据的指针而不是数据本身来避免了内存泄露,变量(Variable)的名字可以由数字.字母.$ 或者 _组成,但是不能包 ...

  7. 【原创】基于NodeJS Express框架开发的一个VIP视频网站项目及源码分享

    项目名称:视频网站项目 开发语言:HTML,CSS(前端),JavaScript,NODEJS(expres)(后台) 数据库:MySQL 开发环境:Win7,Webstorm 上线部署环境:Linu ...

  8. ES6学习历程(变量的声明)

    2019-01-25: 一:变量的声明: 1.对于变量的声明添加了let,const两种方式 关于let: (1)不存在变量提升--必须先声明再使用; (2)会出现暂时性死区--在一个方法外用var声 ...

  9. String s = new String("xyz");创建了几个对象?

    两个或一个都有可能 . ”xyz”对应一个对象,这个对象放在字符串常量池,常量”xyz”不管出现多少遍,都是常量池中的那一个. new String每写一遍,就创建一个新的对象,它使用常量”xyz”对 ...

  10. java堆排序实现

    代码如下: public class HeapSort { public static void heapSort(DataWrap[] data) { System.out.println(&quo ...