Submission #1119588

#TimeUsernameProblemLanguageResultExecution timeMemory
1119588InvMODMuseum (CEOI17_museum)C++14
100 / 100
412 ms308176 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define gcd __gcd #define sz(v) (int) v.size() #define pb push_back #define pi pair<int,int> #define all(v) (v).begin(), (v).end() #define compact(v) (v).erase(unique(all(v)), (v).end()) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define dbg(x) "[" #x " = " << (x) << "]" ///#define int long long using ll = long long; using ld = long double; using ull = unsigned long long; template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;} template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;} const int N = 2e5+5; const ll MOD = 1e9+7; const ll INF = 1e18; void solve() { int n,k,start; cin >> n >> k >> start; vector<vector<pair<int,int>>> adj(n+1, vector<pair<int,int>>()); FOR(i, 1, n-1){ int u,v,c; cin >> u >> v >> c; adj[u].emplace_back(v, c); adj[v].emplace_back(u, c); } vector<int> sz; sz.resize(n+1); vector<vector<vector<ll>>> dp(n+1, vector<vector<ll>>(2, vector<ll>())); function<vector<vector<ll>>(vector<vector<ll>>&, vector<vector<ll>>&)> combine = [&](vector<vector<ll>>& dad, vector<vector<ll>>& child){ vector<vector<ll>> combined(2, vector<ll>(sz(dad[0]) + sz(child[0]) - 1, INF)); FOR(i, 0, sz(dad[0]) - 1){ combined[0][i] = dad[0][i]; combined[1][i] = dad[1][i]; } FOR(i, 1, sz(child[0]) - 1){ combined[0][i] = min(combined[0][i], child[0][i]); combined[1][i] = min(combined[1][i], child[1][i]); } FOR(i, 1, sz(dad[0]) - 1){ FOR(j, 1, sz(child[0]) - 1){ combined[0][i + j] = min(combined[0][i + j], dad[0][i] + child[0][j]); combined[1][i + j] = min(combined[1][i + j], dad[0][i] + child[1][j]); combined[1][i + j] = min(combined[1][i + j], dad[1][i] + child[0][j]); } } return combined; }; auto dfs = [&](auto&& dfs, int x, int p) -> void{ dp[x][0] = {(ll)1e12, (ll)0}; dp[x][1] = {(ll)1e12, (ll)0}; sz[x] = 1; for(pair<int,int> e : adj[x]){ int v = e.first, w = e.second; if(v != p){ dfs(dfs, v, x); FOR(i, 1, sz[v]){ dp[v][0][i] = dp[v][0][i] + 2ll * w; dp[v][1][i] = dp[v][1][i] + 1ll * w; } sz[x] += sz[v]; dp[x] = combine(dp[x], dp[v]); } } }; dfs(dfs, start, - 1); cout << min(dp[start][1][k], dp[start][0][k]) <<"\n"; return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "InvMOD" if(fopen(name".INP", "r")){ freopen(name".INP","r",stdin); freopen(name".OUT","w",stdout); } int t = 1; //cin >> t; while(t--) solve(); return 0; }

Compilation message (stderr)

museum.cpp: In function 'int main()':
museum.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
museum.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   99 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...