#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define pb push_back
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ii = pair<int,int>;
using iii = tuple<int,int,int>;
const ll inf = 1e18;
const int mod = 1e9+7;
const int maxn = 1e5+100;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
int a[5],n,k,m;
vector<pair<ll,int>>g[maxn];
ll dist[1<<5][maxn];
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>>pq;
void dijkstra(ll d[maxn]){
for(int i=1;i<=n;++i)pq.push({d[i],i});
while(!pq.empty()){
pair<ll,int> me = pq.top();pq.pop();
int u = me.second;
if(me.first>d[u])continue;
for(pair<ll,int>e :g[u]){
if(d[e.second]>e.first+me.first){
d[e.second] = e.first + me.first;
pq.push({d[e.second],e.second});
}
}
}
}
int main(){_
cin>>n>>k>>m;
for(int i=0;i<k;++i){
memset(dist[1<<i], 0x3F, 8*(n+1));
cin>>a[i], dist[1<<i][a[i]] = 0;
}
for(int i=1;i<=m;++i){
ll c;int u,v;cin>>u>>v>>c;
g[u].pb({c,v});
g[v].pb({c,u});
}
for(int i=1;i<(1<<k);++i){
if(__builtin_popcount(i)>1){
memset(dist[i], 0x3F, 8*(n+10));
for(int j=0;j<k;++j)
if(i>>j&1)
for(int l=1;l<=n;++l)
ckmin(dist[i][l],dist[i^1<<j][l]+dist[1<<j][l]);
}
dijkstra(dist[i]);
}
ll ans = inf;
for(int i=1;i<=n;++i)ckmin(ans,dist[(1<<k)-1][i]);
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
502 ms |
25188 KB |
Output is correct |
2 |
Correct |
488 ms |
24952 KB |
Output is correct |
3 |
Correct |
307 ms |
17204 KB |
Output is correct |
4 |
Correct |
63 ms |
15240 KB |
Output is correct |
5 |
Correct |
281 ms |
23080 KB |
Output is correct |
6 |
Correct |
59 ms |
15184 KB |
Output is correct |
7 |
Correct |
4 ms |
2900 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3068 KB |
Output is correct |
2 |
Correct |
8 ms |
3068 KB |
Output is correct |
3 |
Correct |
5 ms |
2900 KB |
Output is correct |
4 |
Correct |
4 ms |
2912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
987 ms |
31552 KB |
Output is correct |
2 |
Correct |
924 ms |
31044 KB |
Output is correct |
3 |
Correct |
621 ms |
23464 KB |
Output is correct |
4 |
Correct |
510 ms |
25144 KB |
Output is correct |
5 |
Correct |
149 ms |
17448 KB |
Output is correct |
6 |
Correct |
67 ms |
17496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1822 ms |
44380 KB |
Output is correct |
2 |
Correct |
1808 ms |
44128 KB |
Output is correct |
3 |
Correct |
1765 ms |
43580 KB |
Output is correct |
4 |
Correct |
1212 ms |
36268 KB |
Output is correct |
5 |
Correct |
941 ms |
31408 KB |
Output is correct |
6 |
Correct |
235 ms |
18820 KB |
Output is correct |
7 |
Correct |
81 ms |
17996 KB |
Output is correct |