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;
    }
  • 相关阅读:
    HGOI 20200724
    HGOI 20200722
    [USACO Open08]牛的邻居Cow Neighborhoods解题报告
    [USACO Jan07]考试Schul解题报告
    [CF 249D]Donkey and Start解题报告
    [CF 321D]Ciel and Flipboard解题报告
    [CF 294D]Shaass and Painter Robot解题报告
    [CF 297E]Mystic Carvings解题报告
    [CF 306E]Levko and Game题解翻译
    [CF 316F3]Suns and Rays解题报告
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3376849.html
Copyright © 2011-2022 走看看