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 ...
随机推荐
- 侯捷C++学习(一)
//c++学习//标准库非常重要//要规范自己的代码complex c1(2,1);complex c2;complex* pc = new complex(0,1);string s1(" ...
- CSS - 控制最后边框的隐藏或设置为none
div{ width: 20%; border-left: 1px solid $border-color; &:nth-child(5n+1){ ...
- postProcessBeanFactory方法源码跟踪
看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...
- 数据可视化-gojs插件使用技巧总结
随着云计算时代的到来,由于Web技术的快速革新以及为了提供高质量的用户体验,数据可视化成为了前端技术发展的一大方向.为了解决这个问题,现如今涌现了很多优秀的第三方的javascript图形库,比如hi ...
- vDom和domDiff
虚拟dom和domDiff 1. 构建虚拟DOM var tree = el('div', {'id': 'container'}, [ el('h1', {style: 'color: blue'} ...
- 杭电oj1860:统计字符(字符串hash / 水题)
统计字符 题目链接 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem D ...
- 让SVG以组件的方式引入吧!
安装 npm i -D vue-svg-loader or yarn add -D vue-svg-loader webpack 配置 module.exports = { module: { rul ...
- 获取目录结构,并写到txt文档里
cmd里直接运行: tree /f > ml.txt 写成bat tree D:\a\ /f > D:\a\目录.txt 效果 卷 本地磁盘 的文件夹 PATH 列表 卷序列号为 18A9 ...
- Spring Boot2(006):关于配置类(Configuration Classes)和配置(Configuration)
一.配置类(Configuration Classes) Spring Boot 支持基于 xml 的配置,但更偏向于使用基于 Java 的配置,通常建议使用定义有 main 方法的主 @Config ...
- 《方方格子》(WPS版) _v3.6.6.0
<方方格子>(WPS版) 下载地址(b2a7) SHA1:35AE4D99B77613D9E2BAF912477DC74C5C2B8389 版本信息 发行版本 3.6.6.0 ...