poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
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
#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)的更多相关文章
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj 3662 Telephone Lines dijkstra+二分搜索
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accepted: 2071 D ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
- poj 3662 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7115 Accepted: 2603 D ...
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- POJ 3662 Telephone Lines (分层图)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6785 Accepted: 2498 D ...
- POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)
题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...
- POJ - 3662 Telephone Lines (Dijkstra+二分)
题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...
随机推荐
- JavaScript闭包函数的写法
<script type="text/javascript"> //通过js内置的函数构造器创建函数 var func=new Function('a','b','re ...
- pyqt之倒计时例子
from PyQt4.Qt import *from PyQt4.QtCore import *from PyQt4.QtGui import *import sysdef main(): a= ...
- AAM(Active Appearance Model)算法介绍
前面介绍ASM算法(http://blog.csdn.net/carson2005/article/details/8194317)的时候,笔者提到,ASM是基于统计形状模型的基础上进行的,而AAM则 ...
- LoadRunner监控windows资源报错Monitor name :Windows Resources. Cannot connect to machine
目标机:被监控的机器,windows server 2008 R2. 测试机:执行control的机器,windows7 操作:在测试机上执行Control,添加windows的监控 问题现象:Mon ...
- [转]Sql Server 2005中的架构(Schema)、用户(User)、登录(Login)和角色(Role)
每一个概念的产生必然是因为碰到了无法解决的问题.换句话说,如果没有它,必然会导致某些问题难以解决.所以我想从这个角度切入,希望能把这几个复杂而暧昧的多角关系从最实用的角度来阐述清楚. 在问题的最初,我 ...
- 理解SQL SERVER中的分区表(转)
简介 分区表是在SQL SERVER2005之后的版本引入的特性.这个特性允许把逻辑上的一个表在物理上分为很多部分.而对于SQL SERVER2005之前版本,所谓的分区表仅仅是分布式视图,也就是多个 ...
- 常用SQL语句学习整理
增 insert into table_name (column_name1,column_name2) values (value1,value2) 删 delete from table_name ...
- 数据库分库分表(sharding)系列(五) 一种支持自由规划无须数据迁移和修改路由代码的Sharding扩容方案
作为一种数据存储层面上的水平伸缩解决方案,数据库Sharding技术由来已久,很多海量数据系统在其发展演进的历程中都曾经历过分库分表的Sharding改造阶段.简单地说,Sharding就是将原来单一 ...
- pfile,spfile 初始化参数文件顺序【weber出品】
一.初始化参数文件 启动实例时会读取初始化参数文件.参数文件有两种类型: 1.服务器参数文件:这是首选类型的初始化参数文件.这是一个由数据库服务器写入或读取的二进制文件,不得手动进行编辑.此文件驻留在 ...
- C# 程序打包
1:新建安装部署项目打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装项目,然后点击确定.(详细见下图) 此主题相关图片如下: 2:开始打包2.1 确定即可进入项目文件夹:双 ...