#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 pb push_back
#define ll long long
const int N = 1e5 + 10;
const ll oo = 1e18;
typedef pair<int, int> ii;
typedef pair<ll, ii> pii;
int n, k, m;
int pos[N];
ll f[N][(1 << 5)];
vector<ii> vi[N];
void upd(ll &x, ll 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] = 0ll;
pq.push(pii(0ll, ii(i, mask)));
}
while(!pq.empty()) {
int u = pq.top().se.fi, mask = pq.top().se.se;
ll 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] + 1ll * v.se + f[v.fi][t]) {
f[v.fi][nex] = f[u][mask] + 1ll * 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("%d%d%d", &n, &k, &m);
FOR(i, 1, k) {
int x; scanf("%d", &x);
pos[x] = i;
}
FOR(i, 1, m) {
int u, v, c; scanf("%d%d%d", &u, &v, &c);
vi[u].pb(ii(v, c)); vi[v].pb(ii(u, c));
}
solve();
ll 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:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &k, &m);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
cities.cpp:51:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x; scanf("%d", &x);
~~~~~^~~~~~~~~~
cities.cpp:55:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v, c; scanf("%d%d%d", &u, &v, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2712 KB |
Output is correct |
4 |
Correct |
4 ms |
2756 KB |
Output is correct |
5 |
Correct |
4 ms |
2760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1443 ms |
50620 KB |
Output is correct |
2 |
Correct |
1278 ms |
50620 KB |
Output is correct |
3 |
Correct |
550 ms |
50620 KB |
Output is correct |
4 |
Correct |
197 ms |
50620 KB |
Output is correct |
5 |
Correct |
571 ms |
50620 KB |
Output is correct |
6 |
Correct |
130 ms |
50620 KB |
Output is correct |
7 |
Correct |
10 ms |
50620 KB |
Output is correct |
8 |
Correct |
7 ms |
50620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50620 KB |
Output is correct |
2 |
Correct |
16 ms |
50620 KB |
Output is correct |
3 |
Correct |
11 ms |
50620 KB |
Output is correct |
4 |
Correct |
14 ms |
50620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4129 ms |
100052 KB |
Output is correct |
2 |
Correct |
3764 ms |
100052 KB |
Output is correct |
3 |
Correct |
2019 ms |
100052 KB |
Output is correct |
4 |
Correct |
2795 ms |
100052 KB |
Output is correct |
5 |
Correct |
710 ms |
100052 KB |
Output is correct |
6 |
Correct |
334 ms |
100052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6055 ms |
165824 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |