题意:有一些村庄需要修一些道路是所有村庄都可以连接,不过有些道路已经修好了,问题最少还需要修建的道路长度是多少。
输入的第一行是一个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\n", ans);
    }     return 0;
}

D - Constructing Roads - 2421的更多相关文章

  1. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  2. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/D Description There ar ...

  3. POJ - 2421 Constructing Roads 【最小生成树Kruscal】

    Constructing Roads Description There are N villages, which are numbered from 1 to N, and you should ...

  4. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...

  5. Constructing Roads——F

    F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...

  6. Constructing Roads In JGShining's Kingdom(HDU1025)(LCS序列的变行)

    Constructing Roads In JGShining's Kingdom  HDU1025 题目主要理解要用LCS进行求解! 并且一般的求法会超时!!要用二分!!! 最后蛋疼的是输出格式的注 ...

  7. [ACM] hdu 1025 Constructing Roads In JGShining's Kingdom (最长递增子序列,lower_bound使用)

    Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

  8. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. Constructing Roads (MST)

    Constructing Roads Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

随机推荐

  1. Python之路,Day13-----暂无正在更新中

    Python之路,Day13-----暂无正在更新中

  2. Android签名详解(debug和release)

    Android签名详解(debug和release)   1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...

  3. Mysql数据库一个小程序实现自动创建分表。

    每当跨月的时候也是系统出问题最多的时候,没有表和字段缺失是两个最常见的错误. 为了解决这个问题,研究了一下mysql的 information_schema 表: information_schema ...

  4. DedeCms autoindex和itemindex使用介绍

    autoindex/itemindex 可以使用 @me+1;实现由指定数字开始,下面为大家详细介绍下具体的两者具体的用法,感兴趣的朋友可以参考下 代码如下: <span style=" ...

  5. 关于svn获取获取文件时 Unable to connect to a repository at URL"https://..."执行上下文错误:参数错误

    错误提示: 下面的六种解决方案都未能解决: 1.不提示输入用户名和密码,不管重装多少次都一样. 2.TortoiseSVN的setting->Saved Data->Authenticat ...

  6. ASP.NET C#使用JavaScriptSerializer实现序列化与反序列化得到JSON

    在JavaScriptSerializer中,我们可以看到下面可以使用的方法或者构造函数,它们都是实例方法: Member Description JavaScriptSerializer() 构造函 ...

  7. Android开发手记(19) 数据存储四 ContentProvider

    转载自:http://www.cnblogs.com/devinzhang/archive/2012/01/20/2327863.html Android为数据存储提供了五种方式: 1.SharedP ...

  8. android编译系统学习

    近日接手了后续android新平台项目搭建的任务. 本文内容基于sprd公司提供的android5.1源码. 一.一般的编译工作流程 我们代码一般情况下是从芯片商SPRD/MTK获得的. 源码的编译上 ...

  9. Linux特殊权限

    ************************     ACL权限       ****************************************** acl权限的出现是为了弥补用户权 ...

  10. 【转】 Xcode基本操作

    原文: http://blog.csdn.net/phunxm/article/details/17044337 1.IDE概览 Gutter & Ribbon 焦点列:灰色深度与代码嵌套深度 ...