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的边全部免费,那么此时由大 ...
随机推荐
- poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)
Description Beads of red, blue or green colors are connected together into a circular necklace of ...
- maven-Android项目环境搭建
参考:http://blog.csdn.net/earbao/article/details/40741051 android maven环境搭建: 1.Maven的版本要求3.1.1 2.设置AND ...
- Django之Cookie与Session
一.cookie 1.cookie使用 def cookie(request): print(request.COOKIES) # 获取所有的COOKIES obj = render(request, ...
- 盘点20款表现出众的HTML5游戏
不管是对用户还是开发者来说,HTML5和JavaScript游戏这几年的发展真的是件好事.随着浏览器平台的日趋成熟,并开始整合这类型游戏所 要求的技术,我们每天都能在各大应用商店和社交网站中看到越来越 ...
- maven 打包源文件
1.The source plugin can be used to create a jar file of the project sources from the command line or ...
- Dynamics CRM 常用 JS 方法集合
JS部分 拿到字段的值 var value= Xrm.Page.getAttribute("attributename").getValue(); Xrm.Page.getAttr ...
- 卸载rpm包提示:error: specifies multiple packages
–allmatches Remove all versions of the package which match PACKAGE_NAME. Normally an error is issue ...
- WIN7中组件服务中的DCOM配置找不到Microsoft Excel应用程序的解决办法
转自:http://blog.csdn.net/lploveme/article/details/8215265 在运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,但是却发现找不到Micro ...
- html5响应式布局
1.media控制布局 <link type="text/css" rel="stylesheet" href="css04.css" ...
- C#类型基础——学习笔记一
1.C#中的类型一共分两类,一类是值类型,一类是引用类型.2.结构类型变量本身就相当于一个实例.3.调用结构上的方法前,需要对其所有的字段进行赋值.4.所有元素使用前都必须初始化.5.(结构类型)ne ...