首页 > 其他分享 >Telephone Lines S

Telephone Lines S

时间:2024-06-07 12:55:10浏览次数:14  
标签:cable int Lines Telephone num 1e6 电话线 dis

[USACO08JAN] Telephone Lines S

题目描述

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.

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

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

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

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

输入格式

输入文件的第一行包含三个数字 \(n,p,k\)。

第二行到第 \(p+1\) 行,每行分别都为三个整数 \(a_i,b_i,l_i\)。

输出格式

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

样例 #1

样例输入 #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

样例输出 #1

4

P1948 [USACO08JAN] Telephone Lines S

分析

\(x\) 是要求的最小化最大值的答案。

要使 \(x\) 成立,从 \(1\) 到 \(n\) 的路径中大于x的边全部要删掉。
因此大于x的边的数量必须要小于k。

最小化最大值,考虑二分:

\(num(x)\) 表示从 \(1\) 到 \(n\) 的路径中大于 \(x\) 的边的最小数量。
在区间 \([l,r]\) 上满足 \(num(x)=k\)。

  • 当存在 \(y>r\)
    易证明,\(num(y)<num(x)\),因此 \(num(y)<k\)。

  • 当存在 \(y<l\)
    \(num(y)>num(x)\),\(num(y)>k\)。

因此二分确定区间,再取区间的最小值,当然是边权存在情况下的最小值,就可以得到答案。
二分区间为 \([0,1e6+1]\)。

具体来说:

对于一个满足 \(num(x)<=k\) 的 \(x\):

  • \(y>x\) 时,虽然满足 \(num(y)<=k\),但一定不满足最小,舍去。
  • \(y<x\) 时,可能满足 \(num(y)<=k\),此时答案 \(x\) 就更新为 \(y\)。

对于一个满足 \(num(x)>k\) 的 \(x\):

  • \(y<x\) 时,一定有 \(num(y)>=num(x)>k\),不符合条件,舍去。
  • \(y>x\) 时,\(num(y)<=num(x)\),可能满足 \(num(y)<=k\),此时答案 \(x\) 就更新为 \(y\)。

这就是二分的完整过程。

tips

为何二分区间为 \([0,1e6+1]\)?

  • 对于左端点 \(0\),当从 \(1\) 到 \(n\) 的路径经过的边的数量小于 \(k\) 答案就是 \(0\)。
  • 对于右端点 \(1e6+1\),\(1\) 无法到达 \(n\) 时,\(1e6+1\) 作为无解的返回值。

如何求 \(num(x)\)?

如果边长大于 \(x\),边权看作 \(1\),否则看作 \(0\)。
接下来求从 \(1\) 到 \(n\) 的最短路径,不过这里可以不用最短路算法,边权为 \(1\) 或 \(0\),可以直接使用 \(bfs+双端队列\)。

code

#include <bits/stdc++.h>
using namespace std;
const int N=1005,M=2e5+5;
int n,m,k;
int h[N],e[M],w[M],ne[M],tot;
deque<int> q;
int dis[N];
bool vis[N];
void add(int x,int y,int z)
{
    e[++tot]=y,w[tot]=z,ne[tot]=h[x],h[x]=tot;
}
bool check(int x)
{
    memset(dis,0x3f,sizeof dis);
    memset(vis,0,sizeof vis);
    dis[1]=0;
    q.push_back(1);
    while(q.size())
    {
        int u=q.front();q.pop_front();
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=h[u];i;i=ne[i])
        {
            int y=e[i],z=w[i]>x;
            if(dis[y]>dis[u]+z)
            {
                dis[y]=dis[u]+z;
                if(z) q.push_back(y);
                else q.push_front(y);
            }
        }
    }
    return dis[n]<=k;
}
int main ()
{
    cin>>n>>m>>k;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        cin>>x>>y>>z;
        add(x,y,z),add(y,x,z);
    }
    int l=0,r=1e6+1;
    while(l<r)
    {
        int mid=(l+r)>>1;
        if(check(mid)) r=mid;
        else l=mid+1;
    }
    if(r==1e6+1) r=-1;
    cout<<r<<"\n";
    return 0;
}

