ICode9

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

P1948 [USACO08JAN]Telephone Lines S (分层图最短路/二分+最短路)

2022-07-11 16:05:13  阅读:152  来源: 互联网

标签:USACO08JAN idx int 短路 Lines st dist 电话线 define


题目描述

[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

代码

version1 分层图最短路

//分层图最短路
#include<bits/stdc++.h>
#define zp ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define pii pair <int, int>
#define pll pair <ll, ll>
#define endl '\n'
#define il inline
#define pb push_back
#define fi first
#define se second
#define lc u<<1
#define rc u<<1|1
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=1e6+10,M=1e7+5;
int n,m,k,tot;
priority_queue<pii,vector<pii>,greater<pii>>q;
int h[N],e[M],ne[M],w[M],idx;
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++;
}

void dijkstra()//由普通堆优化dijkstra修改而来
{
	memset(dist,0x3f,sizeof dist);
	dist[1]=0;
	q.push({0,1});
	while(q.size())
	{
		auto t=q.top();
		q.pop();
		int dis=t.first,ver=t.second;
		if(st[ver])continue;
		st[ver]=true;
		for(int i=h[ver];~i;i=ne[i])
		{
			int j=e[i],z=max(w[i],dist[ver]);
			if(dist[j]>z)//本题计算的是路径边权最大值
			{
				dist[j]=z;
				q.push({dist[j],j});
			}
		}
	}
}

int main()
{
    cin>>n>>m>>k;
    int x,y,t;
    memset(h,-1,sizeof h);
    for(int i=1;i<=m;i++)
    {
    	cin>>x>>y>>t;
		//第一层加边
    	add(x,y,t);
    	add(y,x,t);
    	
		//各层加边。
    	for(int j=1;j<=k;j++)
    	{
    		//在两层之间加边。两层之间的边代表一次免费操作,所以边权为0
    		add(x+(j-1)*n,y+j*n,0);
    		add(y+(j-1)*n,x+j*n,0);
			
			//在同一层建边
    		add(x+j*n,y+j*n,t);
    		add(y+j*n,x+j*n,t);
    	}
    }  	
	//在每一层的终点建连一条边,防止在免费次数没用完就到达了终点
	//也可以从上往下遍历一遍所有终点,求个最小值
    for(int i=1;i<=k;i++)
    {
    	add(i*n,(i+1)*n,0);
    }

    dijkstra();//最短路

    if(dist[(k+1)*n]==0x3f3f3f3f)cout<<-1<<endl;//无解输出-1
    else cout<<dist[(k+1)*n]<<endl;

    return 0;
}

version2 二分+最短路

// Problem: P1948 [USACO08JAN]Telephone Lines S
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1948
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Created Time: 2022-07-10 20:59:48
// 
// Powered by CP Editor (https://cpeditor.org)

//fw
#include<bits/stdc++.h>
#define zp ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);
#define pii pair <int, int>
#define pll pair <ll, ll>
#define endl '\n'
#define il inline
#define pb push_back
#define fi first
#define se second
#define lc u<<1
#define rc u<<1|1
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=1e5+10;
int n,m,k;
int h[N],e[N],ne[N],w[N],idx;
deque<int>q;
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 bound)//二分检查
{
	memset(st,0,sizeof st);
	memset(dist,0x3f,sizeof dist);
	q.push_back(1);
	dist[1]=0;
	while(q.size())
	{
		auto t=q.front();
		q.pop_front();
		if(st[t])continue;
		st[t]=true;

		for(int i=h[t];~i;i=ne[i])
		{
			int j=e[i],v=w[i]>bound;//判断这条边是否大于边界
			if(dist[j]>dist[t]+v)
			{
				dist[j]=dist[t]+v;
				
				if(!v)q.push_front(j);
				
				else q.push_back(j);
				
			}
		}
	}
	return dist[n]<=k;//如果经过大于边界的边小于等于k,则符合
}
int main()
{	
    cin>>n>>m>>k;
    memset(h,-1,sizeof h);
    while(m--)
    {
    	int a,b,c;
    	cin>>a>>b>>c;
    	add(a,b,c);
    	add(b,a,c);
    } 
    int l=0,r=1e6+1;//如果图不连通则会在1e6+1结束
    while(l<r)
    {
    	int mid=l+r>>1;//二分第k+1大的边权
    	if(check(mid))
    	r=mid;
    	else l=mid+1;
    } 	
    if(r==1e6+1)r=-1;
    cout<<r<<endl;
    return 0;
}

分层图解法参考资料
二分解法参考资料

标签:USACO08JAN,idx,int,短路,Lines,st,dist,电话线,define
来源: https://www.cnblogs.com/avarice/p/16466727.html

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

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

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

ICode9版权所有