-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1005.cpp
More file actions
70 lines (68 loc) · 1.35 KB
/
1005.cpp
File metadata and controls
70 lines (68 loc) · 1.35 KB
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
70
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
int n,m,t,k;
vector<int>v[1001];
int cost[1001];
int dp[1001];
long long ans;
int bfs()
{
queue<pair<int,int> >q;
q.push(make_pair(k,cost[k]));
while(!q.empty())
{
int way = q.front().first;
long long c = q.front().second;
q.pop();
if(v[way].size()==0)
{
if(c>ans) ans = c;
continue;
}
for (int i = 0; i < v[way].size(); ++i)
{
if(dp[v[way][i]]<c+cost[v[way][i]])
{
q.push(make_pair(v[way][i],c+cost[v[way][i]]));
dp[v[way][i]] = c+cost[v[way][i]];
}
}
}
return 0;
}
int main(int argc, char** argv)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a,b;
cin>>t;
for (int i = 0; i < t; ++i)
{
cin>>n>>m;
for (int j = 1; j <= n; ++j)
{
cin>>cost[j];
}
for (int j = 0; j < m; ++j)
{
cin>>a>>b;
v[b].push_back(a);
}
cin>>k;
bfs();
cout<<ans<<"\n";
for (int j = 1; j <= n; ++j)
{
cost[j] = 0;
dp[j] = 0;
v[j].clear();
}
ans = 0;
}
return 0;
}