#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m, sz, t[100001];
ll d[100001];
vector <pair<int, int>> g[100001];
vector <int> v;
const ll inf = 1000000000000000000;
struct result {
bitset <100001> f;
ll cost;
result()
{
f = 0;
cost = inf;
}
}a[32];
struct nod {
int key;
bool operator < (const nod& aux) const
{
return d[key] > d[aux.key];
}
};
int main() {
cin >> n >> sz >> m;
for (int i = 1; i <= sz; i++)
{
int x;
cin >> x;
v.push_back(x);
}
for (int i = 1; i <= m; i++)
{
int x, y, cost;
cin >> x >> y >> cost;
g[x].push_back({ y, cost });
g[y].push_back({ x, cost });
}
for (int i = 0; i < v.size(); i++)
a[(1 << i)].f[v[i]] = true, a[(1 << i)].cost = 0;
priority_queue <nod> Q;
for (int i = 1; i < (1 << sz); i++)
for (int j = 1; j < (1 << sz); j++)
{
if (i & j)
continue;
int aux = (i | j);
if ((a[i].f & a[j].f).any())
continue;
for (int k = 1; k <= n; k++)
if (a[i].f[k])
d[k] = 0, Q.push({ k }), t[k] = k;
else
d[k] = inf;
ll cost = inf;
int last;
while (!Q.empty())
{
int key = Q.top().key;
Q.pop();
if (a[j].f[key] && d[key] < cost)
cost = d[key], last = key;
for (auto& k : g[key])
if (d[k.first] > d[key] + k.second)
{
d[k.first] = d[key] + k.second;
t[k.first] = key;
Q.push({ k.first });
}
}
ll newCost = a[i].cost + a[j].cost + cost;
if (newCost >= a[aux].cost)
continue;
a[aux].cost = newCost;
a[aux].f = (a[i].f | a[j].f);
while (t[last] != last)
{
a[aux].f[last] = true;
last = t[last];
}
}
cout << a[(1 << sz) - 1].cost;
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:53:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for (int i = 0; i < v.size(); i++)
| ~~^~~~~~~~~~
cities.cpp:104:26: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
104 | while (t[last] != last)
| ~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3052 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
747 ms |
15080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
3064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2467 ms |
14992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
6010 ms |
15004 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |