P1948 [USACO08JAN]电话线Telephone Lines
2019-10-27

题目描述

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.

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

原题链接

Solve

一眼,这不就是分层图裸题吗?但是这个范围。
虽然我没试过,但是这个范围会GG。于是我放弃了分层图。(其实有心的同学可以试一试。
那么仔细分析一下这道题,发现求解的不是最小总代价,而是最小代价。而且这个代价是具有单调性的。
也就是说,一个最小代价为x的可行情况,那么大于x的情况也可行。
这就很明白了,二分答案。二分最小代价。
那如何判断呢?想一想,拿当然是最短路惹。DJ,SPFA,乃至BFS都可以。
我个人比较喜欢DJ。

众所周知

其实这个题,SPFA也可以啦。
解法也就出来了。
二分答案+DJ/SPFA

  • 大于mid的边权设为1,小于等于mid的权值设为0。
  • 跑最短路。
  • 如果dis[n]<=k的话就是可行的情况。

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define N 100000
using namespace std;
int n,m,k;
int l=0,r=100000,mid,ans=-1;
int dis[N];
int u,v,w;
int head[N],len;
bool vis[N];
struct note{
	int to,vl,nxt;
}eg[N];
struct node{
	int u,vl;
	bool operator<(const node &a)const{
		return a.vl<vl;
	}
};
priority_queue<node> q;
void add(int u,int v,int w){
	len++;
	eg[len].to=v;
	eg[len].vl=w;
	eg[len].nxt=head[u];
	head[u]=len;
}
bool dj(int s){
	memset(dis,0x3f,sizeof(dis));
	dis[1]=0;q.push((node){1,0});
	while(!q.empty()){
		int e=q.top().u;
		q.pop();
		vis[e]=false;
		for(int i=head[e];i;i=eg[i].nxt){
			int v=eg[i].to;
			int dist;
			if(eg[i].vl>s) dist=1;
			else dist=0;
			if(dis[v]>dis[e]+dist){
				dis[v]=dis[e]+dist;
				if(!vis[v]) vis[v]=true,q.push((node){v,dis[v]});
			}
		}
	}
	if(dis[n]<=k) return true;
	else return false;
}
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=m;i++){
		scanf("%d%d%d",&u,&v,&w);
		add(u,v,w);add(v,u,w);
		r=max(r,w);
	}
	while(l<=r){
		int mid=(l+r)>>1;
		if(dj(mid)){
			r=mid-1;
			ans=mid;
		}
		else l=mid+1;
	}
	printf("%d",ans);
	return 0;
}

后言

答案单调的二分莽就完了。