比赛 |
NNOI2024 |
评测结果 |
AWWWEEEEEE |
题目名称 |
连珠线 |
最终得分 |
10 |
用户昵称 |
袁书杰 |
运行时间 |
1.373 s |
代码语言 |
C++ |
内存使用 |
6.54 MiB |
提交时间 |
2024-12-28 21:38:39 |
显示代码纯文本
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[1005][1005],ans;
bool point[1005];
signed main() {
freopen("line.in","r",stdin);
freopen("line.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1; i<n; i++) {
int x,y,z;
cin>>x>>y>>z;
a[x][y]=a[y][x]=z;
}
while(1) {
int the_main_max=0;
pair<int,int> the_end_first_x_and_y,the_end_second_x_and_y;
pair<int,int> the_first_x_and_y,the_second_x_and_y;
for(int i=1; i<=n; i++) {
int maxn_first=0,maxn_second=0;
for(int j=1; j<=n; j++) {
if(maxn_first<=a[i][j]) {
maxn_first=a[i][j];
the_first_x_and_y.first=i;
the_first_x_and_y.second=j;
}
}
for(int j=1; j<=n; j++) {
if(maxn_first!=a[i][j]) {
if(maxn_second<=a[i][j]) {
maxn_second=a[i][j];
the_second_x_and_y.first=i;
the_second_x_and_y.second=j;
}
}
}
if(the_first_x_and_y.first==the_second_x_and_y.first&&point[the_second_x_and_y.first]){
maxn_first=0;
}
if(the_first_x_and_y.first==the_second_x_and_y.second&&point[the_second_x_and_y.second]){
maxn_first=0;
}
if(the_first_x_and_y.second==the_second_x_and_y.first&&point[the_second_x_and_y.first]){
maxn_first=0;
}
if(the_first_x_and_y.second==the_second_x_and_y.second&&point[the_second_x_and_y.second]){
maxn_first=0;
}
if(maxn_first!=0&&maxn_second!=0) {
if(the_main_max<=maxn_first+maxn_second) {
the_main_max=maxn_first+maxn_second;
the_end_first_x_and_y=the_first_x_and_y;
the_end_second_x_and_y=the_second_x_and_y;
}
}
}
if(the_main_max==0) {
break;
} else {
ans+=the_main_max;
a[the_end_first_x_and_y.first][the_end_first_x_and_y.second]=0;
a[the_end_first_x_and_y.second][the_end_first_x_and_y.first]=0;
a[the_end_second_x_and_y.first][the_end_second_x_and_y.second]=0;
a[the_end_second_x_and_y.second][the_end_second_x_and_y.first]=0;
if(the_end_first_x_and_y.first==the_end_second_x_and_y.second){
point[the_end_first_x_and_y.first]=true;
}
if(the_end_first_x_and_y.first==the_end_second_x_and_y.first){
point[the_end_first_x_and_y.first]=true;
}
if(the_end_first_x_and_y.second==the_end_second_x_and_y.second){
point[the_end_first_x_and_y.second]=true;
}
if(the_end_first_x_and_y.second==the_end_second_x_and_y.first){
point[the_end_first_x_and_y.second]=true;
}
}
}
cout<<ans;
return 0;
}
/*
5
1 2 10
1 3 40
1 4 15
1 5 20
*/