POJ - 3662 Telephone Lines (Dijkstra+二分)
题意:一张带权无向图中,有K条边可以免费修建。现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费。
分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大于等于X的边组成的图,从点1到点N走过的边数小于等于K,那么这个X就是所求的答案。所以可以通过二分答案的方法求解该问题,每一次根据mid值跑迪杰斯特拉,d[i]记录路径长度(走过边的数目)。需要注意的是,要特判一下点1到点N本身不连通的情况以及花费为0的情况。二分的时候,当d[N]>K时修改答案为mid,因为此时能确定最后的结果一定大于等于mid。
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef int LL;
const int maxn =1e3+;
const int maxm =5e4+;
const LL INF =0x3f3f3f3f;
struct Edge{
int from,to,next;
LL val;
bool operator <(const Edge &e) const {return val<e.val;}
}; struct HeapNode{
LL d; //费用或路径
int u;
bool operator < (const HeapNode & rhs) const{return d > rhs.d;}
};
struct Dijstra{
int n,m,tot;
Edge edges[maxm];
bool used[maxn];
LL d[maxn];
int head[maxn]; void init(int n){
this->n = n;
this->tot=;
memset(head,-,sizeof(head));
} void Addedge(int u,int v ,LL dist){
edges[tot].to = v;
edges[tot].val = dist;
edges[tot].next = head[u];
head[u] = tot++;
} int dijkstra(int s,int limit){
memset(used,,sizeof(used));
priority_queue<HeapNode> Q;
for(int i=;i<=n;++i) d[i]=INF; //d[i]记录的是到i点走过的权值超过limit的边数
d[s]=;
Q.push((HeapNode){,s});
while(!Q.empty()){
HeapNode x =Q.top();Q.pop();
int u =x.u;
if(d[u]<x.d) continue; //没有更新的必要,不加判断也对,但是慢一点点
for(int i=head[u];~i;i=edges[i].next){
int nd = d[u]+(edges[i].val>=limit?:);
int v = edges[i].to;
if (d[v] > nd){
d[v] = nd;
Q.push({d[v], v});
}
}
}
return d[n];
}
}G; #define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int N,M,K,u,v,k;
LL tmp;
while(scanf("%d%d%d",&N,&M,&K)==){
G.init(N);
int maxL = -;
for(int i=;i<M;++i){
scanf("%d%d%d",&u,&v,&tmp);
G.Addedge(u,v,tmp);
G.Addedge(v,u,tmp);
if(maxL<tmp) maxL = tmp;
}
int res = G.dijkstra(,);
if(res==INF){
printf("-1\n");
continue;
}
else if(res<=K){ //先特判一下
printf("0\n");
continue;
}
int L =,R=maxL,mid,ans;
while(L<R){
mid =(L+R)>>;
if(G.dijkstra(,mid)>K){
ans = mid; //此时能确定的是:肯定要花费mid的代价
L = mid+;
}
else R =mid-;
}
printf("%d\n",ans);
}
return ;
}
POJ - 3662 Telephone Lines (Dijkstra+二分)的更多相关文章
- poj 3662 Telephone Lines dijkstra+二分搜索
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5696 Accepted: 2071 D ...
- 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【二分答案+最短路】||【双端队列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
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7115 Accepted: 2603 D ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...
随机推荐
- 网页端的utf8和gb2312 之间关于osd 传参数的乱码问题
(0)HZK16 点阵字库原理及实现 (1)utf8 和 unicode gb2312之间的转换 (2)gb2312 的拓展 gbk 实现了更多的文字编码 像“瞭望塔”的瞭子在gb2312中是没有的 ...
- bash脚本IFS=',' read的意思
IFS is the Input Field Separator, which means the string read will be split based on the characters ...
- 你 get 了无数技能,为什么一事无成
前 几日看到阮一峰老师的发的一句话,颇有感慨,「你只是坐在电脑前,往网上发表了一段文字或者一张图片,随便什么,就能够接触到多少陌生的灵魂.这就是我热 爱互联网的原因」.我打心底认为这是一个最好的时代, ...
- 第6步:检查grid安装环境
6.1 检查系统包 grid 身份下校验安装环境(检测crs安装环境(sgdb1)) [root@node1 soft]#su – grid [grid@node1 ~]$ cd /soft/grid ...
- PHP和JS判断手机还是电脑访问
当用户使用手机等移动终端访问网站时,我们可以通过程序检测用户终端类型,如果是手机用户,则引导用户访问适配手机屏幕的移动站点.本文将介绍分别使用PHP和JAVASCRIPT代码判断用户终端类型. PHP ...
- java 包冲突解决方法
1.诊断包冲突 java.lang.NoSuchMethodError: org.apache.commons.io.output.DeferredFileOutputStream.<init& ...
- Linux中buffer/cache,swap,虚拟内存和page ++
1.Buffer 和 cache Free 命令相对于top 提供了更简洁的查看系统内存使用情况: [apptest@vs022 ~]$ free -m ——以MB为单位 ...
- dns解决测试微信二级域名访问问题
背景介绍: 1:解决本地不能通过域名访问问题: 2:解决微信设置二级域名且本地iis站点使用非80端口号问题: ps:网站中微信部分在global中设置了重定向,代码已经修改为必须通过“wechat. ...
- 【BZOJ1283/3550】序列/[ONTAK2010]Vacation 最大费用流
[BZOJ1283]序列 Description 给出一个长度为 的正整数序列Ci,求一个子序列,使得原序列中任意长度为 的子串中被选出的元素不超过K(K,M<=100) 个,并且选出的元素之和 ...
- Powershell About LocalGroupMembership
一: 结合active directory获取本地群组成员信息(包含本地用户和域用户,及域用户的情况 $DBServer = "xxxx" $DBDatabase = " ...