이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define nl '\n'
#define fu(i,a,b) for(int i=a; i<=b; i++)
#define fd(i,a,b) for(int i=a; i>=b; i--)
#define BIT(i, n) (((n)>>(i))&(1))
#define pii pair<int, int>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define SZ(V) (int)(V.size())
#define pb push_back
#define eb emplace_back
#define NAME "quan"
int t,n,m,k,q;
const int N = 1e4 + 5;
const int MOD = 1e9 + 7;
const int inf = 2e9 + 7;
void add(int &a, int b) {a+=b; if(a>=MOD) a-=MOD;}
void sub(int &a, int b) {a-=b; if(a<0) a+=MOD;}
// dp[u][k][0/1] : số bước ít nhất để đi được k đỉnh ở subtree u và có quay lại u hay không
int sz[N];
int dp[N][N][2];
vector<pii> adj[N];
int root;
void dfs(int u, int p){
dp[u][1][0] = dp[u][1][1] = 0;
sz[u] = 1;
for(pii it: adj[u]){
int v = it.ff;
int w = it.ss;
if(v == p) continue;
dfs(v, u);
fd(i,sz[u],1)
fd(j,sz[v],1){
dp[u][i + j][0] = min(dp[u][i + j][0], dp[u][i][1] + dp[v][j][0] + w);
dp[u][i + j][0] = min(dp[u][i + j][0], dp[u][i][0] + dp[v][j][1] + 2*w);
dp[u][i + j][1] = min(dp[u][i + j][1], dp[u][i][1] + dp[v][j][1] + 2*w);
}
sz[u] += sz[v];
}
}
void nhap(){
cin >> n >> k >> root;
fu(i,1,n-1){
int u,v,w;
cin >> u >> v >> w;
adj[u].eb(v, w);
adj[v].eb(u, w);
}
memset(dp, 0x3f, sizeof dp);
dfs(root, 0);
cout << dp[root][k][0] << nl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(NAME".inp", "r")){
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
nhap();
}
컴파일 시 표준 에러 (stderr) 메시지
museum.cpp: In function 'int main()':
museum.cpp:69:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen(NAME".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:70:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen(NAME".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |