POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7214 | Accepted: 2638 |
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个电线杆,有P对电线杆是可以连接的,用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电线杆N能相互通信,并且电线公司提出K条电线是可以免费使用的,当使用电线的数量超过K条,超出的电线要收费,收的总费用为去掉免费使用的K条电线之后最长的那条电线的长度。现在需要尽可能的减少费用,问最少费用是多少
解题思路:最短路+二分,二分第k+1条长的长度,然后按照二分的值用0,1处理整个图:将比二分值大的边都置为1,将比二分值小的边都置位0,然后进行找1到n的最短路。不过若1到n的边数小于等于k,答案为0
下面给出AC代码:
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include <set>
#include <map>
using namespace std;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
struct Node
{
int id,dis;
};
bool operator<(const Node &a,const Node &b)
{
return a.dis>b.dis;
}
typedef struct
{
int v,next,cost;
}Edge;
Edge e[];
int head[];
int d[];
int n,p,k;
int cnt;
inline bool BFS()
{
int vis[];
memset(vis,false,sizeof(vis));
queue<int>Q;
Q.push();
vis[]=;
while(!Q.empty())
{
int u=Q.front();
Q.pop();
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(vis[v])
continue;
vis[v]=;
if(v==n)
return ;
Q.push(v);
}
}
return ;
}
inline int Dijkstra(int ans)
{
priority_queue<Node>q;
Node temp,pp;
bool vis[];
for(int i=;i<=n;i++)
{
d[i]=1e+;
vis[i]=;
}
d[]=;
temp.id=;
temp.dis=;
q.push(temp);
while(!q.empty())
{
pp=q.top();
q.pop();
int u=pp.id;
if(vis[u])
continue;
vis[u]=;
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
int t=e[i].cost>ans?true:false;
if(!vis[v])
{
if(d[v]>d[u]+t)
{
d[v]=d[u]+t;
pp.id=v;
pp.dis=d[v];
q.push(pp);
}
}
}
}
return d[n];
}
int main()
{
int fr,to,cost;
while(scanf("%d%d%d",&n,&p,&k)!=EOF)
{
cnt=;
memset(e,,sizeof(e));
memset(head,-,sizeof(head));
for(int i=;i<p;i++)
{
fr=read();
to=read();
cost=read();
e[cnt].v=to;
e[cnt].next=head[fr];
e[cnt].cost=cost;
head[fr]=cnt;
cnt++;
e[cnt].v=fr;
e[cnt].next=head[to];
e[cnt].cost=cost;
head[to]=cnt;
cnt++;
}
if(!BFS())
{
puts("-1");
continue;
}
int l=,r=,mid;
while(l<=r)
{
mid=(l+r)/;
if(Dijkstra(mid)<=k)
r=mid-;
else
l=mid+;
}
write(r+);
printf("\n");
}
return ;
}
POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】的更多相关文章
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- poj 3662 Telephone Lines dijkstra+二分搜索
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accepted: 2071 D ...
- POJ - 3662 Telephone Lines (Dijkstra+二分)
题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- poj 3662 Telephone Lines
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7115 Accepted: 2603 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: 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 (二分 + 最短路)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
随机推荐
- DataBase MongoDB高级知识-易使用
MongoDB高级知识-易使用 mongodb是一个面向文档的数据库,而不是关系型数据库.不采用关系模型主要是为了获取更好的扩展性.当然还有其他的一些好处. 与关系型数据库相比,面向文档的数据库不再有 ...
- ABP PUT、DELETE请求错误405.0 - Method Not Allowed 因为使用了无效方法(HTTP 谓词) 引发客户端错误 No 'Access-Control-Allow-Origin' header is present on the requested resource
先请检查是否是跨域配置问题,请参考博客:http://www.cnblogs.com/donaldtdz/p/7882225.html 一.问题描述 ABP angular前端部署后,查询,新增都没问 ...
- 命令行执行php脚本 中$argv和$argc
在实际工作中有可能会碰到需要在nginx命令行执行php脚本的时候,当然你可以去配置一个conf用外网访问. 在nginx命令行中 使用 php index.php 就可以执行这个index.php脚 ...
- xamarin android alertdialog详解
说明一下:学习xamarin android一段时间,准备写一些xamarin android相关的例子,alertdialog也是使用的非常多得空间之一,非常感谢鸟巢上的小猪,我也是看着他写的教程学 ...
- rtmp流媒体搭建的所需安装包
说明:这是基于nginx rtmp控件 搭建的rtmp流媒体服务器,在此附上的是搭建所需要的安装包,具体的搭建过程看我之前的"ubuntu流媒体搭建" 链接地址:http://p ...
- mintUI配合vue2.0,webpack,vue-cli脚手架从零搭建
步骤说明: 1.确保安装了vue-cli 安装:cnpm install vue-cli -g 验证版本:vue --version 2.生成项目模板: vue init webpack-simple ...
- shell脚本-批量执行机器命令
场景:通过跳板机,批量获取线上机器日志 使用方式:run2 host 'ls -al /home/admin/' #! /bin/sh USER_NAME=$USER if [ $# -ne 2 ]; ...
- C#实现冲顶大会辅助工具 (截图+图像识别+搜索)
前两天在博客园看到 .NET开发一个微信跳一跳辅助程序, 原来可以通过C#连接手机操作.正好朋友圈有人分享"冲顶大会".冲顶大会是一个在线答题APP.每次12道题,每道题有10秒钟 ...
- Benchmark Test On Android Devices
一.Android设备上的Benckmark测试概述 同PC相比,在Android设备上的性能测试还没有一个公认的标准.也没有PC上那么多的测试程序集.但我们可以通过一些工具所得到的信息更好的了解设备 ...
- java多线程(一)-概述
最近这段在看java多线程编程方面的东西.所以特写了几篇文章,来总结和回顾一下自己所学习到的相关知识.因为水平有限,文章中总结不全面甚至理解错误的地方,欢迎读者指点批评. 我们平时所接触到的程序,都是 ...