이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <queue>
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
ll const inf = 1000000000000000000;
int const nmax = 100000;
int const sigma = 5;
ll dp[1 << sigma][1 + nmax];
int special[1 + nmax];
std::vector<std::pair<int,int>> g[1 + nmax];
void solvelayer(int mask, int n) {
std::priority_queue<std::pair<ll,int>, std::vector<std::pair<ll,int>>, std::greater<std::pair<ll,int>>> pq;
for(int mask2 = mask; 0 < mask2; mask2 = (mask2 & (mask2 - 1)) & mask)
for(int i = 1; i <= n; i++)
dp[mask][i] = std::min(dp[mask][i], dp[mask2][i] + dp[mask2 ^ mask][i]);
for(int i = 1; i <= n; i++)
pq.push({dp[mask][i], i});
while(0 < pq.size()) {
int node = pq.top().second;
ll cost = pq.top().first;
pq.pop();
if(dp[mask][node] != cost)
continue;
for(int h = 0; h < g[node].size();h ++) {
int to = g[node][h].first;
if(cost + g[node][h].second < dp[mask][to]) {
dp[mask][to] = cost + g[node][h].second;
pq.push({dp[mask][to], to});
}
}
}
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n, k, m;
std::cin >> n >> k >> m;
for(int i = 1; i < (1 << sigma); i++)
for(int j = 1; j <= n; j++)
dp[i][j] = inf;
for(int i = 1;i <= k; i++) {
int val;
std::cin >> val;
special[val] = i;
dp[(1 << (i - 1))][val] = 0;
}
for(int i = 1;i <= m; i++) {
int x, y, cost;
std::cin >> x >> y >> cost;
g[x].push_back({y, cost});
g[y].push_back({x, cost});
}
for(int mask = 0; mask < (1 << k); mask++) {
solvelayer(mask, n);
}
ll result = inf;
for(int i = 1; i <= n; i++)
result = std::min(result, dp[(1 << k) - 1][i]);
std::cout << result;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cities.cpp: In function 'void solvelayer(int, int)':
cities.cpp:34:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | for(int h = 0; h < g[node].size();h ++) {
| ~~^~~~~~~~~~~~~~~~
# | 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... |