首页 > 其他分享 >Navigation System(djkstra,反向建图,思维)

Navigation System(djkstra,反向建图,思维)

时间:2024-03-17 18:29:06浏览次数:36  
标签:int System system Polycarp 建图 djkstra path along intersection

The map of Bertown can be represented as a set of nn intersections, numbered from 11 to nn and connected by mm 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 vv to another intersection uu is the path that starts in vv, ends in uu and has the minimum length among all such paths.

Polycarp lives near the intersection ss and works in a building near the intersection tt. Every day he gets from ss to tt by car. Today he has chosen the following path to his workplace: p1p1, p2p2, ..., pkpk, where p1=sp1=s, pk=tpk=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 ss to tt.

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 ss, the system chooses some shortest path from ss to tt and shows it to Polycarp. Let's denote the next intersection in the chosen path as vv. If Polycarp chooses to drive along the road from ss to vv, then the navigator shows him the same shortest path (obviously, starting from vv as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection ww instead, the navigator rebuilds the path: as soon as Polycarp arrives at ww, the navigation system chooses some shortest path from ww to tt and shows it to Polycarp. The same process continues until Polycarp arrives at tt: 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][1,2,3,4] (s=1s=1, t=4t=4):

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

  1. When Polycarp starts at 11, the system chooses some shortest path from 11 to 44. There is only one such path, it is [1,5,4][1,5,4];
  2. Polycarp chooses to drive to 22, which is not along the path chosen by the system. When Polycarp arrives at 22, the navigator rebuilds the path by choosing some shortest path from 22 to 44, for example, [2,6,4][2,6,4] (note that it could choose [2,3,4][2,3,4]);
  3. Polycarp chooses to drive to 33, which is not along the path chosen by the system. When Polycarp arrives at 33, the navigator rebuilds the path by choosing the only shortest path from 33 to 44, which is [3,4][3,4];
  4. Polycarp arrives at 44 along the road chosen by the navigator, so the system does not have to rebuild anything.

