【P1948 [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\)

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

image

思路

最大的最小,显然是二分

二分\(K+1\)值,然后最短路求一遍最少需要免费多少条电话线杆。

在最短路的过程中,大于当前二分的值边权为1,小于则为0。

然后判断最短路跑出的数量和 \(K\) 的关系,大于则可行,小于则不可行

Code

// Problem: P1948 [USACO08JAN]Telephone Lines S
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1948
// Memory Limit: 125 MB
// Time Limit: 1000 ms

#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define N 10010
//#define M
//#define mo
struct node
{
	int x, y, z, n; 
}d[N<<1]; 
int n, m, i, j, k, T; 
int c, u, v, w, h[N], ans[N], b[N]; 
int l, r, mid, g; 
queue<int>q; 

void cun(int x, int y, int z)
{
	d[++k].x=x; d[k].y=y; d[k].z=z; 
	d[k].n=h[x]; h[x]=k; 
}

int check(int x)
{
	memset(ans, 0x3f, sizeof(ans)); 
	ans[1]=0; q.push(1); 
	while(!q.empty())
	{
		u=q.front(); q.pop(); 
		b[u]=0; 
		// printf("%lld\n", u); 
		for(g=h[u]; g; g=d[g].n)
		{
			v=d[g].y; w=(d[g].z>x ? 1 : 0); 
			if(ans[u]+w<ans[v])
			{
				ans[v]=ans[u]+w; 
				if(!b[v]) b[v]=1, q.push(v); 
			}
		}
	}
	// printf("%lld %lld\n", x, ans[n]); 
	return (ans[n]<=c ? 1 : 0); 
}

signed main()
{
//	freopen("tiaoshi.in","r",stdin);
//	freopen("tiaoshi.out","w",stdout);
	n=read(); m=read(); c=read(); 
	for(i=1; i<=m; ++i)
	{
		u=read(); v=read(); w=read(); 
		cun(u, v, w); 
		cun(v, u, w); 
	}
	if(!check((int)1e9)) return printf("-1"), 0; 
	l=0; r=1e9; 
	while(l<r)
	{
		mid=(l+r)>>1; 
		if(check(mid))  r=mid; 
		else l=mid+1; 
	}
	printf("%lld", l); 
	return 0;
}

总结

这是一道非常经典的二分题。

首先看到最大的最小显然可以发现是二分,然后转化为01边权问题。

这类思想在很多问题中会用到。

posted @ 2022-05-19 17:46  zhangtingxi  阅读(30)  评论(0编辑  收藏  举报