#include<bits/stdc++.h>
//12 48
using namespace std;
#define int long long
#define INF 1e17
typedef pair<int, int> ii;
template <typename T>
using vv = vector<vector<T> >;
struct edge{
int u, v, c;
};
int n, k, m;
vector<int> h;
vv<int> g;
vector<edge> e;
vector<vector<int> > dh;
void dijkstra1(int val){ //val is the index of the impp array
int source = h[val];
dh[val] = vector<int>(n, INF);
priority_queue<ii> q;
q.push({0, source});
while(!q.empty()){
pair<int, int> t = q.top();
q.pop();
if(dh[val][t.second] != INF){
assert(dh[val][t.second] <= -t.first);
continue;
}
dh[val][t.second] = -t.first;
for(int eid: g[t.second]){
int next = e[eid].u + e[eid].v - t.second;
if(dh[val][next] == INF){
q.push({t.first - e[eid].c, next});
}else{
assert(dh[val][next] <= (-t.first + e[eid].c));
}
}
}
}
vector<vector<vector<int> > > dab;
signed main(){
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>k>>m;
h.resize(k);
for(int i = 0; i < k; i++){
cin>>h[i]; h[i]--;
}
g.resize(n);
for(int i = 0; i < m; i++){
int a, b, c; cin>>a>>b>>c;
a--, b--;
e.push_back({a, b, c});
g[a].push_back(i);
g[b].push_back(i);
}
//find shortest path from all important to all others
dh.resize(k);
for(int i = 0; i < k; i++){
dijkstra1(i);
}
//d[a][b][i] short distance from i to a and i to b
dab = vector<vector<vector<int> > > (k, vector<vector<int> > (k, vector<int>(n, INF)));
for(int a = 0; a < k; a++){
for(int b = 0; b < k; b++){
if(a == b){
dab[a][a] = dh[a];
}else{
for(int i = 0; i < n; i++){
dab[a][b][i] = dh[a][i] + dh[b][i];
}
}
}
}
//do multisource dijkstra from
for(int a = 0; a < k; a++){
for(int b = 0; b < k; b++){
//make dab[a][b][i] the best value for i
vector<int> vis(n, 0);
priority_queue<ii> q;
for(int i = 0; i < n; i++){
q.push({-dab[a][b][i], i});
}
while(!q.empty()){
ii t = q.top(); q.pop();
if(vis[t.second]) {
assert(-t.first >= dab[a][b][t.second]);
continue;
}
vis[t.second] = 1;
assert(dab[a][b][t.second] >= -t.first);
dab[a][b][t.second] = -t.first;
for(int eid: g[t.second]){
int next = e[eid].u + e[eid].v - t.second;
if(dab[a][b][next] > (-t.first + e[eid].c)){
q.push({t.first - e[eid].c, next});
assert(vis[next] == 0);
}
}
}
}
}
int bestAns = LONG_LONG_MAX;
//find answer
for(int i = 0; i < n; i++){
vector<int> perm(k);
for(int j = 0; j < k; j++) perm[j] = j;
do{
int curAns = 0;
for(int j = 0; j < k; j += 2){
if(j == k-1){
curAns += dab[perm[j]][perm[j]][i];
}else{
curAns += dab[perm[j]][perm[j+1]][i];
}
}
bestAns = min(bestAns, curAns);
}while(next_permutation(perm.begin(), perm.end()));
}
cout<<bestAns<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1313 ms |
31108 KB |
Output is correct |
2 |
Correct |
1495 ms |
30492 KB |
Output is correct |
3 |
Correct |
727 ms |
22320 KB |
Output is correct |
4 |
Correct |
268 ms |
16668 KB |
Output is correct |
5 |
Correct |
754 ms |
26284 KB |
Output is correct |
6 |
Correct |
188 ms |
16664 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
768 KB |
Output is correct |
2 |
Correct |
7 ms |
768 KB |
Output is correct |
3 |
Correct |
4 ms |
640 KB |
Output is correct |
4 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2055 ms |
38244 KB |
Output is correct |
2 |
Correct |
2220 ms |
37532 KB |
Output is correct |
3 |
Correct |
1168 ms |
29528 KB |
Output is correct |
4 |
Correct |
1375 ms |
27332 KB |
Output is correct |
5 |
Correct |
515 ms |
17372 KB |
Output is correct |
6 |
Correct |
341 ms |
18364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3194 ms |
46752 KB |
Output is correct |
2 |
Correct |
3349 ms |
46776 KB |
Output is correct |
3 |
Correct |
2840 ms |
46208 KB |
Output is correct |
4 |
Correct |
2112 ms |
37988 KB |
Output is correct |
5 |
Correct |
2161 ms |
29096 KB |
Output is correct |
6 |
Correct |
599 ms |
17456 KB |
Output is correct |
7 |
Correct |
383 ms |
18428 KB |
Output is correct |