P5805 [SEERC2019] Graph and Cycles 题解
SAMSHAWCRAFT · · 题解
本题解旨在补充其他题解做法的推理过程。
注意本题中特别提及了点数是奇数,即
接下来思考如何统计答案,通过前面分析,一个顶点的
这是一个贪心问题,给序列从小到大排序,然后使
#include <iostream>
#include <algorithm>
#include <vector>
const int sz=1e3+9;
int n,m;
long long ans;
std::vector<int> edges[sz];
int main(){
std::cin.tie(nullptr)->sync_with_stdio(false);
std::cout.sync_with_stdio(false);
std::cin>>n;
m=(n*(n-1))>>1;
for(int cx=0,u,v,w;cx!=m;++cx){
std::cin>>u>>v>>w;
edges[u].push_back(w);
edges[v].push_back(w);
}
for(int cx=1;cx<=n;++cx)
std::sort(edges[cx].begin(),edges[cx].end());
for(int cx=1;cx<=n;++cx){
for(int cy=1;cy!=n;cy+=2)
ans+=edges[cx][cy];
}
std::cout<<ans<<std::endl;
return 0;
}