Codeforces Round #625 D. Navigation System

Link
meaning of the questions:
connectivity directed graph
have been a planned path from s to t, you're driving in accordance with the planned route
at the time of the starting point, a navigation system gives the shortest path from the origin to the end of the
planned path and navigation possible to the shortest path different
so do not move when you follow the path of a point before the planned departure, the shortest route navigation may be reset
when the shortest path is not unique, navigation will be given a random
and asked to navigate a minimum and a maximum reset the shortest path a few time
: thinking
shortest
first built FIG backwards, bfs by calculating the distance of each point to the end point \ (D \)
when the \ (U \) is moved to the \ (V \) when:
when \ (d [v]> -1 \) d [u] , the reset certainly
when \ (d [v] = d [u] -1 \) when, if there is a point \ (w (w \ neq v ) \) makes \ ( D [W] D = +. 1 [V] \) , it may reset
the code:

#include<bits/stdc++.h>

using namespace std;

const int N=200010;

int n,m,k;
int u[N],v[N];
int path[N];
int cnt,to[N],nxt[N],head[N];
int d[N];

void addedge(int u,int v) {
    cnt++;
    to[cnt]=v;
    nxt[cnt]=head[u];
    head[u]=cnt;
}
void bfs() {
    queue<int>q;
    q.push(path[k]);
    d[path[k]]=1;
    while(q.size()) {
        auto u=q.front();
        q.pop();
        for(int i=head[u];i;i=nxt[i]) {
            int v=to[i];
            if(d[v]) continue;
            d[v]=d[u]+1;
            q.push(v);
        }
    }
}
void solve() {
    cnt=0;
    memset(head,0,sizeof head);
    for(int i=1;i<=m;i++) addedge(u[i],v[i]);
    int mn=0,mx=0;
    for(int i=1;i<k;i++) {
        if(d[path[i+1]]>=d[path[i]]) mx++,mn++;
        else for(int j=head[path[i]];j;j=nxt[j]) {
            int x=to[j];
            if(d[x]==d[path[i]]-1&&x!=path[i+1]){mx++;break;}
        }
    }
    cout<<mn<<" "<<mx<<endl;
}
int main() {
    //freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++) {
        cin>>u[i]>>v[i];
        addedge(v[i],u[i]);
    } 
    cin>>k;
    for(int i=1;i<=k;i++) cin>>path[i];
    bfs();
    solve();
    return 0;
}

Guess you like

Origin www.cnblogs.com/c4Lnn/p/12512918.html