Codeforces Round #625 (Div. 2, based on Technocup 2020 Final Round)Navigation SystemD(最短路+思维)

outputstandard output
The map of Bertown can be represented as a set of n intersections, numbered from 1 to n and connected by m one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection v to another intersection u is the path that starts in v, ends in u and has the minimum length among all such paths.

Polycarp lives near the intersection s and works in a building near the intersection t. Every day he gets from s to t by car. Today he has chosen the following path to his workplace: p1, p2, …, pk, where p1=s, pk=t, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp’s path beforehand (it is fixed), and it is not necessarily one of the shortest paths from s to t.

Polycarp’s car has a complex navigation system installed in it. Let’s describe how it works. When Polycarp starts his journey at the intersection s, the system chooses some shortest path from s to t and shows it to Polycarp. Let’s denote the next intersection in the chosen path as v. If Polycarp chooses to drive along the road from s to v, then the navigator shows him the same shortest path (obviously, starting from v as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection w instead, the navigator rebuilds the path: as soon as Polycarp arrives at w, the navigation system chooses some shortest path from w to t and shows it to Polycarp. The same process continues until Polycarp arrives at t: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.

Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path [1,2,3,4] (s=1, t=4):

Check the picture by the link http://tk.codeforces.com/a.png

When Polycarp starts at 1, the system chooses some shortest path from 1 to 4. There is only one such path, it is [1,5,4];
Polycarp chooses to drive to 2, which is not along the path chosen by the system. When Polycarp arrives at 2, the navigator rebuilds the path by choosing some shortest path from 2 to 4, for example, [2,6,4] (note that it could choose [2,3,4]);
Polycarp chooses to drive to 3, which is not along the path chosen by the system. When Polycarp arrives at 3, the navigator rebuilds the path by choosing the only shortest path from 3 to 4, which is [3,4];
Polycarp arrives at 4 along the road chosen by the navigator, so the system does not have to rebuild anything.
Overall, we get 2 rebuilds in this scenario. Note that if the system chose [2,3,4] instead of [2,6,4] during the second step, there would be only 1 rebuild (since Polycarp goes along the path, so the system maintains the path [3,4] during the third step).

The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp’s path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?

Input
The first line contains two integers n and m (2≤n≤m≤2⋅105) — the number of intersections and one-way roads in Bertown, respectively.

Then m lines follow, each describing a road. Each line contains two integers u and v (1≤u,v≤n, u≠v) denoting a road from intersection u to intersection v. All roads in Bertown are pairwise distinct, which means that each ordered pair (u,v) appears at most once in these m lines (but if there is a road (u,v), the road (v,u) can also appear).

The following line contains one integer k (2≤k≤n) — the number of intersections in Polycarp’s path from home to his workplace.

The last line contains k integers p1, p2, …, pk (1≤pi≤n, all these integers are pairwise distinct) — the intersections along Polycarp’s path in the order he arrived at them. p1 is the intersection where Polycarp lives (s=p1), and pk is the intersection where Polycarp’s workplace is situated (t=pk). It is guaranteed that for every i∈[1,k−1] the road from pi to pi+1 exists, so the path goes along the roads of Bertown.

Output
Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.

Examples
inputCopy
6 9
1 5
5 4
1 2
2 3
3 4
4 1
2 6
6 4
4 2
4
1 2 3 4
outputCopy
1 2
inputCopy
7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7
outputCopy
0 0
inputCopy
8 13
8 7
8 6
7 5
7 4
6 5
6 4
5 3
5 2
4 3
4 2
3 1
2 1
1 8
5
8 7 5 2 1
outputCopy
0 3
思路:其实画图一看就能想到,肯定要求每个节点到p[k]节点的最短路,并且还要求出来每个节点的最短路中有几个选择。由于是单向路,我们寻找每个节点到p[k]节点的最短路的时候需要反向建边。上面的这些操作完成后我们就要求最小值和最大值了。
①如果下一个经过的不是当前节点的最短路经过的节点,那么_min++,_max++。
②上一个条件满足了。
1)下一个节点是唯一一个最短路经过的节点,_max就不会变了。
2)下一个节点不是唯一一个,_max++。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=2e5+100;
struct edge{
	int to;
	int next;
}e[maxx<<1];
struct node{
	int _dis;
	vector<int> q;
}p[maxx];
struct Node{
	int v;
	int w;
	Node(int x,int y)
	{
		v=x;w=y;
	}
	bool operator<(const Node &a)const{
		return w<a.w;
	}
};
vector<int> pp[maxx];
int head[maxx<<1],a[maxx],vis[maxx];
int n,m,k,tot;

/*----------事前准备----------*/
inline void init()
{
	tot=0;
	memset(vis,0,sizeof(vis));
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++) p[i]._dis=inf,p[i].q.clear(),pp[i].clear();
}
inline void add(int u,int v)
{
	e[tot].next=head[u],e[tot].to=v,head[u]=tot++;
}
/*-----------最短路-----------*/
inline void Dijkstra(int u)
{
	p[u]._dis=0;
	priority_queue<Node> q;
	q.push(Node(u,p[u]._dis));
	while(q.size())
	{
		Node cc=q.top();
		q.pop();
		if(vis[cc.v]) continue;
		vis[cc.v]=1;
		for(int i=head[cc.v];i!=-1;i=e[i].next)
		{
			int to=e[i].to;
			if(p[to]._dis>p[cc.v]._dis+1)
			{
				p[to]._dis=p[cc.v]._dis+1;
				q.push(Node(to,p[to]._dis));
			}
		}
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	init();
	int x,y;
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		add(y,x);
		pp[x].push_back(y);
	}
	scanf("%d",&k);
	for(int i=1;i<=k;i++) scanf("%d",&a[i]);
	Dijkstra(a[k]);
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<pp[i].size();j++)
		{
			if(p[pp[i][j]]._dis+1==p[i]._dis) p[i].q.push_back(pp[i][j]);
		}
	}
	for(int i=1;i<=n;i++) sort(p[i].q.begin(),p[i].q.end()),p[i].q.erase(unique(p[i].q.begin(),p[i].q.end()),p[i].q.end());
	int _min=0,_max=0;
	for(int i=1;i<=k-1;i++)
	{
		int pos=lower_bound(p[a[i]].q.begin(),p[a[i]].q.end(),a[i+1])-p[a[i]].q.begin();
		if(pos==p[a[i]].q.size()||a[i+1]!=p[a[i]].q[pos]) _min++,_max++;
		else if(p[a[i]].q[pos]==a[i+1]&&p[a[i]].q.size()>1) _max++;
	}
	cout<<_min<<" "<<_max<<endl;
	return 0;
}

努力加油a啊,(o)/~

发布了502 篇原创文章 · 获赞 26 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104635097