POJ-3662 Telephone Lines 二分+双端队列
题意:有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 二分+双端队列的更多相关文章
- POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)
题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...
- POJ 3662 Telephone Lines (二分+dijkstra)
题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...
- POJ 3662 Telephone Lines(二分+最短路)
查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...
- POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】
<题目链接> 题目大意: 在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小. 解题分析:直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+ ...
- (poj 3662) Telephone Lines 最短路+二分
题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ3662 SPFA//二分 + 双端队列最短路
https://cn.vjudge.net/problem/12427/origin 题意:求1到N第K + 1大条边权最小的路径 首先想到dp递推,dp[x][y]表示到x这个点经过y条免费边的最小 ...
- poj 3259 Wormholes : spfa 双端队列优化 判负环 O(k*E)
/** problem: http://poj.org/problem?id=3259 spfa判负环: 当有个点被松弛了n次,则这个点必定为负环中的一个点(n为点的个数) spfa双端队列优化: 维 ...
- POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7214 Accepted: 2638 D ...
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
随机推荐
- “$Bitmap 有标记已使用的未用簇”
前几天在电脑上用 DiskGenius 给移动硬盘分区的时候出现了这个错误,如下图所示: 解决方法: 在 cmd 命令行窗口中输入如下代码: chkdsk /f /x c: PS: 其中 " ...
- ansible-service
#service#查询服务状态 ansible server01 -m service -a "name=httpd state=started" #停止服务 ansible se ...
- Linux内核OOM killer机制
程序运行了一段时间,有个进程挂掉了,正常情况下进程不会主动挂掉,简单分析后认为可能是运行时某段时间内存占用过大,系统内存不足导致触发了Linux操作系统OOM killer机制,将运行中的进程杀掉了. ...
- C#文件下载流程
private bool DownloadPicture(string picUrl, string savePath, int timeOut) { bool ...
- Cannot attach the file “MvcMovie.mdf” as database “aspnet-MvcMovie”
今天在微软开发人员官网上学习asp.net mvc5入门的时候,遇到一个棘手的问题,我是按照教程一步一步操作的,但期间遇到一个自己觉得莫名其妙的问题,教程中也没有提到这个, 在添加新字段这一章节,跟着 ...
- java学习中碰到的疑惑和解答(二)
路径问题是一个在平时学习和开发碰到的常见问题,对于初学者是一个比较值得研究的东西.因此对路径问题进行总结. 1. 编写路径为了告诉编译器如何找到其他资源. 2. 路径分类: 相对路径:从当前资源出 ...
- 一文速览Vue全栈
一文速览Vue全栈 原创: 新哥Lewis 天道酬勤Lewis 7月7日 Vue 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用,专注于声明式渲染视图 ...
- RocketMQ中PullConsumer的消息拉取源码分析
在PullConsumer中,有关消息的拉取RocketMQ提供了很多API,但总的来说分为两种,同步消息拉取和异步消息拉取 同步消息拉取以同步方式拉取消息都是通过DefaultMQPullConsu ...
- js及jquery常用代码
1.获取屏幕尺寸 document.documentElement.scrollWidth; document.documentElement.scrollHeight; $(window).widt ...
- net core Webapi基础工程搭建(二)——创建工程
目录 前言 创建工程 工程文件概述(个人理解) 运行 小结 前言 前面介绍了开发工具及net core版本,后端工程的框架结构开发工具及环境,因为是基础工程,所以没考虑太复杂的框架,如果有架构师请勿喷 ...