-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha61_q2_farthest.cpp
More file actions
56 lines (45 loc) · 1.12 KB
/
Copy patha61_q2_farthest.cpp
File metadata and controls
56 lines (45 loc) · 1.12 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
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1005;
const int INF = 1e9 + 7;
vector <pair <int, int>> graph[MAX_N];
int dist[MAX_N];
bool visited[MAX_N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
int w;
cin >> w;
if (w != -1) graph[i].emplace_back(j, w);
}
}
for (int i = 1; i <= N; i++) dist[i] = INF;
priority_queue <pair <int, int>> pq;
pq.emplace(0, 1);
dist[1] = 0;
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (visited[u] == true) continue;
visited[u] = true;
for (auto [v, w] : graph[u]) {
if (visited[v] == false and dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
pq.emplace(-dist[v], v);
}
}
}
int ans = 0;
for (int i = 1; i <= N; i++) {
if (dist[i] == INF) {
ans = -1;
break;
}
else ans = max(ans, dist[i]);
}
cout << ans;
return 0;
}