zoukankan      html  css  js  c++  java
  • POJ 3723 Conscription

    最大生成树
    cin cout 被卡不开心

    #include <cstdio>
    #include <algorithm>
    
    using std::sort;
    
    const int MAXV=20011;
    const int MAXE=50011;
    
    int read(){
    	int x=0, f=1;char ch=getchar();
    	while(ch<'0' || ch>'9'){if(ch=='-')f=-f;ch=getchar();}
    	while(ch>='0' && ch<='9'){x=x*10+(ch-'0');ch=getchar();}
    	return x*f;
    }
    
    int T;
    int N1, N2, M;
    
    int Vcnt=0;
    int F[MAXV];
    int B[MAXV], G[MAXV];
    
    struct Edge{
    	int x, y, l;
    } E[MAXE];
    
    bool cmpl(Edge A, Edge B){
    	return A.l>B.l;
    }
    
    int Find(int a){
    	if(F[F[a]]!=F[a])	F[a]=Find(F[a]);
    	return F[a];
    }
    
    int Ans=0;
    
    int main(){
    	
    	T=read();
    	
    	while(T--){
    		
    		N1=read();N2=read();M=read();
    		
    		Vcnt=0;
    		for(int i=1;i<=N1;++i){
    			B[i]=++Vcnt;
    			F[Vcnt]=Vcnt;
    		}
    		for(int i=1;i<=N2;++i){
    			G[i]=++Vcnt;
    			F[Vcnt]=Vcnt;
    		}
    		
    		for(int i=1;i<=M;++i){
    			E[i].x=read();E[i].y=read();E[i].l=read();
    			++E[i].x;++E[i].y;
    			E[i].x=B[E[i].x];E[i].y=G[E[i].y];
    		}
    		
    		sort(E+1, E+M+1, cmpl);
    		
    		Ans=0;
    		for(int i=1, fx, fy;i<=M;++i){
    			fx=Find(E[i].x);fy=Find(E[i].y);
    			if(fx!=fy){
    				F[fx]=fy;
    				Ans+=E[i].l;
    			}
    		}
    		
    		Ans=(N1+N2)*10000-Ans;
    		printf("%d
    ", Ans);
    		
    	}
    	
    	return 0;
    }
    
    
  • 相关阅读:
    CentOS7安装MySql5.7
    环境变量配置
    Spring 注解
    MySQL
    常用命令
    Android Studio & IntelliJ IDEA常见问题与设置
    order by、group by、having的区别
    把WebStrom添加到右键菜单
    解决github访问速度慢的问题
    docker修改时区
  • 原文地址:https://www.cnblogs.com/Pickupwin/p/9028912.html
Copyright © 2011-2022 走看看