forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlost.cpp
104 lines (85 loc) · 2.39 KB
/
lost.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
ll inf = (ll) 1 << 59;
struct node {
vector<ll> neighbors;
ll bestcon;
ll bestval;
ll parent;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
vector<node> nodes;
node start;
start.bestcon = 0;
start.bestval = 0;
start.parent = -1;
nodes.push_back(start);
ll n, m;
cin >> n >> m;
map<string, ll> to_num;
to_num["English"] = 0;
for(ll i = 1; i <= n; i++) {
string name;
cin >> name;
to_num[name] = i;
node temp;
temp.bestcon = inf;
temp.bestval = inf;
temp.parent = -1;
nodes.push_back(temp);
}
vector<vector<ll>> adj;
adj.resize(n+1, vector<ll>(n+1, inf));
for(ll i = 0; i <= n; i++) {
adj[i][i] = 0;
}
for(ll i = 0; i < m; i++) {
string s1, s2;
ll dist;
cin >> s1 >> s2 >> dist;
nodes[to_num[s1]].neighbors.push_back(to_num[s2]);
nodes[to_num[s2]].neighbors.push_back(to_num[s1]);
adj[to_num[s1]][to_num[s2]] = dist;
adj[to_num[s2]][to_num[s1]] = dist;
}
queue<ll> q;
q.push(0);
while(!q.empty()) {
ll curr = q.front();
q.pop();
for(auto i : nodes[curr].neighbors) {
// If we can get to a node with less translations
if(nodes[curr].bestcon + 1 < nodes[i].bestcon) {
nodes[i].bestcon = nodes[curr].bestcon + 1;
nodes[i].bestval = nodes[curr].bestval + adj[curr][i];
nodes[i].parent = curr;
q.push(i);
}
// If we can get to a node with the same translations and less money
else if(nodes[curr].bestcon + 1 == nodes[i].bestcon) {
if(adj[i][curr] <= adj[i][nodes[i].parent]) {
nodes[i].bestcon = nodes[curr].bestcon + 1;
nodes[i].bestval = nodes[curr].bestval + adj[curr][i];
nodes[i].parent = curr;
q.push(i);
}
}
}
}
ll total = 0;
for(ll i = 1; i < nodes.size(); i++) {
node curr = nodes[i];
if(curr.bestcon > inf/2) {
cout << "Impossible" << endl;
return 0;
}
total += adj[curr.parent][i];
}
cout << total << endl;
}