Luo Gu -2296 finding your way

Title Description
In a directed graph G, a length of each side are now given start and end points, you find a path from the origin to the destination in the figure, this path satisfies the following condition:
all points on the path pointed out side and end points are communicating directly or indirectly.
So that the shortest path in a condition is satisfied.
Note: there may be heavy graph G and side loopback, the subject does not ensure that end edge.
Please output length qualifying path.
Input Output Format
Input Format:
first line separated by a space of two integers n and m, there is a view showing the n points and edges m.
The next two lines each m integers x, y, between separated by a space, there is an edge point represents a point y from the point x.
The last line has two integers separated by a space s, ts, ts, t, expressed as a starting point s, the end point as t.
Output format:
only the output line, comprising an integer representing the length of the shortest path to meet the described subject. If such a path does not exist, the output of -1.

Sample Input Output
Input Sample # 1:
. 3 2
1 2
2 1
1. 3

Output Sample # 1:
-1

Input Sample # 2:
. 6. 6
. 1 2
. 1. 3
2. 6
2. 5
. 4. 5
. 3. 4
. 1. 5

Output Sample # 2:
3

Explanation: The first condition is not satisfied can point to get rid of the rest of the map again seeking the shortest path just fine. We can build the reverse side. Point shortest path from t, we can not reach the point is not satisfied. Re-build a positive view of, if not satisfied directly to the point, as the point is not satisfied. Finally, the point of view of construction meet on OK

#include<stdio.h>
#include<queue>
#include<cstring>
#include<iostream>
#include<cstdlib>
#define N 10005
#define M 200003
#define INF 200003
using namespace std;
int head[N]={0};
int next1[M]={0};
int V[M]={0};
bool vis[N]={0};
int W[M]={0};
int tot=0;
int dist[N]={0};
int a[M]={0},b[M]={0};
bool H[N]={0};
int n=0,m=0,s=0,t=0;
void add(int x,int y,int c){
    tot++;
    next1[tot]=head[x];
    V[tot]=y;W[tot]=c;
    head[x]=tot;
}
priority_queue<pair<int,int> > que;
void dij(int x){
    fill(dist,dist+N,INF);
    memset(vis,0,sizeof(vis));
    dist[x]=0;
    que.push(make_pair(0,x));
    while(que.size()){
        pair<int,int> temp=que.top();que.pop();
        int from=temp.second;if(vis[from]) continue;
        vis[from]=true;
        for(int i=head[from];i;i=next1[i]){
            int to=V[i],w=W[i];
            if(dist[to]>dist[from]+w){
                dist[to]=dist[from]+w;
                que.push(make_pair(-dist[to],to));
            }
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        scanf("%d%d",&a[i],&b[i]);
        add(b[i],a[i],1);
    }
    scanf("%d%d",&s,&t);
    dij(t);
    memset(head,0,sizeof(head));tot=0;
    for(int i=1;i<=m;i++) add(a[i],b[i],1);
    for(int i=1;i<=n;i++){
        int temp=dist[i];
        for(int j=head[i];j;j=next1[j]){
            int to=V[j];
            temp=max(temp,dist[to]);
        }
        if(temp>=INF) H[i]=1;
    }
    memset(head,0,sizeof(head));tot=0;
    for(int i=1;i<=m;i++){
        if(H[a[i]]||H[b[i]]) continue;
        add(a[i],b[i],1);
    }
    dij(s);
    if(dist[t]>=INF) printf("-1\n");
    else printf("%d\n",dist[t]);
    return 0;
}



Guess you like

Origin blog.csdn.net/mkopvec/article/details/92388164