#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define debugs(x) cerr << #x << " " << x << " "
//#include"holiday.h"
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
typedef tree <ll, null_type, less_equal <ll>, rb_tree_tag, tree_order_statistics_node_update> OST;
const ll NMAX = 100001;
const ll INF = (1LL << 60);
const ll MOD = 666013;
const ll BLOCK = 225;
const ll base = 31;
const ll nr_of_bits = 21;
vector <pii> v[NMAX];
int n, k, m;
struct ura {
int a, b, c;
};
vector <ura> edges;
int scor = 0;
int viz[NMAX];
bool cmp(ura a, ura b) {
return a.c < b.c;
}
void add_Edge(int a, int b, int c) {
edges.push_back({a, b, c});
}
int d[NMAX][5];
int dp[(1 << 5)][NMAX];
int imp[NMAX];
void dijkstra(int ind) {
priority_queue <pii> pq;
pq.push({0, imp[ind + 1]});
for(int i = 0; i <= n; i++) {
viz[i] = 0;
}
d[imp[ind + 1]][ind] = 0;
while(pq.size()) {
pii node = pq.top();
int city = node.second;
pq.pop();
if(viz[city])
continue;
viz[city] = 1;
for(auto x : v[city]) {
if(d[city][ind] + x.second < d[x.first][ind]) {
d[x.first][ind] = d[city][ind] + x.second;
pq.push({-d[x.first][ind], x.first});
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> m;
priority_queue <pii> pq;
for(int i = 1; i <= n; i++) {
for(int t = 0; t < k; t++)
d[i][t] = 2e9;
}
for(int i = 1; i <= k; i++) {
int x;
cin >> x;
imp[i] = x;
}
for(int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, c});
}
for(int i = 1; i <= k; i++) {
dijkstra(i - 1);
}
for(int mask = 0; mask < (1 << k); mask++) {
for(int i = 0; i <= n; i++)
dp[mask][i] = 1e9;
}
for(int i = 1; i <= k; i++) {
dp[(1 << (i - 1))][imp[i]] = 0;
}
for(int i = 1; i <= n; i++)
dp[0][i] = 0;
for(int mask = 0; mask < (1 << k); mask++) {
for(int submask = mask; submask > 0; submask = (submask - 1) & mask) {
for(int i = 1; i <= n; i++) {
dp[mask][i] = min(dp[mask][i], dp[submask][i] + dp[(mask ^ submask ^ (1 << i))][i]);
debugs(mask);
debugs(i);
debug(dp[mask][i]);
debug(submask);
debug(dp[submask][i]);
debug(dp[(mask ^ submask ^ (1 << i))][i]);
debug("\n\n\n");
}
}
for(int i = 1; i <= n; i++){
int submask = 0;
dp[mask][i] = min(dp[mask][i], dp[submask][i] + dp[(mask ^ submask ^ (1 << i))][i]);
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j < k; j++) {
if(!(mask & (1 << j)))
continue;
dp[mask][i] = min(dp[mask][i], dp[(mask ^ (1 << j))][i] + d[j][i]);
}
}
}
int sol = 2e9;
for(int i = 1; i <= n; i++) {
sol = min(sol, dp[(1 << k) - 1][i]);
}
cout << sol;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
2636 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
150 ms |
41648 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
8 ms |
5776 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
156 ms |
47888 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
169 ms |
60772 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |