#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const long long INF = 100000000000000 + 14;
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 b[N];
vector<ban> a[N];
long long dp[1 << 6][N];
bool c[N];
void djk(int x)
{
for (int i = 1; i <= n; ++i)
c[i] = false;
priority_queue<ban> q;
q.push(ban(b[x], 0));
while (1)
{
ban t;
do
{
if (q.empty())
return;
t = q.top();
q.pop();
} while (c[t.x]);
c[t.x] = true;
dp[1 << x][t.x] = t.d;
for (int i = 0; i < a[t.x].size(); ++i)
{
ban h = a[t.x][i];
h.d += t.d;
q.push(h);
}
}
}
void djk1(int x)
{
for (int i = 1; i <= n; ++i)
c[i] = false;
priority_queue<ban> q;
for (int i = 1; i <= n; ++i)
q.push(ban(i, dp[x][i]));
while (1)
{
ban t;
do
{
if (q.empty())
return;
t = q.top();
q.pop();
} while(c[t.x]);
c[t.x] = true;
dp[x][t.x] = t.d;
for (int i = 0; i < a[t.x].size(); ++i)
{
ban h = a[t.x][i];
h.d += t.d;
q.push(h);
}
}
}
int main()
{
//freopen("input.txt", "r", stdin);
cin >> n >> k >> m;
for (int i = 0; i < k; ++i)
cin >> b[i];
for (int i = 0; i < m; ++i)
{
int x, y, z;
cin >> 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[x][i] = INF;
}
for (int i = 0; i < k; ++i)
djk(i);
for (int x = 0; x < (1 << k); ++x)
{
int q = 0;
for (int i = 0; i < k; ++i)
if ((x&(1 << i)))
++q;
if (q <= 1)
continue;
for (int y = x; y; y = (y - 1) & x)
{
int z = x - y;
if (z == 0)
continue;
for (int i = 1; i <= n; ++i)
dp[x][i] = min(dp[x][i], dp[z][i] + dp[y][i]);
}
djk1(x);
}
long long ans = INF;
for (int i = 1; i <= n; ++i)
ans = min(ans, dp[(1 << k) - 1][i]);
cout << ans << endl;
return 0;
}
Compilation message
cities.cpp: In function 'void djk(int)':
cities.cpp:48:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[t.x].size(); ++i)
~~^~~~~~~~~~~~~~~
cities.cpp: In function 'void djk1(int)':
cities.cpp:76:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < a[t.x].size(); ++i)
~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2804 KB |
Output is correct |
3 |
Correct |
4 ms |
3008 KB |
Output is correct |
4 |
Correct |
4 ms |
3008 KB |
Output is correct |
5 |
Correct |
5 ms |
3044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1474 ms |
27696 KB |
Output is correct |
2 |
Correct |
1460 ms |
29024 KB |
Output is correct |
3 |
Correct |
553 ms |
29024 KB |
Output is correct |
4 |
Correct |
1040 ms |
29024 KB |
Output is correct |
5 |
Correct |
768 ms |
29024 KB |
Output is correct |
6 |
Correct |
568 ms |
29024 KB |
Output is correct |
7 |
Correct |
10 ms |
29024 KB |
Output is correct |
8 |
Correct |
8 ms |
29024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
29024 KB |
Output is correct |
2 |
Correct |
13 ms |
29024 KB |
Output is correct |
3 |
Correct |
9 ms |
29024 KB |
Output is correct |
4 |
Correct |
13 ms |
29024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2892 ms |
36316 KB |
Output is correct |
2 |
Correct |
3005 ms |
39824 KB |
Output is correct |
3 |
Correct |
1199 ms |
39824 KB |
Output is correct |
4 |
Correct |
2569 ms |
45416 KB |
Output is correct |
5 |
Correct |
2278 ms |
45416 KB |
Output is correct |
6 |
Correct |
2162 ms |
48636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5706 ms |
66900 KB |
Output is correct |
2 |
Correct |
5730 ms |
70940 KB |
Output is correct |
3 |
Correct |
5474 ms |
74364 KB |
Output is correct |
4 |
Correct |
2484 ms |
74364 KB |
Output is correct |
5 |
Correct |
5313 ms |
74364 KB |
Output is correct |
6 |
Correct |
4093 ms |
74364 KB |
Output is correct |
7 |
Correct |
3813 ms |
74364 KB |
Output is correct |