-
Notifications
You must be signed in to change notification settings - Fork 0
/
1087.cpp
134 lines (106 loc) · 3.19 KB
/
1087.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <algorithm>
#include <utility>
#include <queue>
#include <string>
#include <iostream>
#include <vector>
#include <climits>
#include <functional>
#include <map>
using namespace std;
const int MAXN = 2000 + 10;
struct Node;
struct Edge;
string id[MAXN];
struct Node {
int dis;
vector<Edge> edges;
int val, cnt, sum_val, num;
Node *fr;
Node() : dis(INT_MAX), val(0), cnt(0), sum_val(0), num(0) {
edges.clear();
fr = nullptr;
}
} nodes[MAXN];
struct Edge {
Node *fr, *to;
int w;
Edge(Node *fr, Node *to, int w) : fr(fr), to(to), w(w) {}
};
void add_edge(int u, int v, int w) {
nodes[u].edges.push_back(Edge(&nodes[u], &nodes[v], w));
nodes[v].edges.push_back(Edge(&nodes[v], &nodes[u], w));
}
using info = pair<int, Node*>;
void dijkstra(Node *x) {
priority_queue<info, std::vector<info>, std::greater<info>> pq;
x->dis = 0;
x->cnt = 1;
x->num = 1;
pq.push(make_pair(0, x));
while (!pq.empty()) {
info v = pq.top();
pq.pop();
if (v.first != v.second->dis) continue;
for (auto &e : v.second->edges) {
if (e.to->dis > e.w + v.second->dis) {
e.to->cnt = v.second->cnt;
e.to->dis = e.w + v.second->dis;
e.to->sum_val = e.to->val + v.second->sum_val;
e.to->num = v.second->num + 1;
e.to->fr = v.second;
pq.push(make_pair(e.to->dis, e.to));
} else if (e.to->dis == e.w + v.second->dis) {
e.to->cnt += v.second->cnt;
if (e.to->sum_val < e.to->val + v.second->sum_val) {
e.to->sum_val = e.to->val + v.second->sum_val;
e.to->num = v.second->num + 1;
e.to->fr = v.second;
} else if (e.to->sum_val == e.to->val + v.second->sum_val) {
if (e.to->num > v.second->num + 1) {
e.to->num = v.second->num + 1;
e.to->fr = v.second;
}
}
}
}
}
}
void print_path(Node *v, vector<string> &ans) {
if (!v->fr) ans.push_back(id[v - nodes]);
else {
print_path(v->fr, ans);
ans.push_back(id[v - nodes]);
}
return;
}
int main() {
int n, m;
string s;
map<string, int> mp;
cin >> n >> m >> s;
id[0] = s;
mp[id[0]] = 0;
for (int i = 1; i < n; i++) {
cin >> id[i] >> nodes[i].val;
mp[id[i]] = i;
}
for (int i = 0 ; i < m; i++) {
string u, v;
int w;
cin >> u >> v >> w;
add_edge(mp[u], mp[v], w);
}
dijkstra(&nodes[0]);
vector<string> ans;
print_path(&nodes[mp["ROM"]], ans);
cout << nodes[mp["ROM"]].cnt << ' '
<< nodes[mp["ROM"]].dis << ' '
<< nodes[mp["ROM"]].sum_val << ' '
<< nodes[mp["ROM"]].sum_val / (ans.size() - 1) << endl;
for (int i = 0; i < ans.size(); i++) {
if (i == ans.size() - 1) cout << ans[i] << endl;
else cout << ans[i] << "->";
}
return 0;
}