Overall, we get 22 rebuilds in this scenario. Note that if the system chose [2,3,4][2,3,4] instead of [2,6,4][2,6,4] during the second step, there would be only 11 rebuild (since Polycarp goes along the path, so the system maintains the path [3,4][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 nn and mm (2≤n≤m≤2⋅1052≤n≤m≤2⋅105) — the number of intersections and one-way roads in Bertown, respectively.

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

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

The last line contains kk integers p1p1, p2p2, ..., pkpk (1≤pi≤n1≤pi≤n, all these integers are pairwise distinct) — the intersections along Polycarp's path in the order he arrived at them. p1p1 is the intersection where Polycarp lives (s=p1s=p1), and pkpk is the intersection where Polycarp's workplace is situated (t=pkt=pk). It is guaranteed that for every i∈[1,k−1]i∈[1,k−1] the road from pipi to pi+1pi+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

input

Copy

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

output

Copy

1 2

input

Copy

7 7
1 2
2 3
3 4
4 5
5 6
6 7
7 1
7
1 2 3 4 5 6 7

output

Copy

0 0

input

Copy

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

output

Copy

0 3

思路:

1,正向的想很复杂,反向建图更加有力

2,涉及最短路=》dj反向建图找最短距离=》正向的从p1到pk,看dist【u】==dist【v】+1?是否有多个路径:mx++,mn++;

3,为什么看是否有多个路径,因为最短路径事前确定,但是不一定是当前路径,也是mx》=mn的原因

代码:

int ksm(int a,int b){//快速幂
    int ans=1;
    while(b){
        if(b&1)ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
int log2(int a){
    return floor(log(a)/log(2));
}
int lower_bit(int x){
    return x&(-x);
}
struct qnode{
    int v;
    int c;
    qnode(int _v=0,int _c=0):v(_v),c(_c){}
    bool operator < (const qnode &r)const {
        return c>r.c;
    }
};
struct edge{
    int v,cost;
    edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<edge>g[maxj],z[maxj];
bool vis[maxj];
int dist[maxj];
void add1(int u,int v,int w){
    g[u].push_back(edge(v,w));
}
void add2(int u,int v,int w){
    z[u].push_back(edge(v,w));
}
int n;
void dj(int st){
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;++i)dist[i]=inf;
    priority_queue<qnode>que;
    while(!que.empty())que.pop();
    dist[st]=0;
    que.push(qnode(st,0));
    qnode tmp;
    while(!que.empty()){
        tmp=que.top();que.pop();
        int u=tmp.v;
        if(vis[u])continue;
        vis[u]=1;
        for(int i=0;i<g[u].size();++i){
            int v=g[tmp.v][i].v;
            int cost = g[u][i].cost;
            if(!vis[v]&&dist[v]>dist[u]+cost){
                dist[v]=dist[u]+cost;
                que.push(qnode(v,dist[v]));
            }
        }
    }
}
int a[maxj];
void solve(){
    int m;
    cin>>n>>m;
    rep(i,1,m){
        int x,y;cin>>x>>y;
        add1(y,x,1);
        add2(x,y,1);
    }
    int k;cin>>k;
    rep(i,1,k)cin>>a[i];
    dj(a[k]);
    int mn=0,mx=0;
    rep(i,1,k-1){
        int u=a[i],v=a[i+1];
        if(dist[v]==dist[u]-1){
            for(int j=0;j<z[u].size();++j){
                int to=z[u][j].v;
                if(dist[u]==dist[to]+1&&to!=v){
                    mx++;
                    break;
                }
            }
        }else{
            mn++,mx++;
        }
    }cout<<mn<<' '<<mx<<'\n';
}

标签:int,System,system,Polycarp,建图,djkstra,path,along,intersection
From: https://blog.csdn.net/m0_63054077/article/details/125909358

相关文章

  • 安装install.package("devtools")时报错 提示systemfonts,textshaping, ragg, gert依赖
    devtools可用conda,R的install.packages()以及wget等方式安装,这里我采用install.packages()安装,碰到systemfonts,textshaping,ragg,gert几个依赖包的安装错误。install.package("devtools")错误形式与解决,参考:https://www.cnblogs.com/shuaihe/p/17823059.html1.systemfonts解......
  • ffmpeg avformat_alloc_context System.NotSupportedException 不支持所指定的方法
    这个错误报了第二次了,网上搜不到靠谱的解决方案,赶快记录一下。第一个情况:报错如题目System.NotSupportedException不支持所指定的方法第二个情况:如果换autogen版本的话,我是用的5.1.2.3,切换到5.0或者其他版本的话,会提示avformat.59dllnotfound。这个报错根本原因是没找到对......
  • 程序调用系统的命令进行解释--system的调用
    在C++中,system 是一个函数,通常定义在 <cstdlib> 库中,它允许程序调用操作系统的命令行解释器(如Unix/Linux中的shell)来执行指定的命令。在CentOS7(一个基于Linux的操作系统)中,使用 system 函数可以执行几乎任何可以在命令行中运行的命令。这里是一个简单的例子,演示了如......
  • 使用systemctl来管理手动编译安装的Nginx
    FastDFS(https://github.com/happyfish100/fastdfs/wiki)推荐的nginx启动方式是直接执行/usr/local/nginx/sbin/nginx如果配成用systemctl管理的话,更符合常规使用习惯,而且可以设为开机启动,具体如下:/lib/systemd/system/nginx.service[Unit]Description=nginx-highperform......
  • ret2text(system($0))
    system($0)介绍在正常的编程或系统使用中,system($0) 本身并不会直接导致获取任何权限。$0 是一个特殊的shell变量,它代表当前脚本或程序的名字。在C语言中,如果你使用 system() 函数来执行一个shell命令,那么传递给 system() 的字符串会被当作shell命令来执行。如......
  • 【linux system V 消息队列】
    #简介消息队列就是一些消息的列表,或者说是一些消息组成的队列。消息队列与管道有些类似,消息队列可以认为是管道的改进版。相较于管道的先进先出准则,消息队列在读取时可以按照消息的类型进行读取,这也是消息队列的特点,它可以实现消息随机查询。消息发送时,需要将消息封装,然......
  • TN系统 TN System
    一、电力系统有一点直接接地,电气装置的外露可导电部分通过保护线与该接地点相连接。根据中性导体(N)和保护导体(PE)的配置方式,TN系统可分为如下三类:1 TN-C系统整个系统的N、PE线是合一的;2 TN-C-S系统系统中有一部分线路的N、PE线是合一的;3 TN-S系统整个系统的N、PE线是分开......
  • Android 11 SystemServer启动流程
    在Android11Zygote启动流程有提到,Zygote通过forkSystemServer,fork出SystemServer进程,并在SystemServer进程中调用handleSystemServerProcess返回一个Runnable //...... /*Forchildprocess*/if(pid==0){if(hasSecondZygote(abiList))......
  • 猫头虎分享已解决Bug || 分布式文件系统问题(Distributed File System Issue):DFSUnavail
    博主猫头虎的技术世界......
  • 探索谷歌的秘密花园:Google文件系统GFS之旅(Google File System)
    文章目录......