mst问题
#include<queue> #include<numeric> #include<iostream> using namespace std; const int N=104; struct road{ int src, dst, len, ok; road(int a,int b,int c,int d):src(a),dst(b),len(c),ok(d){} bool operator<(const road&x)const{return len>x.len;} }; priority_queue<road>ed; int pre[N],level[N]; void makeset(){ iota(pre,pre+N,0); } int findset(int k){ if(pre[k]!=k) pre[k]=findset(pre[k]); return pre[k]; } void link(int a,int b){ int x=findset(a),y=findset(b); if(level[x]>level[y])pre[y]=x; else{ pre[x]=y; if(level[x]==level[y]) ++level[y];} } int main(){ makeset(); int n;cin>>n; for(int i=0,t=n*(n-1)/2;i<t;++i){ int a,b,c,d;cin>>a>>b>>c>>d; ed.emplace(a,b,c,d); if(d)link(a,b); } int cost=0; while(ed.size()){ auto x=ed.top();ed.pop(); int a = x.src,b=x.dst; if(findset(a)!=findset(b)){ link(a,b); cost+=x.ok?0:x.len;} }//while cout<<cost; return 0; }