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 ...
随机推荐
- 4.前端基于react,后端基于.net core2.0的开发之路(4) 前端打包,编译,路由,模型,服务
1.简要的介绍 学习react,首先学习的就是javascript,然后ES6,接着是jsx,通常来说如果有javascript的基础,上手非常快,但是真正要搭建一个前端工程化项目,还是有很多坑的 搞 ...
- JS判断终端
//判断手机终端 if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) { window.location.href = 'mobil ...
- MySQL 使用经验
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/10 在索引中完成排序 SELECT thread_id FROM ...
- 调用CMD命令的一个.NET工具类(MyWindowsCmd)
功能大概描述一下如果直接StandardOutput.ReadToEnd()这种方法,有很多限制 这类方式必须把命令全部执行一次写入并标记为exit,而且返回内容的获取会一直等待,如果在主线程里使用会 ...
- Func和Action委托简单用法
Func和Action类是特殊的类型,它们允许你在不必指定自定义委托类型的情况下,去使用委托.在整个.NET框架中都可以使用它们.例如,在我们考察并行计算时,你也会看到这两个类的示例. 上面一段文字是 ...
- 实体框架(Entity Frmaework)简介
l简称EF NH l与Asp.Net MVC关系与ADO.NET关系 lADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapp ...
- python 算法学习部分代码记录篇章1
# -*- coding: utf-8 -*- # @Date : 2017-08-19 20:19:56 # @Author : lileilei '''那么算法和数据结构是什么呢,答曰兵法''' ...
- 理解JavaScript原型
Javascript原型总会给人产生一些困惑,无论是经验丰富的专家,还是作者自己也时常表现出对这个概念某些有限的理解,我认为这样的困惑在我们一开始接触原型时就已经产生了,它们常常和new.constr ...
- ubuntu16编译安装mysql5.7
安装环境: linux版本: ubuntu-16.04.3-desktop-amd64 mysql版本:mysql-boost-5.7.20.tar.gz 下载地址:https://dev.mysql ...
- linux apache虚拟主机配置(基于ip,端口,域名)
配置环境: linux版本:Centos6.4 httpd版本: [root@centos64Study init.d]# pwd/etc/init.d[root@centos64Study init ...