[USACO05MAR] Out of Hay S
#include <bits/stdc++.h>
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
#define int long long
#define INF 2147483647
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"
using namespace std;
pair<int,pair<int,int> >va[(int)1e6];
int n,m,k;
int acc[5010];
int find(int s){
if(acc[s]!=s)acc[s]=find(acc[s]);
return acc[s];
}
int num(){
int ll=0;
for(int i=1;i<=n;i++){
if(i==acc[i])ll++;
}
return ll;
}
void krukal(){
int cnt=0,sum=0,maxn=-INF;
for(int i=1;i<=n;i++)acc[i]=i;
for(int i=1;i<=m;i++){
int a=va[i].se.fi,b=va[i].se.se,c=va[i].fi;
int t1=find(a),t2=find(b);
if(t1!=t2){
acc[t1]=t2;
maxn=max(maxn,c);
cnt++;
if(cnt==n-1){
cout<<maxn;
return ;
}
}
}
}
signed main(){
IOS;
cin>>n>>m;
for(int i=1;i<=m;i++){
int a,b,c;
cin>>a>>b>>c;
va[i]={c,{a,b}};
}
sort(va+1,va+m+1);
krukal();
return 0;
}
口袋的天空
#include <bits/stdc++.h>
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
#define int long long
#define INF 2147483647
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"
using namespace std;
pair<int,pair<int,int> >va[(int)1e6];
int n,m,k;
int acc[5010];
int find(int s){
if(acc[s]!=s)acc[s]=find(acc[s]);
return acc[s];
}
int num(){
int ll=0;
for(int i=1;i<=n;i++){
if(i==acc[i])ll++;
}
return ll;
}
void krukal(){
int cnt,sum=0;
for(int i=1;i<=n;i++)acc[i]=i;
for(int i=1;i<=m;i++){
int a=va[i].se.fi,b=va[i].se.se,c=va[i].fi;
int t1=find(a),t2=find(b);
if(t1!=t2){
acc[t1]=t2;
cnt=num();
sum+=c;
if(cnt==k){
cout<<sum;
return ;
}
}
}
puts("No Answer");
}
signed main(){
IOS;
cin>>n>>m>>k;
for(int i=1;i<=m;i++){
int a,b,c;
cin>>a>>b>>c;
va[i]={c,{a,b}};
}
sort(va+1,va+m+1);
krukal();
return 0;
}
[USACO07DEC] Building Roads S
#include <bits/stdc++.h>
#define PII pair<int,int>
#define fi first
#define se second
#define pb push_back
#define int long long
#define INF 2147483647
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "\n"
using namespace std;
int n,m,cntt;
double sum;
const int N=1e3+5;
pair<int,int > spot[N];
int acc[N];
bool vis[N][N];
pair<double,pair<int,int > > va[N][N];
pair<double,pair<int,int > > vb[1000100];
double dis(int a,int b){
double dx=spot[a].fi-spot[b].fi;
double dy=spot[a].se-spot[b].se;
return sqrt(pow(dx,2)+pow(dy,2));
}
int num(){
int ll=0;
for(int i=1;i<=n;i++){
if(i==acc[i])ll++;
}
return ll;
}
int find(int s){
if(acc[s]!=s)acc[s]=find(acc[s]);
return acc[s];
}
void krukal(){
int cnt=0;
for(int i=1;i<=n;i++)acc[i]=i;
for(int i=1;i<=cntt;i++){
int a=vb[i].se.fi,b=vb[i].se.se;
int t1=find(a),t2=find(b);
if(t1!=t2&&a!=b){
acc[t1]=t2;
if(!vis[a][b]){
sum+=dis(a,b);
cnt++;
vis[a][b]=1;
}
if(num()==1){
return ;
}
}
}
}
signed main(){
IOS;
cin>>n>>m;
for(int i=1;i<=n;i++){
int x,y;
cin>>x>>y;
spot[i]={x,y};
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
va[i][j]={INF,{i,j}};
cntt++;
}
}
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
if(a>b)swap(a,b);
va[a][b].fi=0;
vis[a][b]=1;
}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(va[i][j].fi==INF)va[i][j].fi=dis(i,j);
}
}
int t=0;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
vb[++t]=va[i][j];
}
}
sort(vb+1,vb+cntt+1);
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// cout<<i<<" "<<j<<":"<<va[i][j].fi<<" "<<va[i][j].se.fi<<" "<<va[i][j].se.se<<" ";
// }
// cout<<endl;
// }
// for(int i=1;i<=cntt;i++)cout<<vb[i].fi<<" "<<vb[i].se.fi<<" "<<vb[i].se.se<<" ";
krukal();
cout<<fixed<<setprecision(2)<<sum;
// cout<<endl<<cntt<<" "<<t;
return 0;
}