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 ...
随机推荐
- PyQt4 在Windows下安装
快来加入群[python爬虫交流群](群号570070796),发现精彩内容. 首先在网上下载sip文件下载完之后解压, 在Windows的开始菜单栏中进入sip的解压目录下: 在目录下面 ...
- [__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x17deba00
还真是一波未平一波又起,又出现了这个问题,详情如下: -[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized ...
- 利用dockerfile 安装一个nginx-1.14.1
FROM docker.io/centos MAINTAINER jim 107420988@qq.com ENV TZ "Asia/Shanghai" #ENV TERM xte ...
- How to check all timestamps of a file
A friend of mine she asked me how to check all timestamps of a file on an NTFS volume. She did not h ...
- Redis优化建议
优化的一些建议 1.尽量使用短的key 当然在精简的同时,不要完了key的"见名知意".对于value有些也可精简,比如性别使用0.1. 2.避免使用keys * keys *, ...
- Centos7 搭建owncloud云存储
Centos7 搭建owncloud云存储 首先准备必要的软件和资料. 这里我已经整理好了: 百度云共享 不过最好还是自己去官网上下.这里只不过是提供了快捷方式. owncloud官网:https:/ ...
- idea使用大全(加载mysql驱动)
1.载入mysql驱动 找到项目结构(project structure) 选Modules 找到右边的加号选择第一个 OK
- zookeeper中的分布式一致性协议
1. zookeeper中的一致性协议-ZAB协议 在深入了解ZK之前,相信很多同学都会认为ZK就是Paxos算法的一个实现.但事实上,ZK并没有完全采用Paxos算法,而是使用了一种称为ZooKee ...
- Mysql 局域网连接设置——Windows
在公司工作中,会遇到mysql数据库存储于某个人的电脑上,大家要想连接mysql服务,装有mysql服务的电脑就必须开启远程连接. 其实不仅仅是局域网,只要你有数据库所在服务器的公网IP地址都能连上. ...
- docker-compose 综合训练
Docker-compose综合训练 一. 实验目的: 熟悉Docker-compose的基本工作原理: 熟悉Docker-compose安装 熟悉Docker compose命令基础 熟悉Docke ...