题目链接>>>

题目大意:

给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费。

解题分析:

最小生成树模板题,下面用的是kruscal算法。

//Kruscal算法采用的是"加边"的想法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
];
int n, m, k;

struct line
{
    int x,y,c;
}edge[];

bool cmp(line a, line b)
{
    return a.c < b.c;
}

int find(int x)
{
    int r = x;
    while (father[r] != r)r = father[r];
    int i = x, j;
    while (father[i] != r)
    {
        j = father[i];
        father[i] = r;
        i = j;
    }
    return r;
}

void kruskal()
{
    int i, j;
    ;
    sort(edge, edge + k, cmp);
    ; i <= k; i++)        //按从小到大的顺序将所有不会构成环的边加入当前边集
    {
        int f1 = find(edge[i].x);
        int f2 = find(edge[i].y);
        if (f1 != f2)
        {
            father[f2] = f1;
            sum += edge[i].c;
        }
    }
    printf("%lld\n", sum);
}

int main()
{
    ,a2;
    char str1, str2;
    ,n)
    {
        k = ;
        memset(edge, , sizeof(edge));                //记得每次都要清空bian数组
        ; i < n; i++)
        {
            getchar();
            scanf("%c%d", &str1, &m);
            ; j <= m; j++)
            {
                getchar();
                scanf("%c%d", &str2, &a2);
                edge[k].x = str1 - ; //第一个村庄的编号从1开始
                edge[k].y = str2 - ;
                edge[k].c = a2;
                k++;
            }
        }
        ; i <= n; i++)       //初始化father数组
        {
            father[i] = i;
        }

        kruskal();
        a1++;
    }
    ;
}

2018-04-01

HDU 1301-Jungle Roads【Kruscal】模板题的更多相关文章

  1. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  2. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  3. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  4. hdu 1301 Jungle Roads krusckal,最小生成树,并查集

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  5. hdu 1301 Jungle Roads

    http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...

  6. Hdu 1301 Jungle Roads (最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...

  7. POJ 1251 & HDU 1301 Jungle Roads

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  8. POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】

    题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解  Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...

  9. 最小生成树 || HDU 1301 Jungle Roads

    裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...

  10. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

随机推荐

  1. 处理:“ORA-00257: archiver error. Connect internal only, until freed”的错误问题

    注:本文参考了< ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法  > 一:问题背景: 今天在 ...

  2. Confluence 6 配置 MySQL 服务器

    在这一步,你将要配置你的 MySQL 数据库服务器. 注意: 如果你尝试连接你的 Confluence 到一个已经存在的 MySQL 数据库服务器.我们强烈建议你按照下面描述的安装步骤在 MySQL ...

  3. ionic3 使用html2canvas将数据导出为图片,并下载本地

    1.安装html2canvas npm install --save html2canvas 官方网站 https://html2canvas.hertzen.com/ 2.在需要的组件中引入html ...

  4. tp3.2 事务

    public function exchangeTransfer($user_id, $type, $money, $config, $other_id = 0) { $r['code'] = ERR ...

  5. Nginx详解二十八:Nginx架构篇Nginx+Lua的安全waf防火墙

    Nginx+Lua的安全waf防火墙 看一下别人写好的:https://github.com/loveshell/ngx_lua_waf 先安装git:yum -y install git 在/opt ...

  6. 性能测试五十:Jmeter+Influxdb+Grafana实时数据展示系统搭建

    如果用生成jtl文件再分析结果的方式的话,每一次请求就会往jtl里面写一条数据,在进行长时间的稳定性测试的时候,特别是当TPS很高的时候,写入的数据会非常的大,这个时候等稳定性测试完成,再对jtl进行 ...

  7. Allegro PCB Design GXL (legacy) 由零散的对象构成一个Shape

    Allegro PCB Design GXL (legacy) version 16.6-2015 从DXF文件中导入板框之后,发现板框是由Line Segment.Arc Segment等对象组成, ...

  8. js中的“==”和“===”的区别

    简单来说: == 代表相同, ===代表严格相同, 为啥这么说呢, 这么理解: 当进行双等号比较时候: 先检查两个操作数数据类型,如果相同, 则进行===比较, 如果不同, 则愿意为你进行一次类型转换 ...

  9. appium如何解决每次都要安装apk的烦恼

    1.appium上勾选 No Reset 2.程序加上:capabilities.setCapability("noReset", true);   //不需要再次安装 3.命令行 ...

  10. IDEA窗口重置