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的边全部免费,那么此时由大 ...
随机推荐
- HDOJ/HDU 2544 最短路---dijkstra算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...
- oracle的启动和关闭
一.sql*plus方式: 用sql*plus来连接到Oracle Sqlplus /nolog 是以不连接数据库的方式启动sql*plus Connect /as sysdba 是以DBA ...
- ant学习(1)
路径:/home/framework_Study/springinAction/webRoot/WEB-INF <?xml version="1.0" encoding=&q ...
- android码农神器 偷懒工具 android懒人框架 LoonAndroid 3 讲解
LoonAndroid 3.0 Loonandroid是一个注解框架,不涉及任何UI效果,目的是一个功能一个方法,以方法为最小颗粒度对功能进行拆解.把功能傻瓜化,简单化,去掉重复性的代码,隐藏复杂的实 ...
- android*API19
android android.accessibilityservice android.accounts android.animation android.app android.app.adm ...
- UIImageView填充模式(contentMode)
UIViewContentModeScaleToFill :拉伸填充UIImageView满屏 UIViewContentModeScaleAspectFit :拉伸到合适UIIma ...
- CUICatalog: Invalid asset name supplied:
[UIImage imageNamed:name];但是这个name却是空的,所以就报了这个错了. 解决方法,在项目中搜索UIImage imageNamed:,然后打印看看所谓的name是否为空.找 ...
- 实现VS2010整合NUnit进行单元测试
1.下载安装NUnit(最新win版本为NUnit.3.2.1.msi) http://www.nunit.org/index.php?p=download 2.下载并安装VS的Visual Nuni ...
- OpenGL ES 2.0 光照
基本的光照 光照分成了3种组成元素(3个通道):环境光.散射光以及镜面光. 材质的反射系数实际指的就是物体被照射处的颜色,散射光强度指的是散射光中的RGB(红.绿.蓝)3个色彩通道的强度. 环境光 指 ...
- poj1064 二分,注意精度!
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35269 Accepted: 7513 Des ...