标签:cable,int,Lines,Telephone,num,1e6,电话线,dis
From: https://www.cnblogs.com/zhouruoheng/p/18237009

相关文章

  • idea编辑器中 This document contents very long lines..........
    背景:在idea编辑器中引入压缩后的js文件在子目录中,有如下提示在控制台-元素中js并未加载上 另外,同文件夹下未压缩的js文件可正常加载。可以,排除,路径问题。 解决问题:压缩的js,得放到根目录,可能是文件太大了,需要预加载 ......
  • 去除IDEA项目结构后面出现0%classes0%lines
    按住Ctrl+Alt+F6快捷键弹出下图信息。 去掉勾选对话框中的“Tomcat7CoverageResults”选项,点击“Showselected”按钮,返回查看项目结构即可发现后面的0%classes0%lines已经去掉了......
  • 三十二、openlayers官网示例解析Draw lines rendered with WebGL——使用WebGL动态修
     官网demo地址:DrawlinesrenderedwithWebGL这个示例展示了如何用webgl渲染矢量图形并动态修改点、线属性。首先先把基本的地图加载上去initMap(){this.map=newMap({layers:[newTileLayer({source:newXYZ({......
  • 详细解释def mrlines(fname, sp=‘\n‘):
     引入解释defmrlines(fname,sp='\n'):f=open(fname).read().split(sp)whilef!=[]andf[-1]=='':f=f[:-1]returnfdefmrlines(fname,sp='\n'):这是函数定义的开始部分。mrlines 是函数名,接受两个参数:fname:文件名或......
  • Windows Security Baselines(安全基线指南) 是由微软提供的一个安全配置集合,旨在帮助组
    安全基线指南-WindowsSecurity|MicrosoftLearnWindowsSecurityBaselines(安全基线)是由微软提供的一个安全配置集合,旨在帮助组织和管理员快速部署一套推荐的安全设置,以增强Windows操作系统及其组件的安全性。这些基线覆盖了操作系统本身、MicrosoftEdge浏览器、Inter......
  • Item2:用consts,enums和inlines取代#defines
    芝士wa2024.3.29Item2链接“用compiler(编译器)取代preprocessor(预处理器)”,问题就在于因为#define不被视为语言的一部分要理解这句话,需要先了解C++程序的编译过程:1.预处理preprocess预处理是编译前的准备工作。在这一步中,预处理器会执行以下操作:替换所有的宏定义(#defin......
  • FreeType Glyph Conventions 翻译(2) ——Glyph Outlines 字符图形轮廓线
    原文地址https://freetype.org/freetype2/docs/glyphs/glyphs-2.html目录像素,点,以及设备分辨率Pixels,points,anddeviceresolutions矢量表示VectorialrepresentationHintingandBitmaprendering启发和位图渲染Thissectiondescribesthewayscalablerepresentatio......
  • WPF canvas draw lines via brush
    //xaml<Windowx:Class="WpfApp11.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mic......
  • 【大语言视觉助手+LLaVA1.5】23.10.LLaVA-1.5改善后视觉语言大模型: Improved Baselin
    LLaVa家族官方资源汇总:项目主页||https://huggingface.co/liuhaotian23.04.LLaVA1.0论文:LargeLanguageandVisionAssistant(VisualInstructionTuning)23.06LLaVA-Med(医学图片视觉助手):TrainingaLargeLanguage-and-VisionAssistantforBiomedicineinOne......
  • codeforces 1575M Managing Telephone Poles
    假设固定了\((x,y)\),考虑其和\((x',y')\)的距离\((x-x')^2+(y-y')^2=x^2-2xx'+x'^2+y^2-2yy'+y'^2=(x^2+y^2)+(-2xx'+x'^2)+(-2yy'+y'^2)\)。第一个括号内的式子是个定值,不用管;第二三个式子都是一次函数的形式......