#include<bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define REP(i, b) for (int i = 0, _b = (b); i < _b; i++)
#define ii pair<int, int>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1LL)
#define ll long long
#define ld long double
#define sz(x) ((int)(x).size())
#define next ____next
#define prev ____prev
#define left ____left
#define right ___right
#define lcm ___lcm
#define index ____index
using namespace std;
template<class M> M myabs(M x) {
return x < 0 ? -x : x;
}
template<class M1, class M2> bool minimize(M1 &x, const M2 &y) {
if (x > y) {x = y; return true;} return false;
}
template<class M1, class M2> bool maximize(M1 &x, const M2 &y) {
if (x < y) {x = y; return true;} return false;
}
const int INF = (int)1e9 + 7;
const ll INFLL = (ll)1e18 + 7LL;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
mt19937 rd(time(0));
/**
ii = pair<int, int>
FOR(i, a, b): a, b == int
**/
#define MAX 5050
int n, m, num_special;
vector<ii> adj[MAX];
int a[MAX];
ll d[MAX][MAX];
void dijkstra(int s, ll d[]) {
FOR(i, 1, n) d[i] = INFLL;
d[s] = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> q;
q.push(mp(d[s], s));
while (!q.empty()) {
int u = q.top().se; ll w = q.top().fi; q.pop();
if (w != d[u]) continue;
for (auto p : adj[u]) {
int v = p.se, c = p.fi;
if (minimize(d[v], d[u] + c)) q.push(mp(d[v], v));
}
}
}
bool check(const ii& A, const ii& B) {
return A.fi != B.fi && A.fi != B.se && A.se != B.fi && A.se != B.se;
}
void solve() {
cin >> n >> m >> num_special;
FOR(i, 1, m) {
int u, v, c; cin >> u >> v >> c;
adj[u].push_back(mp(c, v));
adj[v].push_back(mp(c, u));
}
FOR(i, 1, num_special) cin >> a[i];
sort(a + 1, a + 1 + num_special);
FOR(i, 1, num_special) dijkstra(a[i], d[a[i]]);
vector<pair<ll, ii>> vec;
FOR(i, 1, num_special) FOR(j, i + 1, num_special) if (d[a[i]][a[j]] < INFLL)
vec.push_back(mp(d[a[i]][a[j]], mp(a[i], a[j])));
sort(ALL(vec)); vec.erase(unique(ALL(vec)), vec.end());
ll ans = INFLL;
//for (auto p : vec) cout << p.fi << " " << p.se.fi << " " << p.se.se << "\n";
REP(i, sz(vec)) FOR(j, i + 1, sz(vec) - 1) if (check(vec[i].se, vec[j].se)) {
minimize(ans, vec[i].fi + vec[j].fi);
//cout << vec[i].fi << " " << vec[j].fi << "\n";
break;
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1; //cin >> t;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
584 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
720 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
584 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
720 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
456 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
49 ms |
4064 KB |
Output is correct |
37 |
Correct |
95 ms |
6484 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
40 ms |
4040 KB |
Output is correct |
40 |
Correct |
79 ms |
4420 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
104 ms |
5092 KB |
Output is correct |
43 |
Correct |
41 ms |
3908 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
228 ms |
9160 KB |
Output is correct |
47 |
Correct |
79 ms |
4280 KB |
Output is correct |
48 |
Correct |
52 ms |
4208 KB |
Output is correct |
49 |
Correct |
144 ms |
6852 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
25 ms |
4208 KB |
Output is correct |
52 |
Correct |
2 ms |
596 KB |
Output is correct |
53 |
Correct |
34 ms |
2668 KB |
Output is correct |
54 |
Correct |
45 ms |
4284 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
584 KB |
Output is correct |
58 |
Correct |
119 ms |
5768 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
32 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
704 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
456 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
608 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
0 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
584 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
720 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
724 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
716 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
596 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
596 KB |
Output is correct |
33 |
Correct |
1 ms |
464 KB |
Output is correct |
34 |
Correct |
1 ms |
456 KB |
Output is correct |
35 |
Correct |
1 ms |
468 KB |
Output is correct |
36 |
Correct |
49 ms |
4064 KB |
Output is correct |
37 |
Correct |
95 ms |
6484 KB |
Output is correct |
38 |
Correct |
2 ms |
596 KB |
Output is correct |
39 |
Correct |
40 ms |
4040 KB |
Output is correct |
40 |
Correct |
79 ms |
4420 KB |
Output is correct |
41 |
Correct |
2 ms |
596 KB |
Output is correct |
42 |
Correct |
104 ms |
5092 KB |
Output is correct |
43 |
Correct |
41 ms |
3908 KB |
Output is correct |
44 |
Correct |
2 ms |
596 KB |
Output is correct |
45 |
Correct |
1 ms |
596 KB |
Output is correct |
46 |
Correct |
228 ms |
9160 KB |
Output is correct |
47 |
Correct |
79 ms |
4280 KB |
Output is correct |
48 |
Correct |
52 ms |
4208 KB |
Output is correct |
49 |
Correct |
144 ms |
6852 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
25 ms |
4208 KB |
Output is correct |
52 |
Correct |
2 ms |
596 KB |
Output is correct |
53 |
Correct |
34 ms |
2668 KB |
Output is correct |
54 |
Correct |
45 ms |
4284 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
584 KB |
Output is correct |
58 |
Correct |
119 ms |
5768 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
32 ms |
4180 KB |
Output is correct |
61 |
Runtime error |
4 ms |
704 KB |
Execution killed with signal 11 |
62 |
Halted |
0 ms |
0 KB |
- |