题面

题目链接

P1948 [USACO08JAN]电话线Telephone Lines

题目描述

Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system.

There are N (1 ≤ N ≤ 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John's property; no cables connect any them. A total of P (1 ≤ P ≤ 10,000) pairs of poles can be connected by a cable; the rest are too far apart.

The i-th cable can connect the two distinct poles Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used.

As it turns out, the phone company is willing to provide Farmer John with K (0 ≤ K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables.

Determine the minimum amount that Farmer John must pay.

多年以后,笨笨长大了,成为了电话线布置师。由于地震使得某市的电话线全部损坏,笨笨是负责接到震中市的负责人。该市周围分布着N(1<=N<=1000)根据1……n顺序编号的废弃的电话线杆,任意两根线杆之间没有电话线连接,一共有p(1<=p<=10000)对电话杆可以拉电话线。其他的由于地震使得无法连接。

第i对电线杆的两个端点分别是ai,bi,它们的距离为li(1<=li<=1000000)。数据中每对(ai,bi)只出现一次。编号为1的电话杆已经接入了全国的电话网络,整个市的电话线全都连到了编号N的电话线杆上。也就是说,笨笨的任务仅仅是找一条将1号和N号电线杆连起来的路径,其余的电话杆并不一定要连入电话网络。

电信公司决定支援灾区免费为此市连接k对由笨笨指定的电话线杆,对于此外的那些电话线,需要为它们付费,总费用决定于其中最长的电话线的长度(每根电话线仅连接一对电话线杆)。如果需要连接的电话线杆不超过k对,那么支出为0.

请你计算一下,将电话线引导震中市最少需要在电话线上花多少钱?

输入输出格式

输入格式

输入文件的第一行包含三个数字n,p,k;

第二行到第p+1行,每行分别都为三个整数ai,bi,li。

输出格式

一个整数,表示该项工程的最小支出,如果不可能完成则输出-1.

输入输出样例

输入样例

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

输出样例

4

说明

【时空限制】

1000ms,128MB

思路

求最大值最小,那么可以考虑二分答案。假设答案是x,那么大于x的边都要使用升级机会,而不大于x的不需要使用。所以此时我们可以跑一遍最短路,大于x的边权值为1,否则为0,判断1到n的最短路是否小于等于k

AC代码

#include<bits/stdc++.h>
const int maxn=1010;
const int maxm=10010;
using namespace std; int n,m,k;
int tot,to[maxm<<1],nxt[maxm<<1],wt[maxm<<1],head[maxn];
int ll,rr;
int dis[maxn];
bool vis[maxn],b;
priority_queue< pair<int,int> > q; bool Dijkstra(int x)
{
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[1]=0;
q.push(make_pair(0,1));
while(!q.empty())
{
int u=q.top().second;q.pop();
if(vis[u]) continue;
vis[u]=true;
for(int i=head[u];i;i=nxt[i])
{
int v=to[i];
if(dis[v]>dis[u]+(wt[i]>x))
{
dis[v]=dis[u]+(wt[i]>x);
q.push(make_pair(-dis[v],v));
}
}
}
return dis[n]<=k;
} int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1,u,v,w;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);rr=max(rr,w);
to[++tot]=v;nxt[tot]=head[u];head[u]=tot;wt[tot]=w;
to[++tot]=u;nxt[tot]=head[v];head[v]=tot;wt[tot]=w;
}
while(ll<rr)
{
int mid=(ll+rr)>>1;
if(Dijkstra(mid)) rr=mid,b=true;
else ll=mid+1;
}
if(!b) printf("-1");
else printf("%d",ll);
return 0;
}

