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.

输入输出样例

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

【思路】

二分答案+SPFA

【题目大意】

点1到点n中间有很多对可以连接电线的电线杆子

连接的权值不一样

可以让你免费连接K条

求最少的花费(花费等于连接的最长的那个电线,这里连接的电线当然不包括免费的)

【题目分析】

答案是具有单调性的

因为如果花费i的钱可以的话

那花费i+1块钱也一定可以

花费i块钱不行的话

那花费i-1块钱也一定是不可以的

所以用二分答案就很显然了吧

【核心思路】

二分什么呢?

当然是二分花费了

因为是可以免去k条的

所以大于二分出来的值的那就变为0就好了

所以如果1到n被免去最少电线的那条路

只要满足免去电线的根数小于等于k

那这个答案就是属于可行范围的

反之则是不可行的

这就是二分的思路

不过,既然是求最小的那根

那为什么不用SPFA呢?

当成最短路来写就好了

被免去的边的权值就赋值为1

没有被免去的那就是0

这样就能求出1到n之间连接的边中

被免去最少边的那条边的免去的值是多少了

【题外话】

一开始我想的是二分出来值之后搜索的

然后搜索中如果某一条边不行的话

那应该返回去搜另一条边的

但是如果这个点之后的下一个点不行的话

那应该是搜索另外的和这个点连着的点的

所以应该是continue

但是我打成了return

还莫名其妙的比continue多了20分成了70分

因为这个玄学的return让我少跑了很多的东西导致不会超时

因为这个我被gyh大佬戏称为“最强优化大师”!

(在最后附上我的错误解法代码)

【完整代码——AC】

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int Max = 20010;
struct node
{
int y,ne,z;
}a[Max];
int sum = 0,head[1010];
void add(int x,int y,int z)
{
a[++ sum].y = y;
a[sum].ne = head[x];
a[sum].z = z;
head[x] = sum;
}
int n,p,k;
int d[1010];
bool use[1010];
bool check(int x)
{
memset(d,0x3f,sizeof(d));
// memset(use,false,sizeof(use));
// memset(cnt,0,sizeof(cnt));
queue<int>q;
d[1] = 0;
q.push(1);
use[1] = true;
while(!q.empty())
{
int qwq = q.front();
q.pop();
use[qwq] = true;
for(register int i = head[qwq];i != 0;i = a[i].ne)
{
int owo;
if(a[i].z > x)
owo = d[qwq] + 1;
else
owo = d[qwq];
if(owo < d[a[i].y])
{
d[a[i].y] = owo;
if(!use[a[i].y])
{
q.push(a[i].y);
use[a[i].y] = true;
}
}
}
use[qwq] = false;
}
if(d[n] <= k)return true;
else return false;
} int main()
{
cin >> n >> p >> k;
for(register int i = 1;i <= p;++ i)
{
int u,v,w;
cin >> u >> v >> w;
add(u,v,w);add(v,u,w);
}
int ans = -1;
int l = 0,r = 1000000;
while(l <= r)
{
int mid = (r + l) >> 1;
if(check(mid))r = mid - 1,ans = mid;
else l = mid + 1;
}
cout << ans << endl;
return 0;
}

【买一赠一 —— 错解】

