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;
    }
    
    
  • 相关阅读:
    Go 指针
    Go 字符串
    Go Maps
    Go 可变参数函数
    Go 数组和切片
    pyqt5实现窗口跳转并关闭上一个窗口
    spy++查找窗口句柄
    Python中Tk模块简单窗口设计
    pyqt5无边框拖动
    pyqt5 GUI教程
  • 原文地址:https://www.cnblogs.com/Pickupwin/p/9028912.html
Copyright © 2011-2022 走看看