This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops")
using namespace std;
#define ll long long
#define int ll
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define pii pair<int, int>
#define f first
#define s second
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define endl '\n'
#define IOS() ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
const ll maxn = 1e5+5;
const ll inf = 1ll<<60;
int n, m, k;
vector<vector<int>> kdis(5);
vector<int> tod;
vector<pii> g[maxn];
vector<int> ddis(maxn); // dijkstra distance
vector<int> tmpdis(maxn);
vector<int> occ(maxn);
int trident[5][5][maxn];
void dij(){
// initialize ddis before hand
REP1(i, n) occ[i] = 0;
priority_queue<pii, vector<pii>, greater<pii> > pq;
//pq.push({ddis[i], a});
REP1(i, n) pq.push({ddis[i], i});
while(SZ(pq)){
pii tp = pq.top(); pq.pop();
if (occ[tp.s]) continue;
occ[tp.s] = 1;
for (auto e:g[tp.s]){
if (!occ[e.f] && ddis[e.f] > ddis[tp.s]+e.s){
ddis[e.f] = ddis[tp.s]+e.s;
pq.push({ddis[e.f], e.f});
}
}
}
}
void inp(){
cin>>n>>k>>m;
tod = vector<int> (k);
REP(i, k) cin>>tod[i];
REP(i, m){
int u, v, c; cin>>u>>v>>c;
g[u].pb({v, c});
g[v].pb({u, c});
}
}
const vector<vector<int>> four = {{0, 1, 2, 3}, {0, 2, 1, 3}, {0, 3, 1, 2}, {1, 2, 0, 3}, {1, 3, 0, 2}, {2, 3, 0, 1}};
//vector<vector<int>> four = {{0, 2, 1, 3}};
const vector<vector<int>> five1 = {{0, 1, 2, 3, 4},
{0, 1, 3, 2, 4},
{0, 1, 4, 2, 3},
{0, 2, 3, 1, 4},
{0, 2, 4, 1, 3},
{0, 3, 4, 1, 2},
{1, 0, 2, 3, 4},
{1, 0, 3, 2, 4},
{1, 0, 4, 2, 3},
{1, 2, 3, 0, 4},
{1, 2, 4, 0, 3},
{1, 3, 4, 0, 2},
{2, 0, 1, 3, 4},
{2, 0, 3, 1, 4},
{2, 0, 4, 1, 3},
{2, 1, 3, 0, 4},
{2, 1, 4, 0, 3},
{2, 3, 4, 0, 1},
{3, 0, 1, 2, 4},
{3, 0, 2, 1, 4},
{3, 0, 4, 1, 2},
{3, 1, 2, 0, 4},
{3, 1, 4, 0, 2},
{3, 2, 4, 0, 1},
{4, 0, 1, 2, 3},
{4, 0, 2, 1, 3},
{4, 0, 3, 1, 2},
{4, 1, 2, 0, 3},
{4, 1, 3, 0, 2},
{4, 2, 3, 0, 1}
};
const vector<vector<int>> five2 = {{0, 1, 2, 3, 4},
{0, 2, 1, 3, 4},
{0, 3, 1, 2, 4},
{0, 4, 1, 2, 3},
{1, 2, 0, 3, 4},
{1, 3, 0, 2, 4},
{1, 4, 0, 2, 3},
{2, 3, 0, 1, 4},
{2, 4, 0, 1, 3},
{3, 4, 0, 1, 2}
};
signed main(){
IOS();
inp();
REP(i, k){
fill(ALL(ddis), inf);
ddis[tod[i]] = 0;
dij();
kdis[i] = ddis;
}
REP(i, k){
FOR(j, i+1, k){
REP1(l, n){
ddis[l] = kdis[i][l] + kdis[j][l];
}
dij();
REP1(l, n) trident[i][j][l] = ddis[l];
}
}
vector<int> dp(1<<k);
REP(i, (1<<k)){
int bb = __builtin_popcount(i);
if (bb <= 1) continue;
else{
vector<int> ids; // actual indices
REP(j, k) if (i&(1<<j)) ids.pb(j);
dp[i] = inf;
REP1(j, n){
int cnt = 0;
REP(l, k) cnt += kdis[l][j];
dp[i] = min(dp[i], cnt);
}
if (bb <= 3) continue;
for (auto j:ids){
for (auto l:ids){
if (j != l) dp[i] = min(dp[i], dp[i-(1<<j)]+kdis[j][tod[l]]);
}
}
if (bb == 4){
for (auto &t:four){
REP1(j, n){
dp[i] = min(dp[i], trident[ids[t[0]]][ids[t[1]]][j] + kdis[ids[t[2]]][j] + kdis[ids[t[3]]][j]);
}
}
}
else {
for (auto &t:five1){
dp[i] = min(dp[i], dp[(1<<t[0])+(1<<t[1])+(1<<t[2])] + dp[(1<<t[0])+(1<<t[3])+(1<<t[4])]);
}
for (auto &t:five2){
REP1(j, n){
dp[i] = min(dp[i], trident[ids[t[0]]][ids[t[1]]][j] + kdis[ids[t[2]]][j] + kdis[ids[t[3]]][j] + kdis[ids[t[4]]][j]);
}
}
for (auto &t:five1){
REP1(j, n){
dp[i] = min(dp[i], trident[ids[t[1]]][ids[t[2]]][j] + trident[ids[t[3]]][ids[t[4]]][j] + kdis[t[0]][j]);
}
}
}
}
}
cout<<dp[(1<<k)-1]<<endl;
}
/*
6 4 8
2 3 5 6
1 4 3
2 5 13
2 1 3
2 6 13
3 4 3
4 6 3
4 2 13
1 5 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |