-
Notifications
You must be signed in to change notification settings - Fork 0
/
A1003-2.cpp
99 lines (91 loc) · 1.79 KB
/
A1003-2.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
/*
* author: zhouyuhao
* created: 2023-03-30 19:07:00
* modified: 2023-03-30 19:30:26
* item: Programming Ability Test
* site: Yuting
*/
#include <climits>
#include <iostream>
#include <numeric>
#include <queue>
using namespace std;
struct node {
int v, d;
node(int v, int d) : v(v), d(d) {
}
friend bool operator<(node a, node b) {
return a.d > b.d;
}
};
vector<bool> vis;
vector<int> d, t;
vector<vector<int>> pre;
vector<vector<node>> g;
void dijkstra(int s) {
d[s] = 0;
priority_queue<node> q;
q.emplace(s, 0);
while (!q.empty()) {
node t = q.top();
q.pop();
int u = t.v;
if (vis[u]) {
continue;
}
vis[u] = true;
for (int i = 0; i < (int)g[u].size(); i++) {
int v = g[u][i].v;
if (!vis[v]) {
if (d[u] + g[u][i].d < d[v]) {
d[v] = d[u] + g[u][i].d;
pre[v].clear();
pre[v].emplace_back(u);
q.emplace(v, d[v]);
} else if (d[u] + g[u][i].d == d[v]) {
pre[v].emplace_back(u);
}
}
}
}
}
int cnt = 0, maxt = -1;
vector<int> temp;
void dfs(int s, int v) {
if (v == s) {
++cnt;
temp.emplace_back(v);
int sumt = accumulate(temp.begin(), temp.end(), 0, [](int acc, int idx) -> int {
return acc + t[idx];
});
if (sumt > maxt) {
maxt = sumt;
}
temp.pop_back();
return;
}
temp.emplace_back(v);
for (int i = 0; i < (int)pre[v].size(); i++) {
dfs(s, pre[v][i]);
}
temp.pop_back();
}
int main(int argc, char const *argv[]) {
int n, m, c1, c2;
cin >> n >> m >> c1 >> c2;
d.resize(n, INT_MAX), vis.resize(n, false);
t.resize(n), pre.resize(n), g.resize(n);
for (int i = 0; i < n; i++) {
cin >> t[i];
}
for (int i = 0; i < m; i++) {
int id1, id2, l;
cin >> id1 >> id2 >> l;
g[id1].emplace_back(id2, l);
g[id2].emplace_back(id1, l);
}
dijkstra(c1);
dfs(c1, c2);
cout << cnt << " " << maxt << "\n";
return 0;
}