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;
#define ar array
const int N = 20;
int d[N][N], cc[N][N];
ar<int, 2> dp[1<<N][N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
memset(d, 63, sizeof d);
for(int i=0;i<m;i++){
int a, b, c; cin>>a>>b>>c;
d[a][b] = d[b][a] = c;
if(c>1) cc[a][b] = cc[b][a] = 1;
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(d[i][k] + d[k][j] < d[i][j]){
d[i][j] = d[i][k] + d[k][j];
cc[i][j] = cc[i][k] + cc[k][j];
} else if(d[i][k] + d[k][j] == d[i][j]){
cc[i][j] = min(cc[i][j], cc[i][k] + cc[k][j]);
}
}
}
}
memset(dp, 63, sizeof dp);
for(int i=0;i<n;i++) dp[1<<i][i] = {0, 0};
for(int mask=0;mask<(1 << n);mask++){
for(int j=0;j<n;j++){
if(mask >> j & 1) continue;
for(int k=0;k<n;k++){
if(mask >> k & 1){
dp[mask | (1 << j)][j] = min(dp[mask | (1 << j)][j], {dp[mask][k][0] + d[k][j], dp[mask][k][1] + cc[k][j]});
}
}
}
}
const int inf = 2e9;
ar<int, 2> res = {inf, inf};
for(int i=0;i<n;i++){
res = min(res, dp[(1 << n) - 1][i]);
}
assert(res[1] <= 2);
cout<<res[0]<<"\n";
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |