UOJ # 556. Magical road

Topic background

Under compulsion of a coach, I'm a konjac actually has a question for our! ! ! Out of the question! ! ! (I do not find too much water data qwq)

Good, JL said that a good title brought 100 fast

Title Description

We have a directed graph, each edge weight of 1, we have to find a magic way to meet the above:

All points on the side of the road 1. The magic points and end points are communicating directly or indirectly.

2. In the case that the shortest path condition 1 is satisfied.

3. The path is a path from the origin to the destination

Garbage out of the question said they can not find this path, so I hope you help qwq

Input Format

The first two rows separated by a space of the 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, t, expressed as a starting point s, the end point as t.

Output Format

Only 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 "orz" (without quotation marks)

Sample input and output

Input # 1

3 2  
1 2  
2 1  
1 3  

Output # 1

orz

Input # 2

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

Output # 2

3

Description / Tips

For a sample: a start point and end point 3 is not in communication, the subject is satisfied described path does not exist, so the output orz.

data range

FIG communication is guaranteed 30%

100% 0<n≤10000,0<m≤200000,0<x,y,s,t≤n,x,s≠t。

Thinking

Shortest + DFS

Code:

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=200010;
const int oo=0x3f3f3f3f;

int n,m,x,y,s,t;
int tot,head[N];
int cnt[N],dis[N];
int vis[N],kdl[N];

struct no {
	int to,nxt,dis;
} map[N];

struct nod {
	int pos,num;
};

queue<int> q;
queue<int> p;

void add(int u,int v,int w) {
	tot++;
	map[tot].to=v;
	map[tot].nxt=head[u];
	map[tot].dis=w;
	head[u]=tot;
}

void spfa(int s) {
	memset(dis,0x3f,sizeof(dis));
	dis[s]=0;
	q.push(s);
	while(q.size()) {
		int tmp=q.front();
		q.pop();
		for(int i=head[tmp]; i; i=map[i].nxt) {
			int v=map[i].to;
			if(vis[v])
				continue;
			if(dis[v]>dis[tmp]+map[i].dis) {
				dis[v]=dis[tmp]+map[i].dis;
				q.push(v);
			}
		}
	}
}

void dfs(int s) {
	p.push(s);
	while(p.size()) {
		int tmp=p.front();
		p.pop();
		for(int i=head[tmp]; i; i=map[i].nxt) {
			int v=map[i].to;
			cnt[v]--;
			if(!kdl[v]) {
				p.push(v);
				kdl[v]=1;
			}
		}
	}
}

int main () {
	scanf("%d%d",&n,&m);
	for(int i=1; i<=m; i++) {
		scanf("%d%d",&x,&y);
		add(y,x,1);
		cnt[x]++;
	}
	scanf("%d%d",&s,&t);
	dfs(t);
	for(int i=1; i<=n; i++)
		if(cnt[i])
			vis[i]=1;
	spfa(t);
	if(dis[s]>=oo)
		printf("orz\n");
	else
		printf("%d\n",dis[s]);
	return 0;
}

Guess you like

Origin www.cnblogs.com/mysh/p/11355051.html