题目传送门

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

题解:

二分答案。

每次check过程中, 一条边的花费 <= mid 则 路径长度为0,否者路径长度为1。
然后 求 到n的点之后长度<=k。

然后就是bfs的过程中。 如果这条边是0, 那么从前入队, 否者从后入队。

代码:

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<iostream>
#include<cstring>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
int n, p, k;
int fu[N], fv[N], fw[N]; vector<int> vc[N][];
int d[N], vis[N];
deque<int> q;
bool check(int val){
for(int i = ; i <= n; ++i){
vc[i][].clear();
vc[i][].clear();
d[i] = inf;
vis[i] = ;
}
for(int i = ; i <= p; ++i){
if(fw[i] <= val) {
vc[fu[i]][].pb(fv[i]);
vc[fv[i]][].pb(fu[i]);
}
else {
vc[fu[i]][].pb(fv[i]);
vc[fv[i]][].pb(fu[i]);
} }
q.pb();
d[] = ;
while(!q.empty()){
int x = q.front();
q.pop_front(); vis[x] = ;
for(int i = ; i < vc[x][].size(); ++i){
int v = vc[x][][i];
if(d[v] == inf){
d[v] = d[x];
q.push_front(v);
}
}
for(int i = ; i < vc[x][].size(); ++i){
int v = vc[x][][i];
if(d[v] == inf){
d[v] = d[x] + ;
q.push_back(v);
}
}
}
// cout << d[n] << endl;
return d[n] <= k;
}
int main(){
scanf("%d%d%d", &n, &p, &k);
for(int i = ; i <= p; ++i)
scanf("%d%d%d", &fu[i], &fv[i], &fw[i]);
int l = , r = ;
// check(1);
while(l <= r){
int mid = l+r >> ;
if(!check(mid)) l = mid+;
else r = mid - ;
}
if(l > ) l = -;
printf("%d\n", l);
return ;
}
/*
5 4 4
1 2 1
2 3 1
3 4 1
4 5 999999
*/

POJ-3662 Telephone Lines 二分+双端队列的更多相关文章

  1. POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)

    题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...

  2. POJ 3662 Telephone Lines (二分+dijkstra)

    题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...

  3. POJ 3662 Telephone Lines(二分+最短路)

    查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...

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

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

  5. (poj 3662) Telephone Lines 最短路+二分

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

  6. POJ3662 SPFA//二分 + 双端队列最短路

    https://cn.vjudge.net/problem/12427/origin 题意:求1到N第K + 1大条边权最小的路径 首先想到dp递推,dp[x][y]表示到x这个点经过y条免费边的最小 ...

  7. poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)

    /** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维 ...

  8. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  9. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

随机推荐

  1. 10分钟了解分布式CAP、BASE理论

    CAP理论 2000年7月,Eric Brewer教授提出CAP猜想:2年后,Seth Gilbert和Nancy Lynch从理论上证明了CAP:之后,CAP理论正式成为分布式计算领域的公认定理. ...

  2. How to extract WeChat chat messages from a smartphone running Android 7.x or above

    A friend of mine she was frustarted in extracting WeChat chat messages from suspect's smartphone run ...

  3. jenkins未授权访问漏洞

    jenkins未授权访问漏洞 一.漏洞描述 未授权访问管理控制台,可以通过脚本命令行执行系统命令.通过该漏洞,可以后台管理服务,通过脚本命令行功能执行系统命令,如反弹shell,wget写webshe ...

  4. 二、Markdown基本语法

    目录 2.1 标题 一级标题 二级标题 三级标题 2.2 加粗 2.3倾斜 2.4 高亮 2.5 上标 2.6 下标 2.7 代码引用(>式) 2.8 代码引用(```式) 2.9 代码引入(` ...

  5. Canvas动画(PC端 移动端)

    Canvas动画(PC端 移动端) 一,介绍与需求 1.1,介绍 canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3 ...

  6. 保存localStorage并访问

    将用户的输入保存至localStorage对象的属性中,这些属性在再次访问时还会继续保持在原位置. 如果你在浏览器中按照fil://URL的方式直接打开本地文件,则文法在某些浏览器中使用存储功能(比如 ...

  7. 使用PIP键盘输入数字小数位--Smart LCD

    应用范例: 使用TOPWAY Smart LCD (HMT050CC-C) 使用PIP键盘输入数字小数位 第一步 建立工程 第二步 建立三个页面,导入图片 点击工作区域, 右面显示页面属性 属性中Ba ...

  8. js及jquery常用代码

    1.获取屏幕尺寸 document.documentElement.scrollWidth; document.documentElement.scrollHeight; $(window).widt ...

  9. 析构函数中调用 Dispose 报错 :Internal .Net Framework Data Provider error 1.[非原创]

    搜索MSDN的资源可以找到答案: 原文如下http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=473449&SiteID=1 以下是关于 ...

  10. Oracle中的转换函数

    Oracle中的转换函数有三个,分别为to_char(),to_date(),to_number() 1.to_char()的用法 格式化当前的日期时间 select sysdate,to_char( ...