神奇的道路
題目背景
在某教練的強迫之下,我一個蒟蒻居然又出題了!!!出題了!!!(數據太水別找我qwq)
好的,JL說好的一題100快拿來
題目描述
我們有一個有向圖,每條邊權值為1,我們要在上面找一條神奇的道路滿足:
1.神奇的道路上的所有點的出邊所指向的點都直接或間接與終點連通。
2.在滿足條件1的情況下使路徑最短。
3.該路徑是從起點到終點的路徑
垃圾出題人表示找不到這條路徑,所以希望你幫忙qwq
輸入格式
第一行有兩個用一個空格隔開的整數 n 和 m,表示圖有 n個點和 m 條邊。
接下來的 m 行每行 2 個整數 x,y,之間用一個空格隔開,表示有一條邊從點 x 指向點y。
最后一行有兩個用一個空格隔開的整數 s, t,表示起點為 s,終點為 t。
輸出格式
輸出只有一行,包含一個整數,表示滿足題目描述的最短路徑的長度。如果這樣的路徑不存在,輸出“orz”(不含雙引號)
輸入輸出樣例
輸入#1
3 2 1 2 2 1 1 3輸出#1
orz輸入#2
7 8 5 2 3 1 5 3 1 6 3 6 6 7 2 7 2 4 5 7輸出#2
3說明/提示
對于樣例一: 起點1與終點3不連通,所以滿足題目描述的路徑不存在,故輸出orz。
數據范圍
30%保證是連通圖
100% 0<n≤10000,0<m≤200000,0<x,y,s,t≤n,x,s≠t。
?
?
早上毒瘤出題人告訴我跑最長路有60分。。。。。。
?
#include <cstdio> #include <algorithm> #include <cstring> using namespace std;int tot,total,n,m,ss,tt,l[500050],r[500050],pre[500050],last[10050],other[500050];int que[10050],d[10050];bool judge[10050],vis[10050],point[10050];inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-'){w=-1;}ch=getchar();}while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}return s*w; }void add(int u,int v) {pre[++tot]=last[u];last[u]=tot;other[tot]=v; }void bfs(int x) {int h=0,t=1;que[1]=x;vis[x]=1;point[x]=1;total++;while (h<t) {int cur=que[++h];for (int p=last[cur]; p; p=pre[p]) {int q=other[p];if (!vis[q]) {vis[q]=1;que[++t]=q;total++;point[q]=1;}}} }void spfa(int x) {int h=0,t=1;que[1]=x;memset(d,127,sizeof d);d[x]=0;while (h<t) {int cur=que[++h];vis[cur]=0;for (int p=last[cur]; p; p=pre[p]) {int q=other[p];if (!point[q]) continue; if (judge[q]) continue;if (d[q]>d[cur]+1) {d[q]=d[cur]+1;if (!vis[q]) {vis[q]=1;que[++t]=q;}}}} }int main() {n=read();m=read();for (int i=1; i<=m; i++){l[i]=read();r[i]=read();}ss=read();tt=read();for (int i=1; i<=m; i++) add(r[i],l[i]);bfs(tt);if (!point[ss]) {printf("orz\n");return 0; }for (int i=1; i<=n; i++) {if (point[i]) continue;for (int p=last[i]; p; p=pre[p]) {int q=other[p];judge[q]=1;}}memset(que,0,sizeof que);memset(vis,0,sizeof vis);memset(last,0,sizeof last);tot=0;for (int i=1; i<=m; i++) add(l[i],r[i]);spfa(ss);printf("%d",d[tt]);return 0; }?
轉載于:https://www.cnblogs.com/hrj1/p/11357179.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: GetWindowRect GetCli
- 下一篇: MOSS 2007 User Profi