# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
161954 | SamAnd | Cities (BOI16_cities) | C++17 | 3684 ms | 49880 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 100005, K = 5;
const long long INF = 1000000009000000009;
struct ban
{
int x;
long long d;
ban(){}
ban(int x, long long d)
{
this->x = x;
this->d = d;
}
};
bool operator<(const ban& a, const ban& b)
{
return a.d > b.d;
}
int n, k, m;
int u[K];
vector<ban> a[N];
long long dp[N][1 << K];
bool c[N];
void djk(int x)
{
memset(c, false, sizeof c);
priority_queue<ban> q;
for (int i = 1; i <= n; ++i)
q.push(ban(i, dp[i][x]));
while (1)
{
ban t;
do
{
if (q.empty())
{
return;
}
t = q.top();
q.pop();
} while (c[t.x]);
c[t.x] = true;
dp[t.x][x] = t.d;
for (int i = 0; i < a[t.x].size(); ++i)
{
ban h = a[t.x][i];
h.d += t.d;
if (!c[h.x])
q.push(h);
}
}
}
int main()
{
scanf("%d%d%d", &n, &k, &m);
for (int i = 0; i < k; ++i)
scanf("%d", &u[i]);
for (int i = 0; i < m; ++i)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
a[x].push_back(ban(y, z));
a[y].push_back(ban(x, z));
}
for (int x = 0; x < (1 << k); ++x)
{
for (int i = 1; i <= n; ++i)
dp[i][x] = INF;
}
for (int x = 1; x < (1 << k); ++x)
{
int qq = 0, ii;
for (int i = 0; i < k; ++i)
{
if ((x & (1 << i)))
{
++qq;
ii = i;
}
}
if (qq == 1)
{
dp[u[ii]][x] = 0;
}
for (int i = 1; i <= n; ++i)
{
for (int y = 0; y < k; ++y)
{
if (!(x & (1 << y)))
continue;
int z = (x ^ (1 << y));
dp[i][x] = min(dp[i][x], dp[i][z] + dp[i][(1 << y)]);
}
}
djk(x);
}
long long ans = INF;
for (int i = 1; i <= n; ++i)
ans = min(ans, dp[i][(1 << k) - 1]);
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |