-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path** Childfree Time !! - Dijkstras Algo + Multisource BFS
65 lines (58 loc) · 1.3 KB
/
** Childfree Time !! - Dijkstras Algo + Multisource BFS
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
#include <bits/stdc++.h>
#define endl "\n"
#define int long long
using namespace std;
vector<pair<int,int>>g[1000001];
int in[1000001];
int out[1000001];
int dis[10000001];
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
while(t--){
int n,m;
cin >> n >> m;
for(int i=1;i<=m;i++)
{
int a,b;
cin >> a >> b;
int w;
cin >> w;
g[a].push_back({b,w});
in[b]++;
out[a]++;
}
queue<pair<int,int>> q;
for(int i=1;i<=n;i++)
{
if(in[i] == 0)
q.push({i,0});
}
int mx = 0;
while(!q.empty())
{
int x = q.front().first;
int d = q.front().second;q.pop();
if(d!=dis[x])
continue;
if(out[x] == 0)
{
mx = max(mx,d);
continue;
}
for(auto edge:g[x])
{
int v = edge.first;
int cost = edge.second;
if(dis[v] < d + cost + 2){
q.push({v,d + cost + 2});
dis[v] = d + cost + 2;
}
}
}
cout << mx << '\n';
}
}