D. Cow and Fields----------------------思维(最短路)

Bessie is out grazing on the farm, which consists of n fields connected by m bidirectional roads. She is currently at field 1, and will return to her home at field n at the end of the day.

The Cowfederation of Barns has ordered Farmer John to install one extra bidirectional road. The farm has k special fields and he has decided to install the road between two different special fields. He may add the road between two special fields that already had a road directly connecting them.

After the road is added, Bessie will return home on the shortest path from field 1 to field n. Since Bessie needs more exercise, Farmer John must maximize the length of this shortest path. Help him!

Input
The first line contains integers n, m, and k (2≤n≤2⋅105, n−1≤m≤2⋅105, 2≤k≤n) — the number of fields on the farm, the number of roads, and the number of special fields.

The second line contains k integers a1,a2,…,ak (1≤ai≤n) — the special fields. All ai are distinct.

The i-th of the following m lines contains integers xi and yi (1≤xi,yi≤n, xi≠yi), representing a bidirectional road between fields xi and yi.

It is guaranteed that one can reach any field from every other field. It is also guaranteed that for any pair of fields there is at most one road connecting them.

Output
Output one integer, the maximum possible length of the shortest path from field 1 to n after Farmer John installs one road optimally.

Examples
inputCopy

5 5 3
1 3 5
1 2
2 3
3 4
3 5
2 4
outputCopy
3
inputCopy
5 4 2
2 4
1 2
2 3
3 4
4 5
outputCopy
3
Note
The graph for the first example is shown below. The special fields are denoted by red. It is optimal for Farmer John to add a road between fields 3 and 5, and the resulting shortest path from 1 to 5 is length 3.

The graph for the second example is shown below. Farmer John must add a road between fields 2 and 4, and the resulting shortest path from 1 to 5 is length 3.

题意:
给定n个点,m条边,k个特殊点,问选两个特殊点构成最长的最短路是多少?

扫描二维码关注公众号,回复: 9835694 查看本文章

解析:
首先题目最长的最短路上限肯定就是原先的最短路。假设u,v为特殊点 那么最短路min(d1[u]+dn[v]+1,d1[v]+dn[u]+1) .
d1代表:从一号点到其他点的最短距离
dn代表:从n号点到其他点的最短距离

假设 d1[u]+dn[v]<=d1[v]+dn[u]
要求d1[u]+dn[v]的最大值并且满足 d1[u]-dn[u]<=d1[v]-dn[v] 。
因此对特殊点按照d1[u]-dn[u] 排序
然后遍历一下。用一个变量储存特殊点到1的最短距离记为mx。每次更新mx最大值。
最后输出最大值和加边之前的最短路中的最小值即可。(因为最长的最短路的上限一定是之前没连特殊点的最短路)

#include<bits/stdc++.h>
using namespace std;
const int N=4e5+1000;
vector<int> G[N];
int d1[N],dn[N];
int n,m,k,u,v;
int a[N];
bool st[N];
bool cmp(int u,int v)
{
	return d1[u]-dn[u]<d1[v]-dn[v];
}
void spfa(int u,int *d1)
{
	d1[u]=0;
	queue<int> q;
	q.push(u);
	while(q.size())
	{
		int t=q.front();
		q.pop();
		for(int i=0;i<G[t].size();i++)
		{
			int j=G[t][i];
			if(d1[j]==-1)
			{
				q.push(j);
				d1[j]=d1[t]+1;
			}
		 } 
	}
}
int main()
{
	scanf("%d %d %d",&n,&m,&k);
	memset(d1,-1,sizeof d1);memset(dn,-1,sizeof dn);
	for(int i=1;i<=k;i++)  scanf("%d",&a[i]);
	for(int i=1;i<=m;i++)
	{
		scanf("%d %d",&u,&v);
		G[u].push_back(v);G[v].push_back(u);
	}
	spfa(1,d1);spfa(n,dn);
	sort(a+1,a+1+k,cmp);
	int mx=d1[a[1]];
	int ans=0;
	for(int i=2;i<=k;i++)
	{
		ans=max(mx+dn[a[i]]+1,ans);
		mx=max(mx,d1[a[i]]);
	}
	cout<<min(ans,d1[n])<<endl;
 } 
发布了491 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43690454/article/details/104684286
今日推荐