Telephone Lines

Description

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个电线杆,有P对电线杆是可以连接的,用几条线连接在一起的电线杆之间都可相互通信,现在想要使得电线杆1和电
线杆N能相互通信,并且电线公司提出K条电线是可以免费使用的,当使用电线的数量超过K条,超出的电线要收费,收的总费用
为去掉免费使用的K条电线之后最长的那条电线的长度。现在需要尽可能的减少费用,问最少费用是多少(当然,如果电线杆1和电线杆N不能相互通信,那就输出-1)

Input

  • Line 1: Three space-separated integers: N, P, and K

  • (第1行:三个用空格分隔的整数:N、P和K)

  • Lines 2~P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li

  • (第2行 ~P+1行:第i+1行包含三个以空格分隔的整数:Ai、Bi和Li)

Output

  • Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.

Sample Input

5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6

Sample Output

4

解题思路

因为涉及到了免费的路径,所以可以考虑往图中加入若干条边权为0的路径,这里就先引入分层图的概念。
参考文章(大佬写的真的挺好的)

这篇文章写的真的挺好的,强烈建议大家去看一下,我在这里就不再班门弄斧了,就把图再贴过来加深一下理解在这里插入图片描述

搞懂分层图最短路之后,这道题就已经不是什么问题了,我们对于每一条路径都额外建出一条通往下一层的0权值边,同时,在每一层中各点的边权是相等的,与第一层保持一致,之后再跑 d i j k s t r a dijkstra dijkstra就可以了,最终的答案就存在每一层的最后一个点中,即 d i s [ i ∗ n + n ] dis[i*n+n] dis[in+n]

代码

#include<bits/stdc++.h>
using namespace std;
#define maxn 20000010
int n,p,k,l[maxn],vis[maxn],len,dis[maxn];//前i条边最长是多少 

struct node
{
	int y;
	int v;
	int nxt;
}e[maxn];

struct nod
{
	int x;
	int d;
};
bool operator<(nod x,nod y)
{
    return x.d>y.d;
}

void insert(int xx,int yy,int vv)
{
	e[++len].nxt=l[xx];
	l[xx]=len;
	e[len].y=yy;
	e[len].v=vv;
}

void dijkstra()
{
	priority_queue<nod>q;
	q.push(nod{1,0});
	dis[1]=0;
	while(!q.empty())
	{
		int t=q.top().x;
		q.pop();
		if(vis[t]) continue;
		vis[t]=1;
		for(int i=l[t];i;i=e[i].nxt)
		{
			int nt=e[i].y;
			if(dis[nt]>max(dis[t],e[i].v))
				{
					dis[nt]=max(dis[t],e[i].v);
					if(!vis[nt]) 
					q.push(nod{nt,dis[nt]});
				}
		}
	}
}

int main()
{
	cin>>n>>p>>k;
	memset(dis,0x3f,sizeof(dis));
	int u,v,w;
	for(int j=1;j<=p;j++)
	{
		cin>>u>>v>>w;
		for(int i=0;i<=k;i++)//建图记得i从0开始,别把第一层给漏掉
		{
			insert(u+i*n,v+i*n,w);
			insert(v+i*n,u+i*n,w);
			if(i!=k)//第k层就是最后一层了,所以第k层没有通往下一层的0权边
			{
				insert(u+i*n,v+(i+1)*n,0);
				insert(v+i*n,u+(i+1)*n,0);
			}
		}
	}
	dijkstra();
         int ans=100000100;
	for(int i=0;i<=p;i++) ans=min(ans,dis[i*n+n]); 
         if(ans!=100000100) printf("%lld",ans);
	else printf("-1");//不能通信的情况别漏掉哦
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值