<题目链接>

题目大意:

在一个节点标号为1~n的无向图中,求出一条1~n的路径,使得路径上的第K+1条边的边权最小。

解题分析:
直接考虑情况比较多,所以我们采用二分答案,先二分枚举第K+1条路的边权,然后根据枚举的边权,重新建图。因为john只需要支付除K条边之后权值最大的边,所以对于所有边权小于等于枚举边的,将其边权置为0,对于那些大于枚举边权的边,边权则置为1,这样,对1~n跑最短路,就能够用于判断枚举的答案是否成立。因为建的是边权为0、1的图求最短路,所以也可以用双端队列实现的BFS求解。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; #define clr(a,b) memset(a,b,sizeof(a))
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define pb push_back
const int N = 1e3+, M = 1e4+;
const int INF = 0x3f3f3f3f;
int n,m,k,cnt,head[N],vis[N];
struct Edge{
int to,nxt,val;
}edge[M<<]; struct Ege{ //记录下初始输入边的个数
int x,y,w;
Ege(int _x=,int _y=,int _w=):x(_x),y(_y),w(_w){}
};
vector<Ege>vec; struct Node{
int dist,loc;
Node(int _dist=,int _loc=):dist(_dist),loc(_loc){}
bool operator <(const Node &tmp)const{ return dist>tmp.dist; }
}node[N]; void init(){
cnt=;clr(head,-);
}
void addedge(int u,int v,int w){
edge[++cnt].to=v,edge[cnt].val=w;
edge[cnt].nxt=head[u],head[u]=cnt;
}
void Getmap(int x){
init();
for(int i=;i<vec.size();i++){
Ege now=vec[i];
int u=now.x,v=now.y,w=now.w;
if(w<=x)addedge(u,v,),addedge(v,u,); //将花费<=k的道路边权置0,因为john只需要支付最大的道路
else addedge(u,v,),addedge(v,u,); //大于的边置为1,免费的k条边是否能够在满足题目条件的情况下,连通1~n点
}
}
bool check(int x){
Getmap(x); //根据枚举的答案,建立0、1图,然后在这个0、1图上跑最短路
priority_queue<Node>q;
for(int i=;i<=n;i++){
vis[i]=,node[i].loc=i,node[i].dist=INF;
}
node[].dist=;
q.push(node[]);
while(q.size()){
int u=q.top().loc;q.pop();
if(vis[u])continue;
vis[u]=;
for(int i=head[u];~i;i=edge[i].nxt){
int v=edge[i].to;
if(node[v].dist>node[u].dist+edge[i].val){
node[v].dist=node[u].dist+edge[i].val;
q.push(node[v]);
}
}
}
if(node[n].dist<=k)return true;
return false;
}
int main(){
while(scanf("%d%d%d",&n,&m,&k)!=EOF){
rep(i,,m){
int u,v,w;scanf("%d%d%d",&u,&v,&w);
vec.pb(Ege(u,v,w));
}
int l=,r=1e6+,ans=-;
while(l<=r){ //二分答案,枚举john需要支付的钱数,即连通1~n的道路中,除k条路外,花费最大的路径
int mid=l+r>>;
if(check(mid))ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
}

2019-03-06

POJ 3662 Telephone Lines【二分答案+最短路】||【双端队列BFS】的更多相关文章

  1. POJ 3662 Telephone Lines【Dijkstra最短路+二分求解】

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7214   Accepted: 2638 D ...

  2. POJ 3662 Telephone Lines(二分+最短路)

    查看题目 最小化第K大值. 让我怀疑人生的一题目,我有这么笨? #include <cstdio> #include <queue> #include <cstring& ...

  3. poj-3662 Telephone Lines 二分答案+最短路

    链接:洛谷 POJ 题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone co ...

  4. POJ 3662 Telephone Lines (二分+Dijkstra: 最小化第k大的值)

    题意 Farmer John想从电话公司修一些电缆连接到他农场.已知N个电线杆编号为1,2,⋯N,其中1号已经连接电话公司,N号为农场,有P对电线杆可连接. 现给出P对电线杆距离Ai,Bi,Li表示A ...

  5. POJ 3662 Telephone Lines (二分+dijkstra)

    题意: 多年以后,笨笨长大了,成为了电话线布置师.由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人. 该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话 ...

  6. (poj 3662) Telephone Lines 最短路+二分

    题目链接:http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total ...

  7. POJ 3662 Telephone Lines(二分答案+SPFA)

    [题目链接] http://poj.org/problem?id=3662 [题目大意] 给出点,给出两点之间连线的长度,有k次免费连线, 要求从起点连到终点,所用的费用为免费连线外的最长的长度. 求 ...

  8. poj 3662 Telephone Lines(最短路+二分)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6973   Accepted: 2554 D ...

  9. POJ - 3662 Telephone Lines (Dijkstra+二分)

    题意:一张带权无向图中,有K条边可以免费修建.现在要修建一条从点1到点N的路,费用是除掉免费的K条边外,权值最大的那条边的值,求最小花费. 分析:假设存在一个临界值X,小于X的边全部免费,那么此时由大 ...

随机推荐

  1. SQL Server管理员必备技能之性能优化

    SQL Server管理员必备技能之性能优化 高文龙关注1人评论1171人阅读2017-09-22 08:27:41 SQL Server 作为企业必不可少的服务之一,所以对于管理员的日常运维是一个极 ...

  2. bat命令行实现全盘遍历搜索文件

    背景:当想要查找一个文件时,记得放在某个盘里.手动去遍历时感觉好心累,找了半天还是没有找着(虽然win有自带的搜索框,但是看着进度条的速度,我便果断的点了取消).基于这个情况,所以写了脚本满足自身查找 ...

  3. java----java工具类

    System: Runtime: Random: Scanner: Arrays: MessageFormat: Math: 日期: Comparable: cloneable接口: 数字处理: MD ...

  4. lightoj 1282 取对数的操作

    /* 前三位 len=log10n^k(乘积的长度) len=klog10n n^k=x*10^(len-1) x=n^k/10^(len-1) log10x = k*log10n - (len-1) ...

  5. bzoj4821-线段树区间lazy_tag下放的优先级和区间覆盖

    见博客https://www.cnblogs.com/zwfymqz/p/8588693.html 题解链接https://blog.csdn.net/ripped/article/details/7 ...

  6. vsftpd中的local_umask和anon_umask

    umask是在linux中常见的一个东西,它其实是一个掩码.当然,也有umask这样一个命令,它是对用户建立的文件的默认属性的定义.该 定义为: 假设umask为022,则对于一个文件夹的话,它的默认 ...

  7. C++ friend友元函数和友元类(转)

    一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有本类中的函数可以访问本类的 private 成员.现在,我们来介绍一种例外情 ...

  8. SQL Server表关联

    表关联:Hash.Nested Loops.Merge.这是实际算法,不是T-SQL中的inner/left/right/full/cross join.优化器会把这些T-SQL写法转换成上面的3种算 ...

  9. 磁盘blk_update_request: I/O error

    https://www.cnblogs.com/chris-cp/p/6340289.html

  10. ASP.NET Core Middleware管道介绍

    public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Use(async (context, ne ...