-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshortestpath.cpp
More file actions
67 lines (51 loc) · 1.72 KB
/
Copy pathshortestpath.cpp
File metadata and controls
67 lines (51 loc) · 1.72 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
#include<iostream>
#include<vector>
#include<set>
using namespace std;
typedef pair<int,unsigned long long> PII;
typedef vector<PII> VPII;
typedef vector<VPII> VVPII;
int solve(int source_node, int node_count, int dst_node, VVPII& graph) {
const long long INF = 999999999999;
vector<unsigned long long> dist(node_count, INF);
set<PII> set_length_node;
dist[source_node] = 0;
set_length_node.insert(PII(0,source_node));
while (!set_length_node.empty()) {
PII top = *set_length_node.begin();
set_length_node.erase(set_length_node.begin());
int current_source_node = top.second;
for (auto& it : graph[current_source_node]) {
int adj_node = it.first;
int length_to_adjnode = it.second;
if (dist[adj_node] > length_to_adjnode + dist[current_source_node]) {
if (dist[adj_node] != INF) {
set_length_node.erase(set_length_node.find(PII(dist[adj_node],adj_node)));
}
dist[adj_node] = length_to_adjnode + dist[current_source_node];
set_length_node.insert(PII(dist[adj_node], adj_node));
}
}
}
return dist[dst_node];
}
int main(){
int places, dst;
while(cin >> places >> dst){
vector<VPII> graph;
for(int i = 0; i < places; ++i){
int id;
cin >> id;
VPII v;
for(int j = 0; j < places; ++j){
int weight;
cin >> weight;
if(weight != -1){
v.push_back({j, weight});
}
}
graph.push_back(v);
}
cout << solve(0, places, dst-1, graph) << endl;
}
}