ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

P1948 [USACO08JAN]Telephone Lines S

2022-07-17 10:35:55  阅读:128  来源: 互联网

标签:USACO08JAN cable int Lines mid P1948 poles dist 电话线


[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

思路:

因为要求最短的距离中边的最大值,所以考虑用二分,首先我们二分边权最大值,把边权大于最大边权的边免费,最后如果免费的边数小于等于\(k\),那么因为\(mid\)可能是答案,所以\(r=mid\),否则\(l=mid+1\)。
这里要注意如果不连通的话,是会取到最大值的,所以我们把二分上限加\(1\),如果达到上限就输出\(-1\),否则输出\(l\)。

代码

#include <cstdio>
#include <cstring>
#include <deque>
using namespace std;
const int N = 1010,M = 20010;
int n,m,k;
int h[N],e[M],ne[M],w[M],idx = 0;
int dist[N];
bool st[N];
void add (int a,int b,int c) {
	e[idx] = b;
	w[idx] = c;
	ne[idx] = h[a];
	h[a] = idx++;
}
bool check (int x) {
	memset (dist,0x3f,sizeof (dist));
	dist[1] = 0;
	st[1] = true;
	deque <int> q;
	q.push_back (1);
	int cnt = 0;
	while (q.size ()) {
		int t = q.front ();
		q.pop_front ();
		for (int i = h[t];~i;i = ne[i]) {
			int j = e[i],v = w[i] > x;
			if (dist[j] > dist[t]+v || !st[j]) {
				dist[j] = dist[t]+v;
				st[j] = true;
				if (v) q.push_back (j);
				else q.push_front (j);
			}
		}
	}
	return dist[n] <= k;
}
int main () {
	memset (h,-1,sizeof (h));
	scanf ("%d%d%d",&n,&m,&k);
	int maxw = 0;
	while (m--) {
		int a,b,c;
		scanf ("%d%d%d",&a,&b,&c);
		add (a,b,c),add (b,a,c);
		maxw = max (maxw,c);
	}
	int l = 1,r = maxw+1;
	while (l < r) {
		int mid = l + r >> 1;
		if (check (mid)) r = mid;
		else l = mid + 1;
	}
	if (l == maxw+1) printf ("-1");
	else printf ("%d\n",l);
    return 0;
}

标签:USACO08JAN,cable,int,Lines,mid,P1948,poles,dist,电话线
来源: https://www.cnblogs.com/incra/p/16486041.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有