Submission #717328

#TimeUsernameProblemLanguageResultExecution timeMemory
717328vjudge1Paths (RMI21_paths)C++17
56 / 100
291 ms676 KiB
#include <bits/stdc++.h> #include <fstream> #define endl '\n' #define mod 998244353 #define INF 1000000000000000000 #define ll long long ///#define cin fin ///#define cout fout #define fi first #define se second using namespace std; ///ofstream fout("herding.out"); ///ifstream fin("herding.in"); const int N = 2e3 + 5; vector<pair<int,long long>> v[N]; long long mx[N]; priority_queue<long long> pq; void dfs(int s, int p) { pair<int,long long> fin = {-1,-1}; for(auto i : v[s]) { if(i.first != p) { dfs(i.first,s); if(fin.second < mx[i.first]+i.second) { fin.first = i.first; fin.second = mx[i.first]+i.second; } } } for(auto i : v[s]) { if(i.first != p && i.first != fin.first) { pq.push(mx[i.first]+i.second); } } if(fin.second != -1) mx[s] = fin.second; if(p == 0) pq.push(fin.second); } int main() { ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0); int n, k; cin >> n >> k; for(int i = 0; i < n-1; i++) { int a, b, c; cin >> a >> b >> c; v[a].push_back({b,c}); v[b].push_back({a,c}); } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) mx[j] = 0; while(!pq.empty())pq.pop(); long long ans = 0; int num = k; dfs(i,0); while(num != 0 && !pq.empty()) { ans += pq.top(); pq.pop(); num--; } cout << ans << endl; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...