-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1005.cpp
69 lines (57 loc) · 1.52 KB
/
1005.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
#define SETTING ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std ;
int N, K, DEST;
int totalTime[1001] ;
int buildTime[1001] ;
int Topology_Sort[1001] ;
vector< vector <int> > graph(1001) ;
void find_answer()
{
queue<int> que ;
for(int i = 1 ; i <= N ; i++)
if(!Topology_Sort[i]) que.push(i) ;
while(!que.empty())
{
int curr = que.front() ;
que.pop() ;
if(curr == DEST) break ;
for(int i = 0 ; i < graph[curr].size() ; i++)
{
totalTime[graph[curr][i]] =
max(totalTime[graph[curr][i]], totalTime[curr] + buildTime[curr]) ;
Topology_Sort[graph[curr][i]]-- ;
if(!Topology_Sort[graph[curr][i]]) que.push(graph[curr][i]) ;
}
}
}
void input_setting()
{
int startPoint, endPoint ;
graph = vector< vector <int> > (1001) ;
memset(buildTime, 0, sizeof(int) * 1001) ;
memset(totalTime, 0, sizeof(int) * 1001) ;
memset(Topology_Sort, 0, sizeof(int) * 1001) ;
cin >> N >> K ;
for(int i = 1 ; i <= N ; i++)
cin >> buildTime[i] ;
for(int i = 0 ; i < K ; i++)
{
cin >> startPoint >> endPoint ;
graph[startPoint].push_back(endPoint) ;
Topology_Sort[endPoint]++ ;
}
cin >> DEST ;
}
int main()
{
SETTING ;
int T ;
cin >> T ;
for(int i = 0 ; i < T ; i++)
{
input_setting() ;
find_answer() ;
cout << totalTime[DEST] + buildTime[DEST] << "\n" ;
}
}