#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++)
{
bool ok = false;
for (int k = 0; k < sz; k++)
if (!((1 << k) & i) && ((1 << k) & j))
{
ok = true;
break;
}
if (ok)
continue;
int jj = (i ^ j);
if (jj == 0)
continue;
if ((a[j].f & a[jj].f).any())
continue;
for (int k = 1; k <= n; k++)
if (a[j].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[jj].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[j].cost + a[jj].cost + cost;
if (newCost >= a[i].cost)
continue;
a[i].cost = newCost;
a[i].f = (a[j].f | a[jj].f);
while (t[last] != last)
{
a[i].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:116:26: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
116 | while (t[last] != last)
| ~~~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3028 KB |
Output is correct |
2 |
Correct |
2 ms |
3028 KB |
Output is correct |
3 |
Incorrect |
2 ms |
3028 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
804 ms |
10832 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
3028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2609 ms |
10824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6044 ms |
10748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |