//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vli vector<pli>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const ll INF = 1e18 + 7;
const int MAXN = 2e5 + 5;
const double eps = 1e-9;
int ind[5];
ll dp[5][MAXN], dp2[5][5][MAXN];
vli adj[MAXN];
vi a;
int main() {
memset(dp, 0x3f, sizeof dp);
int n, k, m;
scanf("%d %d %d", &n, &k, &m);
for (int i = 0; i < k; i++) {
ni(ind[i]);
ind[i]--;
a.pb(i);
}
sort(ind, ind + k);
k = unique(ind, ind + k) - ind;
if (k == 1) return !printf("0\n");
for (int i = 0; i < m; i++) {
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
u--, v--;
adj[u].pb({w,v});
adj[v].pb({w,u});
}
for (int i = 0; i < k; i++) {
priority_queue<pli,vli,greater<pli>> pq;
pq.push({0,ind[i]});
dp[i][ind[i]] = 0;
while (!pq.empty()) {
ll d; int u;
tie(d, u) = pq.top(); pq.pop();
if (dp[i][u] < d) continue;
for (pli v: adj[u]) {
if (dp[i][v.se] <= d + v.fi) continue;
dp[i][v.se] = d + v.fi;
pq.push({d + v.fi, v.se});
}
}
}
for (int i = 0; i < k; i++) {
for (int j = i + 1; j < k; j++) {
priority_queue<pli,vli,greater<pli>> pq;
for (int l = 0; l < n; l++) {
pq.push({dp[i][l]+dp[j][l], l});
dp2[i][j][l] = dp[i][l]+dp[j][l];
}
while (!pq.empty()) {
ll d; int u;
tie(d, u) = pq.top(); pq.pop();
if (dp2[i][j][u] < d) continue;
for (pli v: adj[u]) {
if (dp2[i][j][v.se] <= d + v.fi) continue;
dp2[i][j][v.se] = d + v.fi;
pq.push({d + v.fi, v.se});
}
}
}
}
ll ans = INF;
switch(k) {
case 2: {
ans = dp[0][1];
break;
}
case 3: {
for (int i = 0; i < n; i++)
ans = min(ans, dp[0][i]+dp[1][i]+dp[2][i]);
break;
}
case 4: {
do {
if (a[0]>a[1]||a[2]>a[3]||a[0]>a[2]) continue;
for (int i = 0; i < n; i++)
ans = min(ans, dp2[a[0]][a[1]][i]+dp2[a[2]][a[3]][i]);
} while (next_permutation(a.begin(), a.end()));
break;
}
case 5: {
do {
if (a[0]>a[1]||a[2]>a[3]||a[0]>a[2]) continue;
for (int i = 0; i < n; i++)
ans = min(ans, dp2[a[0]][a[1]][i]+dp2[a[2]][a[3]][i]+dp[a[4]][i]);
} while (next_permutation(a.begin(), a.end()));
break;
}
}
prl(ans);
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:42: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:6:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
cities.cpp:44:9: note: in expansion of macro 'ni'
ni(ind[i]);
^~
cities.cpp:53:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &u, &v, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
12928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
813 ms |
32020 KB |
Output is correct |
2 |
Correct |
740 ms |
31596 KB |
Output is correct |
3 |
Correct |
435 ms |
24104 KB |
Output is correct |
4 |
Correct |
115 ms |
21916 KB |
Output is correct |
5 |
Incorrect |
489 ms |
28348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
13056 KB |
Output is correct |
2 |
Correct |
18 ms |
13184 KB |
Output is correct |
3 |
Correct |
16 ms |
13056 KB |
Output is correct |
4 |
Correct |
16 ms |
13056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1109 ms |
34252 KB |
Output is correct |
2 |
Correct |
1077 ms |
33928 KB |
Output is correct |
3 |
Correct |
665 ms |
26488 KB |
Output is correct |
4 |
Correct |
638 ms |
29248 KB |
Output is correct |
5 |
Correct |
245 ms |
23068 KB |
Output is correct |
6 |
Correct |
125 ms |
24192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1648 ms |
37512 KB |
Output is correct |
2 |
Correct |
1589 ms |
37544 KB |
Output is correct |
3 |
Correct |
1556 ms |
37224 KB |
Output is correct |
4 |
Correct |
1126 ms |
29836 KB |
Output is correct |
5 |
Correct |
844 ms |
30712 KB |
Output is correct |
6 |
Correct |
281 ms |
23492 KB |
Output is correct |
7 |
Correct |
161 ms |
24156 KB |
Output is correct |