-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet743.cpp
90 lines (80 loc) · 1.95 KB
/
leet743.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <string.h>
#include <cstring>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <functional>
#include <cmath>
#include <set>
#include <map>
#include <assert.h>
#include <stack>
#include <unordered_map>
using namespace std;
struct Node{
int id;
int dist;
Node(int x,int d):id(x),dist(d){}
bool operator<(const Node& node) const
{
return dist>node.dist;
}
};
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int n, int k) {
vector<vector<int>> link(n+1,vector<int>(n+1,-1));
for(auto v:times)
{
int x = v[0];
int y =v[1];
int w = v[2];
link[x][y] = w;
}
vector<int> label(n+1,1e9);
vector<bool> certain(n+1,false);
int INF = 1e9;
priority_queue<Node> pq;
for(int i=1;i<=n;i++)
{
Node node(i,INF);
if(i==k)
node.dist = 0;
pq.push(node);
}
while(pq.empty()==false)
{
while(!pq.empty()&&certain[pq.top().id])
pq.pop();
if(pq.empty())
break;
Node src = pq.top();
pq.pop();
int id = src.id;
int dist = src.dist;
label[id] = dist;
certain[id] = true;
for(int i=1;i<=n;i++)
{
if(link[id][i]!=-1 && label[i]>label[id]+link[id][i])
{
Node node(i,label[id]+link[id][i]);
pq.push(node);
}
}
}
return *max_element(label.begin()+1,label.end())==INF?-1:*max_element(label.begin()+1,label.end());
}
};
int main()
{
Solution sol;
vector<vector<int>> t = {{3,4,1},{2,1,1},{2,3,1}};
sol.networkDelayTime(t,4,2);
return 0;
}