zoukankan      html  css  js  c++  java
  • D

    题意:有一些村庄需要修一些道路是所有村庄都可以连接,不过有些道路已经修好了,问题最少还需要修建的道路长度是多少。
    输入的第一行是一个N代表N个村庄,下面是一个N*N的矩阵,代表着q->j的距离,然后输出一个Q,接着有Q行,表示AB已经修建的村庄

    分析:为了增加麻烦他们设定了一些已经修建的村庄,不过可以使用krusal做,把已经修建的边都连接上,这些麻烦也仅仅如此。。。

    ************************************************************************
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<math.h>
    #include<vector>
    using namespace std;

    #define maxn 105

    int f[maxn];
    struct node
    {
        int u, v, len;
        friend bool operator < (node a, node b)
        {
            return a.len > b.len;
        }
    };
    int Find(int x)
    {
        if(f[x] != x)
            f[x] = Find(f[x]);
        return f[x];
    }

    int main()
    {
        int N;

        while(scanf("%d", &N) != EOF)
        {
            int M, u, v;
            node s;
            priority_queue<node> Q;

            for(s.u=1; s.u<=N; s.u++)
            {
                f[s.u] = s.u;
                for(s.v=1; s.v<=N; s.v++)
                {
                    scanf("%d", &s.len);
                    Q.push(s);
                }
            }

            scanf("%d", &M);

            while(M--)
            {
                scanf("%d%d", &u, &v);
                u = Find(u), v = Find(v);
                f[u] = v;
            }

            int ans = 0;

            while(Q.size())
            {
                s = Q.top();Q.pop();
                u = Find(s.u), v = Find(s.v);

                if(u != v)
                {
                    f[u] = v;
                    ans += s.len;
                }
            }

            printf("%d ", ans);
        }

        return 0;
    }
  • 相关阅读:
    希望jQuery操作DOM文档,则必须确保DOM载入后开始执行
    Web全栈AngularJS
    Web全栈AngularJS
    Web全栈AngularJS
    KD-Tree
    KD-Tree
    KD-Tree
    KD-Tree
    如何提升自身实力
    如何提升自身实力
  • 原文地址:https://www.cnblogs.com/liuxin13/p/4674437.html
Copyright © 2011-2022 走看看