이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// ~ Be Name Khoda ~ //
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxn = 1e6 + 10;
const int maxn5 = 1e5 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 1e18;
vector <pair<int, int>> adj[maxn5];
ll dp[1 << 5 + 1][maxn5];
int imp[maxn5], per[maxn5], cmpmask;
inline bool cmp(int i, int j){return dp[cmpmask][i] < dp[cmpmask][j];}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n, k, m; cin >> n >> k >> m;
for(int i = 0; i < k; i++){
cin >> imp[i];
imp[i]--;
}
for(int i = 0; i < m; i++){
int a, b, c; cin >> a >> b >> c;
a--; b--;
adj[a].pb({b, c});
adj[b].pb({a, c});
}
for(int mask = 0; mask < (1 << k); mask++) for(int i = 0; i < n; i++)
dp[mask][i] = inf;
for(int i = 0; i < n; i++)
dp[0][i] = 0;
for(int i = 0; i < k; i++){
dp[1 << i][i] = 0;
for(auto [u, w] : adj[imp[i]])
dp[1 << i][u] = w;
}
for(int i = 0; i < n; i++)
per[i] = i;
for(int mask = 0; mask < (1 << k); mask++){
cmpmask = mask;
sort(per, per + n, cmp);
for(int i = 0; i < n; i++){
int v = per[i];
int nmask = (1 << k) - 1 - mask;
for(int sub = nmask; sub; sub = (sub - 1) & nmask){
dp[mask^sub][v] = min(dp[mask^sub][v], dp[mask][v] + dp[sub][v]);
}
for(auto [u, w] : adj[v])
dp[mask][u] = min(dp[mask][u], dp[mask][v] + w);
}
}
ll ans = inf;
for(int i = 0; i < n; i++)
ans = min(ans, dp[(1 << k) - 1][i]);
cout << ans << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
cities.cpp:23:14: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
23 | ll dp[1 << 5 + 1][maxn5];
| ~~^~~
# | 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... |