比赛的时候以为是道神题,原来是水题
分别对处于0的位置的和处于1的位置的进行最小生成树
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
struct node{
int x;
int y;
int num;
}line1[1100],line2[1100];
int f[1001];
struct node1{
int from;
int to;
double dist;
}dis1[1100000],dis2[1100000];
double dist[1100000];
bool cmp(node1 a,node1 b){
return a.dist<b.dist;
}
int find(int x){
if(f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
bool mercy(int x,int y){
x=find(x);
y=find(y);
if(x==y)
return false;
else
f[x]=y;
return true;
}
bool cmp1(double x,double y){
return x>y;
}
int main(){
int t;
scanf("%d",&t);
int D,n;
int x,y,val;
while(t--){
scanf("%d%d",&D,&n);
int num1=0;
int num2=0;
for(int i=1;i<=n;i++){
f[i]=i;
scanf("%d%d%d",&x,&y,&val);
if(val==0){
line1[num1].x=x;
line1[num1].y=y;
line1[num1].num=i;
//cout<<line1[num1].x<<"PPPPP"<<line1[num1].y<<"PPPP"<<line1[num1].num<<endl;
num1++;
}
else if(val==1){
line2[num2].x=x;
line2[num2].y=y;
line2[num2].num=i;
//cout<<line2[num2].x<<"PPPPP"<<line2[num2].y<<"PPPP"<<line2[num2].num<<endl;
num2++;
}
}
int num3=0;
for(int i=0;i<num1;i++)
for(int j=i+1;j<num1;j++){
dis1[num3].dist=sqrt((line1[i].x-line1[j].x)*(line1[i].x-line1[j].x)+(line1[i].y-line1[j].y)*(line1[i].y-line1[j].y));
dis1[num3].from=line1[i].num;
dis1[num3].to=line1[j].num;
//cout<<dis1[num3].from<<"PPPPP"<< dis1[num3].to<<"PPPP"<<dis1[num3].dist<<endl;
num3++;
}
int num4=0;
for(int i=0;i<num2;i++)
for(int j=i+1;j<num2;j++){
dis2[num4].dist=sqrt((line2[i].x-line2[j].x)*(line2[i].x-line2[j].x)+(line2[i].y-line2[j].y)*(line2[i].y-line2[j].y));
dis2[num4].from=line2[i].num;
dis2[num4].to=line2[j].num;
num4++;
}
sort(dis1,dis1+num3,cmp);
//cout<<num3<<endl;
//for(int i=0;i<num3;i++)
//cout<<dis1[i].from<<"PPPPP"<< dis1[i].to<<"PPPP"<<dis1[i].dist<<endl;
sort(dis2,dis2+num4,cmp);
//cout<<num4<<endl;
//for(int i=0;i<num4;i++)
//cout<<dis2[i].from<<"PPPPP"<< dis2[i].to<<"PPPP"<<dis2[i].dist<<endl;
double sum=0;
int num5=0;
for(int i=0;i<num3;i++){
if(mercy(dis1[i].from,dis1[i].to)){
sum+=dis1[i].dist;
dist[num5]=dis1[i].dist;
num5++;
}
}
for(int i=0;i<num4;i++){
if(mercy(dis2[i].from,dis2[i].to)){
sum+=dis2[i].dist;
dist[num5]=dis2[i].dist;
num5++;
}
}
dist[num5]=9999999;
for(int i=0;i<num1;i++)
for(int j=0;j<num2;j++){
double coust=sqrt((line1[i].x-line2[j].x)*(line1[i].x-line2[j].x)+(line1[i].y-line2[j].y)*(line1[i].y-line2[j].y));
dist[num5]=min(dist[num5],coust);
}
if(dist[num5]!=9999999)
sum+=dist[num5];
sort(dist,dist+num5,cmp1);
for(int i=0;i<D;i++){
sum-=dist[i];
}
if(sum<0)
printf("0.00\n");
else
printf("%.2lf\n",sum);
}
return 0;
}
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
struct node{
int x;
int y;
int num;
}line1[1100],line2[1100];
int f[1001];
struct node1{
int from;
int to;
double dist;
}dis1[1100000],dis2[1100000];
double dist[1100000];
bool cmp(node1 a,node1 b){
return a.dist<b.dist;
}
int find(int x){
if(f[x]!=x)
f[x]=find(f[x]);
return f[x];
}
bool mercy(int x,int y){
x=find(x);
y=find(y);
if(x==y)
return false;
else
f[x]=y;
return true;
}
bool cmp1(double x,double y){
return x>y;
}
int main(){
int t;
scanf("%d",&t);
int D,n;
int x,y,val;
while(t--){
scanf("%d%d",&D,&n);
int num1=0;
int num2=0;
for(int i=1;i<=n;i++){
f[i]=i;
scanf("%d%d%d",&x,&y,&val);
if(val==0){
line1[num1].x=x;
line1[num1].y=y;
line1[num1].num=i;
//cout<<line1[num1].x<<"PPPPP"<<line1[num1].y<<"PPPP"<<line1[num1].num<<endl;
num1++;
}
else if(val==1){
line2[num2].x=x;
line2[num2].y=y;
line2[num2].num=i;
//cout<<line2[num2].x<<"PPPPP"<<line2[num2].y<<"PPPP"<<line2[num2].num<<endl;
num2++;
}
}
int num3=0;
for(int i=0;i<num1;i++)
for(int j=i+1;j<num1;j++){
dis1[num3].dist=sqrt((line1[i].x-line1[j].x)*(line1[i].x-line1[j].x)+(line1[i].y-line1[j].y)*(line1[i].y-line1[j].y));
dis1[num3].from=line1[i].num;
dis1[num3].to=line1[j].num;
//cout<<dis1[num3].from<<"PPPPP"<< dis1[num3].to<<"PPPP"<<dis1[num3].dist<<endl;
num3++;
}
int num4=0;
for(int i=0;i<num2;i++)
for(int j=i+1;j<num2;j++){
dis2[num4].dist=sqrt((line2[i].x-line2[j].x)*(line2[i].x-line2[j].x)+(line2[i].y-line2[j].y)*(line2[i].y-line2[j].y));
dis2[num4].from=line2[i].num;
dis2[num4].to=line2[j].num;
num4++;
}
sort(dis1,dis1+num3,cmp);
//cout<<num3<<endl;
//for(int i=0;i<num3;i++)
//cout<<dis1[i].from<<"PPPPP"<< dis1[i].to<<"PPPP"<<dis1[i].dist<<endl;
sort(dis2,dis2+num4,cmp);
//cout<<num4<<endl;
//for(int i=0;i<num4;i++)
//cout<<dis2[i].from<<"PPPPP"<< dis2[i].to<<"PPPP"<<dis2[i].dist<<endl;
double sum=0;
int num5=0;
for(int i=0;i<num3;i++){
if(mercy(dis1[i].from,dis1[i].to)){
sum+=dis1[i].dist;
dist[num5]=dis1[i].dist;
num5++;
}
}
for(int i=0;i<num4;i++){
if(mercy(dis2[i].from,dis2[i].to)){
sum+=dis2[i].dist;
dist[num5]=dis2[i].dist;
num5++;
}
}
dist[num5]=9999999;
for(int i=0;i<num1;i++)
for(int j=0;j<num2;j++){
double coust=sqrt((line1[i].x-line2[j].x)*(line1[i].x-line2[j].x)+(line1[i].y-line2[j].y)*(line1[i].y-line2[j].y));
dist[num5]=min(dist[num5],coust);
}
if(dist[num5]!=9999999)
sum+=dist[num5];
sort(dist,dist+num5,cmp1);
for(int i=0;i<D;i++){
sum-=dist[i];
}
if(sum<0)
printf("0.00\n");
else
printf("%.2lf\n",sum);
}
return 0;
}