#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 100005;
int n, k, m;
vector<pii> g[mxn];
vector<int> imp;
ll dist[5][mxn];
vector<int> code;
int deg[5];
ll decode(){
fr(i, 0, k) deg[i] = 1;
for(auto u : code){
++deg[u];
}
set<int> leaves;
fr(i, 0, k){
if(deg[i] == 1) leaves.insert(i);
}
ll sum = 0;
for(auto u : code){
int leaf = *leaves.begin();
leaves.erase(leaves.begin());
sum += dist[leaf][imp[u]];
--deg[u];
if(deg[u] == 1) leaves.insert(u);
}
sum += dist[*leaves.begin()][imp[k-1]];
return sum;
}
ll best = inf;
void generate_code(int pos){
if(pos == k-2){
best = min(best, decode());
}
else{
fr(i, 0, k){
code.pb(i);
generate_code(pos+1);
code.pop_back();
}
}
}
bool is_imp[mxn];
void solve(){
cin >> n >> k >> m;
imp.resize(k);
fr(i, 0, k) cin >> imp[i];
fr(i, 0, k) is_imp[--imp[i]];
fr(i, 0, m){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
g[u].pb({v, w});
g[v].pb({u, w});
}
bool vis[n];
fr(i, 0, (int)imp.size()){
memset(vis, false, sizeof(vis));
fr(j, 0, n) dist[i][j] = inf;
dist[i][imp[i]] = 0;
pq<pii> Q;
Q.push({0, imp[i]});
while(!Q.empty()){
int c = Q.top().nd;
Q.pop();
if(vis[c]) continue;
vis[c] = true;
for(auto e : g[c]){
if(dist[i][c] + e.nd < dist[i][e.st]){
dist[i][e.st] = dist[i][c] + e.nd;
Q.push({-dist[i][e.st], e.st});
}
}
}
}
fr(i, 0, n){
if(is_imp[i]) continue;
imp.pb(i);
fr(j, 0, k){
dist[k][imp[j]] = dist[j][i];
}
++k;
generate_code(0);
--k;
imp.pop_back();
}
generate_code(0);
cout << best << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Incorrect |
3 ms |
2688 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
680 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
46 ms |
2816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4354 ms |
17304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6093 ms |
18084 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |