#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define FOR(a, b, c) for(int a = b; a <= c; ++a)
#define int long long
#define pb push_back
const int N = 1e5 + 10;
const int oo = 1e18;
typedef pair<int, int> ii;
typedef pair<int, ii> pii;
int n, k, m;
int f[N][(1 << 5)], pos[N];
vector<ii> vi[N];
void upd(int &x, int y) { x = min(x, y); }
void solve() {
FOR(i, 1, n) FOR(j, 0, (1 << k) - 1) f[i][j] = oo;
priority_queue<pii, vector<pii>, greater<pii> > pq;
FOR(i, 1, n) {
int mask = 0; if(pos[i]) mask = (1 << (pos[i] - 1));
f[i][mask] = 0;
pq.push(pii(0, ii(i, mask)));
}
while(!pq.empty()) {
int u = pq.top().se.fi, mask = pq.top().se.se, val = pq.top().fi; pq.pop();
//cout << u << ' ' << mask << '\n';
if(f[u][mask] != val) continue;
for(auto v: vi[u]) {
for(int t = 0; t < (1 << k); ++t) if(!(mask & t)) {
int nex = (mask | t);
if(f[v.fi][nex] > f[u][mask] + v.se + f[v.fi][t]) {
f[v.fi][nex] = f[u][mask] + v.se + f[v.fi][t];
pq.push(pii(f[v.fi][nex], ii(v.fi, nex)));
}
}
}
}
}
signed main()
{
//freopen("test.inp", "r", stdin);
scanf("%lld%lld%lld", &n, &k, &m);
FOR(i, 1, k) {
int x; scanf("%lld", &x);
pos[x] = i;
}
FOR(i, 1, m) {
int u, v, c; scanf("%lld%lld%lld", &u, &v, &c);
vi[u].pb(ii(v, c)); vi[v].pb(ii(u, c));
}
solve();
int ans = oo;
FOR(i, 1, n) upd(ans, f[i][(1 << k) - 1]);
printf("%lld", ans);
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld", &n, &k, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:49:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x; scanf("%lld", &x);
~~~~~^~~~~~~~~~~~
cities.cpp:53:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v, c; scanf("%lld%lld%lld", &u, &v, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2856 KB |
Output is correct |
3 |
Correct |
4 ms |
2960 KB |
Output is correct |
4 |
Correct |
4 ms |
2960 KB |
Output is correct |
5 |
Correct |
4 ms |
3056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1594 ms |
63104 KB |
Output is correct |
2 |
Correct |
1460 ms |
63104 KB |
Output is correct |
3 |
Correct |
654 ms |
63104 KB |
Output is correct |
4 |
Correct |
180 ms |
63104 KB |
Output is correct |
5 |
Correct |
679 ms |
63104 KB |
Output is correct |
6 |
Correct |
126 ms |
63104 KB |
Output is correct |
7 |
Correct |
10 ms |
63104 KB |
Output is correct |
8 |
Correct |
7 ms |
63104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
63104 KB |
Output is correct |
2 |
Correct |
17 ms |
63104 KB |
Output is correct |
3 |
Correct |
10 ms |
63104 KB |
Output is correct |
4 |
Correct |
15 ms |
63104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4826 ms |
137092 KB |
Output is correct |
2 |
Correct |
4318 ms |
137092 KB |
Output is correct |
3 |
Correct |
2264 ms |
137092 KB |
Output is correct |
4 |
Correct |
3200 ms |
137092 KB |
Output is correct |
5 |
Correct |
721 ms |
137092 KB |
Output is correct |
6 |
Correct |
300 ms |
137092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6090 ms |
235512 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |