codeup問題 A 關鍵路徑 提供測試樣例

2021-10-03 22:29:39 字數 2721 閱讀 2437

時間限制 : 1.000 sec 記憶體限制 : 128 mb

題目描述

描述:圖的連線邊上的資料表示其權值,帶權值的圖稱作網。

上圖可描述為頂點集為(a,b,c,d,e)

邊集及其權值為(始點,終點 權值):

a b 3

a c 2

b d 5

c d 7

c e 4

d e 6

網的源點是入度為0的頂點,匯點是出度為0的頂點。網的關鍵路徑是指從源點到匯點的所有路徑中,具有最大路徑長度的路徑。上圖中的關鍵路徑為a->c->d->e,其權值之和為關鍵路徑的長度為15。

本題的要求是根據給出的網的鄰接矩陣求該網的關鍵路徑及其長度。

輸入

第一行輸入乙個正整數n(1<=n<=5),其代表測試資料數目,即圖的數目

第二行輸入x(1<=x<=15)代表頂點個數,y(1<=y<=19)代表邊的條數

第三行給出圖中的頂點集,共x個小寫字母表示頂點

接下來每行給出一條邊的始點和終點及其權值,用空格相隔,每行代表一條邊。

輸出

第乙個輸出是圖的關鍵路徑(用給出的字母表示頂點, 用括號將邊括起來,頂點逗號相隔)

第二個輸出是關鍵路徑的長度

每個矩陣對應上面兩個輸出,兩個輸出在同一行用空格間隔,每個矩陣的輸出佔一行。

樣例輸入

25 6

abcde

a b 3

a c 2

b d 5

c d 7

c e 4

d e 6

4 5abcd

a b 2

a c 3

a d 4

b d 1

c d 3

樣例輸出

(a,c) (c,d) (d,e) 15

(a,c) (c,d) 6

注意:每次求出ve[u] == vl[v] - weight表示這條邊是關鍵路徑中的一條邊。如果有多條關鍵路徑,輸出的時候只需要輸出一條關鍵路徑就行了。所以需要新建乙個vector來儲存關鍵路徑。

樣例:14

4abcd

a b 3

a c 3

b d 4

c d 4

(a,b)

(b,d)

7(a,c)

(c,d)

7//只需要兩條路徑中輸出一條即可;

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace std;

const

int maxn =

1010

;struct node

;vector g[maxn]

;vector<

int> pre[maxn]

;stack<

int> toporder;

unordered_map<

char

,int

> map1;

unordered_map<

int,

char

> map2;

int n, x, y, w, ve[maxn]

, vl[maxn]

, indegree[maxn]

;void

maketoporder()

while

(!q.

empty()

)}}int

findendv()

return maxv;

}void

criticalpath()

}}//(a,c) (c,d) (d,e) 15

for(

int u =

0; u < x; u++)}

}int prei;

for(

int i =

0; i < x; i++)}

while

(pre[prei]

.size()

!=0)}

intmain()

fill

(indegree, indegree + maxn,0)

; map1.

clear()

; map2.

clear()

; cin >> x >> y;

for(

int i =

0; i < x; i++

)for

(int i =

0; i < y; i++

)maketoporder()

;int end1 =

findendv()

;//找到關鍵路徑長度

fill

(vl, vl + maxn, end1)

;//初始化vl

criticalpath()

;printf

("%d\n"

, end1);}

return0;

}

Codeup關鍵路徑 關鍵路徑

時間限制 1 sec 記憶體限制 128 mb 提交 261 解決 90 提交 狀態 討論版 命題人 外部匯入 描述 圖的連線邊上的資料表示其權值,帶權值的圖稱作網。上圖可描述為頂點集為 a,b,c,d,e 邊集及其權值為 始點,終點 權值 a b 3 a c 2 b d 5 c d 7 c e 4...

codeup 關鍵路徑 模板

include include include include include using namespace std const int size 100 第一行輸入乙個正整數n 1 n 5 其代表測試資料數目,即圖的數目 第二行輸入x 1 x 15 代表頂點個數,y 1 y 19 代表邊的條數 ...

codeup 問題 D 最短路徑

題目描述 有n個城市m條道路 n 1000,m 10000 每條道路有個長度,請找到從起點s到終點t的最短距離和經過的城市名。輸入輸入包含多組測試資料。每組第一行輸入四個數,分別為n,m,s,t。接下來m行,每行三個數,分別為兩個城市名和距離。輸出每組輸出佔兩行。第一行輸出起點到終點的最短距離。第二...