/*
我二分出来最长的那条电话线的长度
然后从1开始搜索
如果这条线长度超出了二分出来的值
那就先用那k对免费的电话线
如果k对用完了那这条路就是行不通的
就回到上一个点换条路接着搜索
如果这个搜索里面有能够在满足刚好用完或者用不完k条免费的电话线的情况下
还能够让最长的电话线小于等于二分出来的数
那二分返回就是真
*/
#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
const int Max = 10004;
const int M = 1003;
struct node
{
int y,ne;
int w;
}a[Max << 1];
int sum = 0;
int head[M];
void add(int u,int v,int w)
{
a[++ sum].y = v;
a[sum].ne = head[u];
a[sum].w = w;
head[u] = sum; a[++ sum].y = u;
a[sum].ne = head[v];
a[sum].w = w;
head[v] = sum;
}
int n,p,k;
int fg;
bool use[M]; void dfs(int x,int fath,int js,int mm)
{
if(fg == 0)
{
if(x == n)
{
fg = 1;
return;
}
for(register int i =head[x];i != 0;i = a[i].ne)
{
if(a[i].y != fath && use[a[i].y] == false)
{
if(a[i].w > mm)
{
if(js > 0)
{
use[a[i].y] = true;
dfs(a[i].y,x,js - 1,mm);
use[a[i].y] = false;
}
else return;
}
else
{
use[a[i].y] = true;
dfs(a[i].y,x,js,mm);
use[a[i].y] = false;
}
}
}
}
} bool check(int x)
{
fg = 0;
dfs(1,0,k,x);
if(fg == 0)return false;
else return true;
} int father[M]; int find(int x)
{
if(father[x] != x)father[x] = find(father[x]);
return father[x];
} void hebing(int x,int y)
{
x = find(x);
y = find(y);
if(x != y)
father[x] = y;
} signed main()
{
cin >> n >> p >> k;
int MM = 0;
for(register int i = 1;i <= n;++ i)
father[i] = i;
for(register int i = 1;i <= p;++ i)
{
int u,v,w;
cin >> u >> v >> w;
if(find(u) != find(v))
hebing(u,v);
add(u,v,w);
MM = max(MM,w);
}
if(find(1) != find(n))
{
cout << -1 << endl;
return 0;
}
int l = 0,r = MM;
while(l < r)
{
int mid = (l + r) >> 1;
if(check(mid))r = mid;
else l = mid + 1;
}
cout << l << endl;
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 最短路+二分答案

    目录 题面 题目链接 题目描述 输入输出格式 输入格式 输出格式 输入输出样例 输入样例 输出样例 说明 思路 AC代码 题面 题目链接 P1948 [USACO08JAN]电话线Telephone ...

  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. [USACO08JAN]电话线Telephone Lines

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

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

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

  9. [USACO08JAN]电话线Telephone Lines(分层图)/洛谷P1948

    这道题其实是分层图,但和裸的分层图不太一样.因为它只要求路径总权值为路径上最大一条路径的权值,但仔细考虑,这同时也满足一个贪心的性质,那就是当你每次用路径总权值小的方案来更新,那么可以保证新的路径权值 ...

随机推荐

  1. HBase 系列(三)—— HBase 基本环境搭建

    一.安装前置条件说明 1.1 JDK版本说明 HBase 需要依赖 JDK 环境,同时 HBase 2.0+ 以上版本不再支持 JDK 1.7 ,需要安装 JDK 1.8+ .JDK 安装方式见本仓库 ...

  2. node-red inject节点 debug节点 switch节点

    inject节点: https://blog.csdn.net/geek_monkey/article/details/80737818 debug节点: https://blog.csdn.net/ ...

  3. ChineseNumber 转换

    中文数字转换 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * &l ...

  4. 记录个超级Update语句

    -- UPDATE UPDATE affair_list SET deleteState = WHERE gid IN ( SELECT tt.gid FROM ( SELECT a.gid FROM ...

  5. WPS生成多级编号

    需求: 目前标题是标题1,想要 string 是二级标题,并且编号是 2.1 方案: (1)设置string是二级标题 (2)设置多级编号 选中,右击,选择[项目符号和编号] 选择[多级编号],点击[ ...

  6. SQL Server——死锁查看

    一.通过语句查看 --查询哪些死锁SELECT request_session_id spid, OBJECT_NAME( resource_associated_entity_id ) tableN ...

  7. C#实现RSA加密解密

    RSA介绍 RSA公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。 RSA的缺点: 产生密钥 ...

  8. KindEditor 简单使用笔记

    1.在官网下载最新版本  http://kindeditor.net/demo.php 2.在页面中加上如下代码 <textarea id="editor_id" name= ...

  9. MPAndroid 的学习

    1.MPAndroid 的github的地址: https://github.com/PhilJay/MPAndroidChart#documentation 2.使用步骤: 在build.gradl ...

  10. [golang]使用gomail发邮件(在Go中发送电子邮件的最佳方式)

    1 前言 定义邮箱服务器连接信息,如果是网易邮箱 pass填密码,qq邮箱填授权码(客户端专用密码). gomail包: go get gopkg.in/gomail.v2 更多功能可以参考 http ...