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 ...
随机推荐
- 统计函数:MAX,MIN,SUM,AVG,COUNT
- 运算符关键字。数据区别大小写。日期范围。判空的两种写法。NOT IN的两种写法。IN范围可含NULL,但NOT IN值范围不能含NULL。
比较:>,<,=,>=,<=,<>(!=) 逻辑:AND,OR,NOT 范围:BETWEEN...AND... 范围:IN,NOT IN 判空:IS NULL, I ...
- SPCircleView的使用(圆心向四周扩散动画)
今天封装了一个动画,想着以后可能会用,就封装了一下.欢迎下载 https://github.com/USimpleLife/SPCircleView 参数说明 @param centerPoint 中 ...
- JS画几何图形之六【过直线外一点作垂线】
样例:http://www.zhaojz.com.cn/demo/draw10.html 依赖:[点].[直线] //过直线外一点画垂线 function drawVerticalLine(point ...
- ELK日志检索并邮件微信通知
简介 脚本为通过api检索日志内容,并通过邮件或者微信发送出来. 脚本 index检索脚本 #!/usr/bin/env python # coding:utf-8 from elasticsearc ...
- CSS3媒体查询(Media Queries)介绍
媒体类型 all 所有设备 screen 电脑显示器 handheld 便携设备 tv 电视类型设备 print 打印用纸打印预览视图 关键字 and not(排除某种设备) only(限定某种设备) ...
- c#代码技巧
1.#region #endregion 1.#region 是一个分块预处理命令,主要用于编辑代码分段,在编译时会自动屏蔽,同时该指令可以使代码在VS代码编辑器中折叠或展开: 2.#region必须 ...
- 【Python3之正则re】
一.正则re 1.正则表达式定义 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Pytho ...
- IE下javascript cookie path设置Bug
项目中设置完cookie,在Firefox下顺利测试通过.IE测试出现问题,经定位发现是Javascript设置 Cookie 时的 path 有问题.IE下Cookie设置在 /或者URL所在路径时 ...
- JDK1.8中HashMap实现
JDK1.8中的HashMap实现跟JDK1.7中的实现有很大差别.下面分析JDK1.8中的实现,主要看put和get方法. 构造方法的时候并没有初始化,而是在第一次put的时候初始化 putVal方 ...