输入样例:
4
1 2 1 1
1 3 4 0
1 4 1 1
2 3 3 0
2 4 2 1
3 4 5 0
输出样例:
3
代码:
cpp
#include<iostream>
#include<queue>
using namespace std;
const int N=110;
struct node{
int x,y,w;
bool operator <(const node &n1)const{
if(w==n1.w) return x==n1.x? x>n1.x:y>n1.y;
return w>n1.w;
}
};
int ans;
int mp[N][N],f[N];
priority_queue<node>pq;
int fnd(int x){
return f[x]==x?x:f[x]=fnd(f[x]);
}
int main(){
int n; cin>>n;
for(int i=1;i<=n;i++)
f[i]=i;
for(int i=0;i<n*(n-1)/2;i++){
int u,v,w,st;
scanf("%d%d%d%d",&u,&v,&w,&st);
if(st){ f[fnd(u)]=fnd(v); }
else{
if(fnd(u)!=fnd(v)){ pq.push({u,v,w});}
}
}
while(pq.size()){
node tmp=pq.top(); pq.pop();
int u=tmp.x,v=tmp.y;
if(fnd(u)!=fnd(v)){
ans+=tmp.w;
f[fnd(u)]=fnd(v);
}
}
cout<<ans;
return 0;
}