#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
#define mp make_pair
const int maxn5 = 2e5 + 10;
const int maxn3 = 1e3 + 10;
const ll inf = 1e18;
int n;
vector <int> imp;
vector <pair<int, ll>> adj[maxn5];
set <pair<ll, int>> av;
ll dis[5][maxn5], dis2[maxn3][maxn3];
inline void dij(int r, int id){
av.clear();
for(int i = 0; i < n; i++)
dis[id][i] = inf;
dis[id][r] = 0;
for(int i = 0; i < n; i++)
av.insert({dis[id][i], i});
while(av.size()){
int v = av.begin() -> se;
av.erase(av.begin());
for(auto p : adj[v]){
int u = p.fi;
ll w = p.se;
if(dis[id][u] < dis[id][v] + w)
continue;
av.erase({dis[id][u], u});
dis[id][u] = dis[id][v] + w;
av.insert({dis[id][u], u});
}
}
return;
}
inline void dij2(int r, int id){
av.clear();
for(int i = 0; i < n; i++)
dis2[id][i] = inf;
dis2[id][r] = 0;
for(int i = 0; i < n; i++)
av.insert({dis2[id][i], i});
while(av.size()){
int v = av.begin() -> se;
av.erase(av.begin());
for(auto p : adj[v]){
int u = p.fi;
ll w = p.se;
if(dis2[id][u] < dis2[id][v] + w)
continue;
av.erase({dis2[id][u], u});
dis2[id][u] = dis2[id][v] + w;
av.insert({dis2[id][u], u});
}
}
return;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int k, m; cin >> n >> k >> m;
for(int i = 0; i < k; i++){
int a; cin >> a; a--;
imp.pb(a);
}
for(int i = 0; i < m; i++){
int a, b, c; cin >> a >> b >> c;
a--; b--;
adj[a].pb({b, c});
adj[b].pb({a, c});
}
for(int i = 0; i < k; i++)
dij(imp[i], i);
if(k == 2){
cout << dis[0][imp[1]] << endl;
return 0;
}
if(k == 3){
ll ans = dis[0][imp[1]] + dis[1][imp[2]];
for(int i = 0; i < n; i++)
ans = min(ans, dis[0][i] + dis[1][i] + dis[2][i]);
return cout << ans << endl, 0;
}
for(int i = 0; i < k; i++)
dij2(i, i);
ll ans = inf;
int a0 = imp[0], a1 = imp[1], a2 = imp[2], a3 = imp[3];
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){
ans = min(ans, dis2[a0][i] + dis2[a1][i] + dis2[a2][j] + dis2[a3][j] + dis2[i][j]);
ans = min(ans, dis2[a0][i] + dis2[a1][j] + dis2[a2][i] + dis2[a3][j] + dis2[i][j]);
ans = min(ans, dis2[a0][i] + dis2[a1][j] + dis2[a2][j] + dis2[a3][i] + dis2[i][j]);
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
538 ms |
27828 KB |
Output is correct |
2 |
Correct |
589 ms |
27240 KB |
Output is correct |
3 |
Correct |
290 ms |
21032 KB |
Output is correct |
4 |
Correct |
61 ms |
17384 KB |
Output is correct |
5 |
Correct |
431 ms |
27092 KB |
Output is correct |
6 |
Correct |
60 ms |
17280 KB |
Output is correct |
7 |
Correct |
6 ms |
5204 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
5332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1347 ms |
55196 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1573 ms |
56808 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |