zoukankan      html  css  js  c++  java
  • POJ 1251Jungle Roads

    题意:最小生成树模板题,主要是输入方面的处理,数据不大,可以采用cin输入,也可以用scanf用getchar()来控制即可;

    #include<algorithm>
    #include<iostream>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<map>
    #include<set>
    #include<string>
    #include<cstring>
    using namespace std;
    typedef struct node{
    	int x;int y;int date;
    }node;
    bool cmp(node b,node c){
    	return b.date<c.date;
    }
    int first[38];int next[38];
    void csh(int m){
    	for(int i=1;i<=m;i++){
    		first[i]=i;
    	}
    	for(int i=1;i<=m;i++){
    		next[i]=1;
    	}
    	return ;
    }
    int find(int x){
    	if(first[x]==x)  return x;
    	else{
    		x=first[x];
    		return find(x);
    	}
    }
    void hebin(int x,int y){
    	x=find(x);
    	y=find(y);
    	if(x==y)  return ;
    	else{
    		if(next[x]>next[y]){
    			first[y]=x;
    		}
    		else{
    			first[x]=y;
    			if(next[x]==next[y]){
    				next[y]++;
    			}
    		}
    	}
    	return ;
    }
    node a[500];
    int main(){
    	int n;
    	while(cin>>n&&n!=0){
    		int k=0;
    		for(int i=1;i<=n-1;i++){
    		  char ch;int p;
    		  cin>>ch>>p;
    		  char ch1;int w;
    		  for(int j=1;j<=p;j++){
    		  	cin>>ch1>>w;
    		  	k++;
    		  	a[k].x=(ch-'A'+1);
    		  	a[k].y=(ch1-'A'+1);
    		  	a[k].date=w;
    		  }	
    		}
    		csh(n);
    		sort(a,a+k+1,cmp);
    		int sum=0;
    		int l=0;
    		for(int i=1;i<=k;i++){
    			if(find(a[i].x)!=find(a[i].y)){
    				sum+=a[i].date;
    				l++;
    				hebin(a[i].x,a[i].y);
    				if(l==(n-1)){
    					break;
    				}
    			}
    		}
    		cout<<sum<<endl;
    	}
    	return 0;
    }


  • 相关阅读:
    一种不求交点确定直线与三角形是否相交的方法
    碰撞边界锯齿的平滑方法
    demo的凹凸贴图效果
    MySQL进阶篇触发器
    MySQL进阶篇索引
    Maven的POM文件详解
    Swagger
    MySQL进阶篇存储过程
    SpringBoot基础篇
    MySQL基础篇多表操作
  • 原文地址:https://www.cnblogs.com/wang9897/p/7624389.html
Copyright © 2011-2022 走看看