日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

神奇的道路

發(fā)布時間:2024/9/5 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 神奇的道路 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目背景

在某教練的強迫之下,我一個蒟蒻居然又出題了!!!出題了!!!(數(shù)據(jù)太水別找我qwq)

好的,JL說好的一題100快拿來

題目描述

我們有一個有向圖,每條邊權(quán)值為1,我們要在上面找一條神奇的道路滿足:

1.神奇的道路上的所有點的出邊所指向的點都直接或間接與終點連通。

2.在滿足條件1的情況下使路徑最短。

3.該路徑是從起點到終點的路徑

垃圾出題人表示找不到這條路徑,所以希望你幫忙qwq

輸入格式

第一行有兩個用一個空格隔開的整數(shù) n 和 m,表示圖有 n個點和 m 條邊。

接下來的 m 行每行 2 個整數(shù) x,y,之間用一個空格隔開,表示有一條邊從點 x 指向點y。

最后一行有兩個用一個空格隔開的整數(shù) s, t,表示起點為 s,終點為 t。

輸出格式

輸出只有一行,包含一個整數(shù),表示滿足題目描述的最短路徑的長度。如果這樣的路徑不存在,輸出“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。

數(shù)據(jù)范圍

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; }

?

轉(zhuǎn)載于:https://www.cnblogs.com/hrj1/p/11357179.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的神奇的道路的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。