POJ - 3662 Telephone Lines (dijstra+二分)
题意:有N个独立点,其中有P对可用电缆相连的点,要使点1与点N连通,在K条电缆免费的情况下,问剩下的电缆中,长度最大的电缆可能的最小值为多少。
分析:
1、二分临界线(符合的情况的点在右边),找可能的最小值,假设为mid。
2、将大于mid的边变为1,小于等于mid的边变为0(表示这些边由自己承包),由此算出1~N的最短路长度为x。x即为所用的大于mid的电缆个数。
3、若x<=K,则符合情况,但是想让所用的免费电缆条数x更多,所以让mid更小一些,这样自己承包的边也减少,x将更大,即r = mid;
4、若x>K,则所用免费电缆条数x超过了K条,不再符合题意,自己承包的边过少了,所以l = mid + 1;
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int N, P, K;
struct Edge{
int from, to, dist;
Edge(int f, int t, int d):from(f), to(t), dist(d){}
};
struct HeapNode{
int d, u;
HeapNode(int dd, int uu):d(dd), u(uu){}
bool operator < (const HeapNode& rhs)const{
return d > rhs.d;
}
};
struct Dijkstra{
int n, m;
vector<Edge> edges;
vector<int> G[MAXN];
bool done[MAXN];
int d[MAXN];
int p[MAXN];
void init(int n){
this -> n = n;
for(int i = 0; i < n; ++i) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int dist){
edges.push_back(Edge(from, to, dist));
m = edges.size();
G[from].push_back(m - 1);
}
void dijkstra(int s, int t){
priority_queue<HeapNode> Q;
for(int i = 0; i < N; ++i) d[i] = INT_INF;
d[s] = 0;
memset(done, 0, sizeof done);
Q.push(HeapNode(0, s));
while(!Q.empty()){
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = 0; i < G[u].size(); ++i){
Edge& e = edges[G[u][i]];
int tmp = e.dist > t ? 1 : 0;
if(d[e.to] > d[u] + tmp){
d[e.to] = d[u] + tmp;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to], e.to));
}
}
}
}
}dij;
bool judge(int x){
dij.dijkstra(0, x);
if(dij.d[N - 1] <= K) return true;
return false;
}
int solve(){
int l = 0, r = 1e6;
while(l < r){
int mid = l + (r - l) / 2;
if(judge(mid)) r = mid;
else l = mid + 1;
}
if(judge(r)) return r;
return -1;
}
int main(){
dij.init(N);
scanf("%d%d%d", &N, &P, &K);
for(int i = 0; i < P; ++i){
int a, b, l;
scanf("%d%d%d", &a, &b, &l);
dij.AddEdge(a - 1, b - 1, l);
dij.AddEdge(b - 1, a - 1, l);
}
printf("%d\n", solve());
return 0;
}
POJ - 3662 Telephone Lines (dijstra+二分)的更多相关文章
- POJ 3662 Telephone Lines(二分答案+SPFA)
[题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...
- POJ 3662 Telephone Lines (二分 + 最短路)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...
- POJ - 3662 Telephone Lines (Dijkstra+二分)
题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...
- 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 ...
- 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 ...
- 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: 7115 Accepted: 2603 D ...
- POJ 3662 Telephone Lines (分层图)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6785 Accepted: 2498 D ...
随机推荐
- Java的SPI机制
目录 1. 什么是SPI 2. 为什么要使用SPI 3. 关于策略模式和SPI的几点区别 4. 使用介绍或者说约定 4.1 首先介绍几个名词 4.2 约定 5. 具体的demo实现 5.1 创建服务提 ...
- swing开发图形界面工具配置(可自由拖控件上去)
swing开发图形界面工具,eclipse swing图形化操作界面工具配置 1.有一个小功能要有一个界面,之前知道有一个 图形化界面的(就是可以往上面拖控件布局的工具)JBuilder,今天上午就下 ...
- 谈谈HashSet的存储原理及为什么重写equals必须重写hashcode方法
HashSet的存储原理: 1.将要传入的数据根据系统的hash算法得到一个hash值: 2.根据hash值可以得出该数据在hash表中的位置: 3.判断该位置上是否有值,没有值则把数据插入进来:如果 ...
- [PHP] php作为websocket的客户端实时读取推送日志文件
首先要使用composer来下载一个第三方扩展就可以实现php的websocket客户端,直接在当前目录生成下composer.json文件就可以了composer require textalk/w ...
- Java日志相关概述
日志是代码调试.生产运维必备工具,基本所有软件都会有日志记录. 1.常用日志框架介绍 1.Logging jdk1.5自带日志工具类,位于java.util.logging; 2.Log4j 市场占有 ...
- 024、MySQL字符串替换函数,文本替换函数
#文本替换 ,,'); #520ABCDEFG ,,'); #520BCDEFG ,,'); #520CDEFG ,,'); #A520BCDEFG ,,'); #A520CDEFG ,,'); #A ...
- 项目上线后,遇到IE浏览器不显示大部分组件的问题
document.addEventListener('touchmove',(evt)=>{ }) 以上是ES6 语法,箭头函数,当然在IE下是不行的啦. 所以改为:ES5语法 document ...
- 吴裕雄--天生自然java开发常用类库学习笔记:大数操作
import java.math.* ; class MyMath{ public static double add(double d1,double d2){ // 进行加法计算 BigDecim ...
- Docker-harbor-V1.3.0 ”私有仓库“搭建 Easy
准备: centos 7.0 Docker version 1.12.6 docker-compose version 1.19.0 1: updata-yum: 更新yum 源 ...
- 虚拟 DOM 到底是什么?
虚拟 DOM 到底是什么? 作者:wangshengliang 注意:由于文章太长,对文章有删减,但是不会影响整体阅读 是什么? 虚拟 DOM (Virtual DOM )这个概念相信大家都不陌生,从 ...