zoukankan      html  css  js  c++  java
  • Jungle Roads

    这个查了一下解题思路,然后再去做的,说是直接用并查集和kruskal算法,所以我就先把模板炒上去了,可是有时runtime  error,差点没把我逼疯,数组溢出也可能出现这个可能,我试着把road数组加大,然后AC了,悲伤辣么大,快乐辣么小QAQ

    http://www.cnblogs.com/”http:/www.cnblogs.com/dongsheng/archive/2012/08/24/2654116.html

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<cstdlib>
    using namespace std;
    const int inf(1<<20);
    int p[30];
    struct node
    {
        int x,y,cost;
    }road[30];
    int cmp(node a,node b)
    {
        return a.cost<b.cost;
    }
    int find(int a)
    {
        return p[a]==a?a:p[a]=find(p[a]);
    }
    int main()
    {
        int n;
        while(cin>>n,n)
        {
            int i,j;
            for(i=0;i<27;i++)
                 p[i]=i;
            int k=0;
            for(i=0;i<n-1;i++)
            {
                char str[3];
                int m;
                cin>>str>>m;
                for(j=0;j<m;j++,k++)
                {
                    char str2[3];
                    int t;
                    cin>>str2>>t;
                    road[k].x=(int)(str[0]-'A');
                    road[k].y=(int)(str2[0]-'A');
                    road[k].cost=t;
                }
            }
            int ans=0;
            sort(road,road+k,cmp);
            for(int i=0;i<k;i++)
            {
                int fx=find(road[i].x);
                int fy=find(road[i].y);
                if(fx!=fy)
                {
                    ans+=road[i].cost;
                    p[fx]=fy;
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    IOS-github优秀开源项目大全
    IOS-UISearchBar
    iOS-资源大全
    基于java的https双向认证,android上亦可用
    三重Des对称加密在Android、Ios 和Java 平台的实现
    Python练习—文件
    C语言文件进阶操作
    C语言文件基本操作
    二叉树模板
    单源最短路——Dijkstra算法
  • 原文地址:https://www.cnblogs.com/yintoki/p/5708056.html
Copyright © 2011-2022 走看看