zoukankan      html  css  js  c++  java
  • HDU 2122 Ice_cream’s world III

    Ice_cream’s world III

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 575    Accepted Submission(s): 184

    Problem Description
    ice_cream’s world becomes stronger and stronger; every road is built as undirected. The queen enjoys traveling around her world; the queen’s requirement is like II problem, beautifies the roads, by which there are some ways from every city to the capital. The project’s cost should be as less as better.
     
    Input
    Every case have two integers N and M (N<=1000, M<=10000) meaning N cities and M roads, the cities numbered 0…N-1, following N lines, each line contain three integers S, T and C, meaning S connected with T have a road will cost C.
     
    Output
    If Wiskey can’t satisfy the queen’s requirement, you must be output “impossible”, otherwise, print the minimum cost in this project. After every case print one blank.
     
    Sample Input
    2 1
    0 1 10
    4 0
     
    Sample Output
    10
    impossible
     
    Author
    Wiskey
     
    Source
     
    Recommend
    威士忌
     
    思路:Kruskal
     
    代码:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    const int MAXN = 10060;
    int map[1100];
    int n,m;
    int the_last_flag;
    int a,b,how_long;
    struct Node
    {
        int first,end;
        int value;
        int select;
    }Edge[MAXN];
    int find(int x)
    {
        while(x != map[x])
        {
            x = map[x];
        }
        return x;
    }
    void Merge(int x,int y)
    {
        int p = find(x);
        int q = find(y);
        if(p < q)
           map[q] = p;
        else
           map[p] = q;
    }
    bool cmp(Node a,Node b)
    {
        if(a.value != b.value)
           return a.value < b.value;
        if(a.first != b.first)
           return a.first < b.first;
        return a.end < b.end;
    }
    void Kruskal(Node *Edge,int n,int m)
    {
        the_last_flag = 0;
        sort(Edge + 1,Edge + m + 1,cmp);
        for(int i = 1;i <= m;i ++)
        {
            if(the_last_flag == n - 1)
               break ;
            int x = find(Edge[i].first);
            int y = find(Edge[i].end);
            if(x != y)
            {
                Merge(x,y);
                Edge[i].select = true;
                the_last_flag ++;
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            for(int i = 1;i <= n;i ++)
               map[i] = i;
            memset(Edge,0,sizeof(Edge));
            for(int i = 1;i <= m;i ++)
            {
                scanf("%d%d%d",&a,&b,&how_long);
                a ++;b ++;
                Edge[i].first = a;
                Edge[i].end = b;
                Edge[i].value = how_long;
            }
            Kruskal(Edge,n,m);
            if(the_last_flag != n - 1)
               printf("impossible
    ");
            else
            {
                int the_last_sum = 0;
                for(int i = 1;i <= m;i ++)
                {
                    if(Edge[i].select == 1)
                       the_last_sum += Edge[i].value;
                }
                printf("%d
    ",the_last_sum);
            }
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    列举ASP.NET 页面之间传递值的几种方式
    SQL Server 2010附加数据库失败:无法打开物理文件拒绝访问解决方法
    运行出现错误是:未能加载文件或程序集 CrystalDecisions.Web Version=10.2.3600,找不到它的依赖项,并且问题出现在web.config里面
    快捷键
    VS中出现无法嵌入互操作类型
    Java模拟试题(2015-9-2)
    IDEA 的配置文件导入命名空间
    第四章、程序计数器
    IDEA建Mappper.xml
    第三章、运行时数据区概述及线程
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3376849.html
Copyright © 2011-2022 走看看