题意:有一些村庄需要修一些道路是所有村庄都可以连接,不过有些道路已经修好了,问题最少还需要修建的道路长度是多少。
输入的第一行是一个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. 新一代 PHP 加速插件 Zend Opcache <转>

    注: 由于原链接已不存在, 所以我把图片重新整理了一下, 以便看起来更加直观 笔者注: 1>  PHP 性能提升之 PHP NG  =>  php next generation wiki ...

  2. Android开发--WIFI实现

    wifi的基本结构 在Android的官方文档中定义了如下五种状态: WIFI_STATE_DISABLING  WIFI网卡正在关闭  0 WIFI_STATE_DISABLED   WIFI网卡不 ...

  3. Rechability的简单使用

    AppDelegate.m #import "AppDelegate.h" #import "Reachability.h" @interface AppDel ...

  4. java常识和好玩的注释

    如字符串使用strXXXboolean使用isXXX,hasXXX Vector vProducts= new Vector(); Array aryUsers= new Array(); 类与接口基 ...

  5. iOS开发,新手入门指导

    在做了近两年wp,安卓开发之后,某一天突然决定投身iOS的开发之中. 因为一直用的mac,做wp开发的时候都用双系统,vs开久了,就会比较烫,这点让人不爽.后来更多地做安卓,直接mac下开发,很舒适的 ...

  6. canvas 背景填充

    这儿介绍canvas的ccreatePattern函数, context.createPattern(Image,"repeat"),还可以repeat-x,reapter-y 还 ...

  7. HTML5拖放

    HTML5拖放 <!doctype html> <html> <head> <meta charset="UTF-8"> <t ...

  8. ThinkPHP 使用极光推送给ios推送消息

    HTML <div id="wrap"><a href="<{:U('Push/pushData')}>">推送</a ...

  9. 给AVS添加描述(how to add a description to a video)

    UPDATE you might need edit few files. 1. add the input field to the tpl file: /templates/frontend/yo ...

  10. python之7-2类的继承与多态

    类的继承的意思就如同父子关系一样,这个儿子继承了父亲的一切,但是在某些地方(属性)相同的时候,儿子的属性大于老子的属性(覆盖),最底层类,总会继承最接近它的那个类的属性init 类的多态总是和继承相连 ...