洛谷 P1948 [USACO08JAN]电话线Telephone Lines 最短路+二分答案的更多相关文章

  1. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines

    P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...

  2. 洛谷 P1948 [USACO08JAN]电话线Telephone Lines 题解

    P1948 [USACO08JAN]电话线Telephone Lines 题目描述 Farmer John wants to set up a telephone line at his farm. ...

  3. 洛谷P1948 [USACO08JAN]电话线Telephone Lines

    题目描述 Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is u ...

  4. Luogu P1948 [USACO08JAN]电话线Telephone Lines(最短路+dp)

    P1948 [USACO08JAN]电话线Telephone Lines 题意 题目描述 Farmer John wants to set up a telephone line at his far ...

  5. P1948 [USACO08JAN]电话线Telephone Lines(二分答案+最短路)

    思路 考虑题目要求求出最小的第k+1大的边权,想到二分答案 然后二分第k+1大的边权wx 把所有边权<=wx的边权变为0,边权>wx的边权变为0,找出最短路之后,如果dis[T]<= ...

  6. P1948 [USACO08JAN]电话线Telephone Lines

    传送门 思路: 二分+最短路径:可以将长度小于等于 mid 的边视为长度为 0 的边,大于 mid 的边视为长度为 1 的边,最后用 dijkstra 检查 d [ n ] 是否小于等于 k 即可. ...

  7. 洛谷P1462 通往奥格瑞玛的道路 题解 最短路+二分答案

    题目链接:https://www.luogu.com.cn/problem/P1462 题目大意: 有 \(n\) 个点 \(m\) 条边,每个点有一个点权,每个边有一个边权.求所有长度不超过 \(b ...

  8. 题解【洛谷P1948】[USACO08JAN]电话线Telephone Lines

    题面 题解 很显然,答案满足单调性. 因此,可以使用二分答案求解. 考虑\(check\)的实现. 贪心地想,免费的\(k\)对电话线一定都要用上. 每次\(check\)时将小于\(mid\)的边权 ...

  9. [USACO08JAN]电话线Telephone Lines

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

随机推荐

  1. Bash 常用快捷方式

    从历史中执行命令 ctrl +r 搜索历史命令记录 !$ 重复上一个命令参数 文本编辑的快捷方式 c    分别更改这些配对标点符号中的文本内容 di   分别删除这些配对标点符号中的文本内容 do ...

  2. Odoo models.py BaseModel

    class BaseModel(object): """ Base class for OpenERP models. OpenERP models are create ...

  3. Pickle(1)

    1,pickle用于字符显示与存储之间的转换 2,要注意几个点 (1) 使用dump和load: (2) 版本号的要求: 3,官方文档的两个例子 4,pickle之后,数据是什么样的呢? https: ...

  4. 深入浅出 Java Concurrency (9): 锁机制 part 4[转]

    本小节介绍锁释放Lock.unlock(). Release/TryRelease unlock操作实际上就调用了AQS的release操作,释放持有的锁. public final boolean ...

  5. Configuring to Debug and Workaround Broken Client Applications

    背景:C3P0数据库连接池占满 Configuring to Debug and Workaround Broken Client Applications http://www.mchange.co ...

  6. Flask session到期时间设置 用户登录与登出

    flask版本 1.1.1 最近学习Flask开发,看官方文档产生疑问,就是session有效期的问题,默认貌似是没有有效期的,只有关闭浏览器session才会失效,其实控制session的有效期非常 ...

  7. 高性能非阻塞 Web 服务器 Undertow

    Undertow 简介 Undertow是一个用java编写的.灵活的.高性能的Web服务器,提供基于NIO的阻塞和非阻塞API. Undertow的架构是组合式的,可以通过组合各种小型的目的单一的处 ...

  8. Java获取系统时间少了八个小时

    Java获取系统时间少了八个小时 今天忽然遇到需要获取当前时间的问题,我向来谨慎,先测试获取到的系统时间是否正确,结果竟然发现少了八个小时,晕死了,记得之前在页面用javascript获取过当前时间, ...

  9. 2019-8-31-dotnet-获取用户设备安装了哪些-.NET-Framework-框架

    title author date CreateTime categories dotnet 获取用户设备安装了哪些 .NET Framework 框架 lindexi 2019-08-31 16:5 ...

  10. 矩阵快速幂求Fibonacci

    原理 我们取矩阵A 则 F1=F2=1;则可以轻易求出F(i) #define maxn 2 #define mo 1000000007 struct Matrix{ long long a[maxn ...