题目大意:给定一个 N 个顶点,M 条边的无向图,求一条从 1 号节点到 N 号节点之间的路径,使得第 K+1 大的边权最小,若 1 与 N 不连通,输出 -1。

最小化最大值一类的问题,采用二分答案即可,每次跑一遍 dij ,若边权大于二分的值,那么等效边权为1,否则边权为0,最后判断从 1 到 N 之间的最短路是否大于 K 即可。

代码如下

#include <cstdio>
#include <algorithm>
#include <memory.h>
#include <queue>
using namespace std;
const int maxv=1e3+10;
const int maxe=1e4+10; struct node{
int nxt,to,w;
node(int x=0,int y=0,int z=0):nxt(x),to(y),w(z){}
}e[maxe<<1];
int tot=1,head[maxv]; int n,m,k,dis[maxv],mx;
bool vis[maxv]; void add_edge(int from,int to,int w){
e[++tot]=node(head[from],to,w),head[from]=tot;
} void read_and_parse(){
scanf("%d%d%d",&n,&m,&k);
for(int i=1,from,to,w;i<=m;i++){
scanf("%d%d%d",&from,&to,&w);
add_edge(from,to,w),add_edge(to,from,w);
mx=max(mx,w);
}
} typedef pair<int,int> P; bool right(int x){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue<P> q;
dis[1]=0,q.push(make_pair(0,1));
while(q.size()){
int u=q.top().second;q.pop();
if(vis[u])continue;
if(u==n)break;
vis[u]=1;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=(e[i].w>x?1:0);
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
q.push(make_pair(-dis[v],v));
}
}
}
return dis[n]<=k;
} bool dij(){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
priority_queue<P> q;
dis[1]=0,q.push(make_pair(0,1));
while(q.size()){
int u=q.top().second;q.pop();
if(vis[u])continue;
if(u==n)break;
vis[u]=1;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=e[i].w;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
q.push(make_pair(-dis[v],v));
}
}
}
return dis[n]==0x3f3f3f3f;
} void solve(){
int l=0,r=mx;
while(l<r){
int mid=l+r>>1;
if(right(mid))r=mid;
else l=mid+1;
}
printf("%d\n",l);
} int main(){
read_and_parse();
if(dij())return puts("-1"),0;
solve();
return 0;
}

【POJ3662】Telephone Lines dij + 二分答案的更多相关文章

  1. POJ3662 Telephone Lines( dijkstral + 二分 )

    POJ3662 Telephone Lines 题目大意:要在顶点1到顶点n之间建一条路径,假设这条路径有m条边,其中有k条边是免费的,剩余m-k条边是要收费的, 求这m-k条边中花费最大的一条边的最 ...

  2. POJ3662 [USACO08JAN]Telephone Lines (二分答案/分层图求最短路)

    这道题目有两种解法: 1.将每个点视为一个二元组(x,p),表示从起点到x有p条路径免费,相当于构建了一张分层图,N*k个节点,P*k条边.在这张图上用优先队列优化的SPFA算法求解,注意这里的d数组 ...

  3. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

  4. POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】

    <题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...

  5. P1948 [USACO08JAN]电话线Telephone Lines(二分答案+最短路)

    思路 考虑题目要求求出最小的第k+1大的边权,想到二分答案 然后二分第k+1大的边权wx 把所有边权<=wx的边权变为0,边权>wx的边权变为0,找出最短路之后,如果dis[T]<= ...

  6. poj3662 Telephone Lines【最短路】【二分】

    http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. POJ-3662 Telephone Lines 二分+双端队列

    题目传送门 题意:有n个点, p条路,每条道路有个花费Li, 然后现在要建一条1-n的路线,然后可以选k条道路免费, 然后可以在剩下的道路中选择价格最高的边支付费用, 求这个答案最小. 题解: 二分答 ...

  8. POJ - 3662 Telephone Lines (Dijkstra+二分)

    题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...

  9. poj-3662 Telephone Lines 二分答案+最短路

    链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...

随机推荐

  1. RabbitMQ --- Hello Mr.Tua

    目录 RabbitMQ --- Work Queues(工作队列) RabbitMQ --- Publish/Subscribe(发布/订阅) RabbitMQ --- Routing(路由) 安装环 ...

  2. Tomcat通过自带的Cluster方式实现Session会话共享环境操作记录

    一般来说,在多个tomcat集群业务中,session会话共享是必须的需求,不然前端nginx转发过来的请求不知道之前请求在哪台tomcat节点上,从而就找不到session以至于最终导致请求失败.要 ...

  3. Individual P1: Preparation

    Individual Project - Word frequency program tally the frequency of words under a directory (2 modes) ...

  4. 软件工程附加篇章:进阶四则运算和Core对接

    0x01 :计算模块(Core)和前端对接 首先特别结对编程刘乾组(SivilTaram)提供的计算模块(Core),http://www.cnblogs.com/SivilTaram/p/48599 ...

  5. Linux内核及分析 第八周 进程的切换和系统的一般执行过程

    学习笔记: 一.进程调度与进程调度的时机分析 1.不同类型的进程有不同需求的调度需求: 第一种分类: —I/O-bound:频繁的进行I/O,通常会花费很多时间等待I/O操作的完成 —CPU-boun ...

  6. ELF文件格式分析

    一般的 ELF 文件包括三个索引表:ELF  header,Program  header  table,Section header table. 1)ELF  header:在文件的开始,保存了路 ...

  7. VS2015 导航栏 查看每个cpp文件中类以及类成员函数的框框

    这个可以查看每个cpp文件中类以及类成员函数的框框叫导航栏! 怎么打开导航栏可以再百度.

  8. Tomcat启动错误一例org.apache.catalina.core.StandardContext resources Start Error starting static Resources

    org.apache.catalina.core.StandardContext resources Start Error starting static Resources 引发原因:Eclips ...

  9. linux_文件基本操作

    创建文件 $ touch [文件名]

  10. pandas设置值、更改值

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/24 15:03 # @Author : zhang chao # @Fi ...