HDU2122 Ice_cream’s world III【Kruskal】

Ice_cream’s world III

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 997    Accepted Submission(s): 321

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

HDU 2007-10 Programming Contest_WarmUp

题目大意:给你N个点(编号为0~N-1),M条路,问最小生成树是多少,如果不能生成最

小生成树,则输出impossible

思路:用Kruskal来做,如果最后得不到N-1条路,就输出impossible,否则就输出结果。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

const int MAXN = 1100;
const int MAXM = 10010;
int N,M,father[MAXN];

int find(int x)
{
    if(x != father[x])
        father[x] = find(father[x]);
    return father[x];
}
struct EdgeNode
{
    int from;
    int to;
    int w;
}Edges[MAXM];

int cmp(EdgeNode a, EdgeNode b)
{
    return a.w < b.w;
}
void Kruskal()
{
    int ans = 0;
    int Count = 0;
    for(int i = 0; i < M; ++i)
    {
        int u = find(Edges[i].from);
        int v = find(Edges[i].to);
        if(u != v)
        {
            ans += Edges[i].w;
            father[v] = u;
            Count++;
            if(Count == N-1)
                break;
        }
    }
    if(Count == N-1)
        printf("%d\n\n",ans);
    else
        printf("impossible\n\n");
}
int main()
{
    while(~scanf("%d%d",&N,&M))
    {
        memset(Edges,0,sizeof(Edges));
        for(int i = 0; i <= N; ++i)
            father[i] = i;
        for(int i = 0; i < M; ++i)
        {
            scanf("%d%d%d",&Edges[i].from,&Edges[i].to,&Edges[i].w);
        }
        sort(Edges,Edges+M,cmp);
        Kruskal();
    }

    return 0;
}
时间: 2024-08-27 05:25:11

HDU2122 Ice_cream’s world III【Kruskal】的相关文章

HDU2122 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): 802    Accepted Submission(s): 274 Problem Description ice_cream's world becomes stronger and stronger; every road is built

hdoj 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): 1237    Accepted Submission(s): 408 Problem Description ice_cream's world becomes stronger and stronger; every road is buil

hdoj 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): 1254    Accepted Submission(s): 414 Problem Description ice_cream's world becomes stronger and stronger; every road is buil

HDU 2122 Ice_cream’s world III【最小生成树】

解题思路:基础的最小生成树反思:不明白为什么i从1开始取,就一直WA,难道是因为村庄的编号是从0开始的吗 Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1032    Accepted Submission(s): 335 Problem Description ice_cream’s wo

hdoj 1863 畅通工程 【最小生成树】+【kruskal】

题意:... 难点:如何判断是不是信息不全:在输入的时候建立并查集,之后判断有几个节点就可以了,剩下的就是kruskal算法. 代码: #include<stdio.h> #include<string.h> #include<algorithm> #define MAXN 105 #define INF 0x3f3f3f3f using std::sort; struct node{ int from; int to; int w; }edges[MAXN*MAXN]

557. Reverse Words in a String III【easy】

557. Reverse Words in a String III[easy] Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output:

Ice_cream’s world III//最小生成树kruskal

题目: Ice_cream's world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2566    Accepted Submission(s): 904 Problem Description ice_cream's world becomes stronger and stronger; every road is b

POJ2377 Bad Cowtractors【Kruskal】【求最大生成树】

Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10933 Accepted: 4614 Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ

POJ2395 Out of Hay【Kruskal】

Out of Hay Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11656Accepted: 4562 Description The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situat