第一题:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x , y , z;
};
node v[20005];
int fa[1005];
int n , m , cnt=0 , sum=0;
bool cmp (node x , node y)
{
return x.z>y.z;
}
int find (int x)
{
if (fa[x]==x) return x;
return fa[x]=find (fa[x]);
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i=1;i<=n;i++)
{
fa[i]=i;
}
cnt=n;
for (int i=1;i<=m;i++)
{
cin >> v[i].x >> v[i].y >> v[i].z;
}
sort (v+1 , v+m+1 , cmp);
for (int i=1;i<=m;i++)
{
int nx=find (v[i].x) , ny=find (v[i].y);
if (nx==ny) continue;
fa[ny]=nx;
cnt--;
sum+=v[i].z;
if (cnt==1)
{
cout << sum;
return 0;
}
}
cout << -1;
return 0;
} 第二题:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x , y , z;
};
node v[10005];
int fa[1005];
int n , m , k , cnt=0 , sum=0;
bool cmp (node x , node y)
{
return x.z<y.z;
}
int find (int x)
{
if (fa[x]==x) return x;
return fa[x]=find (fa[x]);
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
cnt=n;
for (int i=1;i<=n;i++)
{
fa[i]=i;
}
for (int i=1;i<=m;i++)
{
cin >> v[i].x >> v[i].y >> v[i].z;
}
if (n==k)
{
cout << 0;
return 0;
}
sort (v+1 , v+m+1 , cmp);
for (int i=1;i<=m;i++)
{
int nx=find (v[i].x) , ny=find (v[i].y);
if (nx==ny) continue;
cnt--;
fa[ny]=nx;
sum+=v[i].z;
if (cnt==k)
{
cout << sum;
return 0;
}
}
cout << "No Answer";
return 0;
} 第三题:
#include <bits/stdc++.h>
#define int long long
using namespace std;
double dis[5005];
bool vis[5005];
int x[5005] , y[5005];
int n;
double sum=0;
double JL (int a , int b)
{
double nx=x[a]-x[b] , ny=y[a]-y[b];
return sqrt (nx*nx+ny*ny);
}
signed main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> x[i] >> y[i];
dis[i]=1e20;
}
dis[1]=0;
for (int i=1;i<=n;i++)
{
double minn=1e18;
int id;
for (int j=1;j<=n;j++)
{
if (vis[j]==0&&dis[j]<minn)
{
minn=dis[j];
id=j;
}
}
vis[id]=1;
sum+=minn;
for (int j=1;j<=n;j++)
{
dis[j]=min (dis[j] , JL (id , j));
}
}
printf ("%.2lf" , sum);
return 0;
} 第四题:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int id , x , y , a , b;
};
struct NODE
{
int x , y;
};
node v[20005];
int fa[10005];
NODE t[20005];
int n , m , k;
int cnt=0 , maxn=0 , now=0;
bool cmp1 (node x , node y)
{
return x.a<y.a;
}
bool cmp2 (node x , node y)
{
return min (x.a , x.b)<min (y.a , y.b);
}
bool cmp3 (NODE x , NODE y)
{
return x.x<y.x;
}
int find (int x)
{
if (fa[x]==x) return x;
return fa[x]=find (fa[x]);
}
int main ( )
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k >> m;
m--;
cnt=n;
for (int i=1;i<=n;i++)
{
fa[i]=i;
}
for (int i=1;i<=m;i++)
{
cin >> v[i].x >> v[i].y >> v[i].a >> v[i].b;
v[i].id=i;
}
int p;
sort (v+1 , v+m+1 , cmp1);
for (int i=1;i<=m;i++)
{
int nx=find (v[i].x) , ny=find (v[i].y);
if (nx==ny) continue;
cnt--;
t[++now]={v[i].id , 1};
fa[ny]=nx;
maxn=max (maxn , v[i].a);
p=i;
if (cnt==n-k) break;
}
if (cnt==1)
{
sort (t+1 , t+now+1 , cmp3);
cout << maxn << "\n";
for (int i=1;i<=now;i++)
{
cout << t[i].x << " " << t[i].y << "\n";
}
return 0;
}
sort (v+p+1 , v+m+1 , cmp2);
for (int i=p;i<=m;i++)
{
int nx=find (v[i].x) , ny=find (v[i].y);
if (nx==ny) continue;
cnt--;
t[++now]={v[i].id , 2};
fa[ny]=nx;
maxn=max (maxn , min (v[i].a , v[i].b));
p=i;
if (cnt==1)
{
sort (t+1 , t+now+1 , cmp3);
cout << maxn << "\n";
for (int i=1;i<=now;i++)
{
cout << t[i].x << " " << t[i].y << "\n";
}
return 0;
}
}
return 0;
}