畅通project





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

Total Submission(s): 16075    Accepted Submission(s): 6677

Problem Description
省政府“畅通project”的目标是使全省不论什么两个村庄间都能够实现公路交通(但不一定有直接的公路相连,仅仅要能间接通过公路可达就可以)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编敲代码,计算出全省畅通须要的最低成本。
 
Input
測试输入包括若干測试用例。每一个測试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N


行相应村庄间道路的成本,每行给出一对正整数,各自是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,所有输入结束,相应的结果不要输出。
 
Output
对每一个測试用例,在1行里输出全省畅通须要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 
Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
 
Sample Output
3
?

这是一道带权值的并查集,由于是中文题, 题意也就不用多说了。。

所以我是用结构体写的。。。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<sstream>
#include<cmath> using namespace std; #define f1(i, n) for(int i=0; i<n; i++)
#define f2(i, n) for(int i=1; i<=n; i++)
#define f3(i, n) for(int i=n; i>=1; i--)
#define f4(i, n) for(int i=1; i<n; i++)
#define M 10050 int f[M];
int r[M];
int ans;
int t;
int n, m;
int coun; struct node
{
int x;
int y;
int cost; //花费
}q[M]; int cmp(node x1, node y1)
{
return x1.cost < y1.cost;
} int find(int x) //并查集的find
{
return f[x] == x ? x:f[x] = find( f[x] );
} void Kruskal()
{
sort(q, q+n, cmp);
f2(i, n)
{
int xx = find(q[i].x);
int yy = find(q[i].y);
if( xx!=yy ) //当不是统一集合时。。
{
ans+=q[i].cost;
f[yy] = xx;
coun --; //连起来一条路
}
}
} int main()
{
while(scanf("%d%d", &n, &m) &&n)
{
ans = 0;
t = 0;
coun = m;
f2(i, m) f[i] = i; //初始化
f2(i, n)
scanf("%d%d%d", &q[i].x, &q[i].y, &q[i].cost);
Kruskal();
if(coun==1)
printf("%d\n", ans);
else
printf("?\n");
} return 0;
}

HDU 1863:畅通project(带权值的并查集)的更多相关文章

  1. 洛谷 P2024 [NOI2001]食物链——带权值的并查集维护

    先上一波题目 https://www.luogu.org/problem/P2024 通过这道题复习了一波并查集,学习了一波带权值操作 首先我们观察到 所有的环都是以A->B->C-> ...

  2. HDU 1863 畅通project (最小生成树是否存在)

    题意 中文 入门最小生成树  prim大法好 #include<cstdio> #include<cstring> using namespace std; const int ...

  3. hdu 1863 畅通project kruskal || prim

    简单最小生成树,畅通project.这三道题目都是练习最小生成树的. 注意一下推断是否有通路时,kruskal能够推断每一个点的祖先是否同样.prim能够推断每一个点是否都加进集合里面了,也就是说是否 ...

  4. HDU1863_畅通project【Prim】【并查集】

    畅通project Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. hdu 1863 畅通project

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  6. hdu 1829 A Bug's Life(分组并查集(偏移量))

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. 51nod1459(带权值的dijkstra)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...

  8. 带权值的LCA

    例题:http://poj.org/problem?id=1986 POJ1986 Distance Queries Language: Default Distance Queries Time L ...

  9. HDU 3081 Marriage Match II (二分图,并查集)

    HDU 3081 Marriage Match II (二分图,并查集) Description Presumably, you all have known the question of stab ...

随机推荐

  1. Web Api Session开启会话支持

        1.WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型           //代码如下 public override voi ...

  2. AVL树插入和删除

    一.AVL树简介 AVL树是一种平衡的二叉查找树. 平衡二叉树(AVL 树)是一棵空树,或者是具有下列性质的二叉排序树:    1它的左子树和右子树都是平衡二叉树,    2且左子树和右子树高度之差的 ...

  3. Css控制div水平垂直居中显示

    <style>#info{height:0px; width:0px;top:50%; left:50%;position:absolute;}#center{background:#FF ...

  4. 关于百度鹰眼中 xcode 7 编译报错问题

    请把 这个地方改为 YES 否则demo 不能运行

  5. iOS — Autolayout之Masonry解读

    前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...

  6. 练习—单链表—Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...

  7. C# 数据实现设计模式

    一个人没事,写了一个底层数据实现读取设计模式,个人觉得还是蛮好扩展,里面有不足的地方希望大家给予指导.话不多说先看个图吧!图可能不正规,伤害了你的眼睛见谅.有图有真相 其实这个设计模式,就是一个简单的 ...

  8. Altium Designer 生成 Mach3 G代码的程序

    Altium Designer做PCB设计,还是很方便的,最近头脑发热,在网上买了一套CNC机床,用来做钻孔用,但是翻来翻去,基本上所有的软件都是铣削功能,而且很多软件很复杂.翻了好几天,发现没有什么 ...

  9. python导入matplotlib模块出错

    我的系统是linux mint.用新立得软件包安装了numpy和matplotlib.在导入matplotlib.pyplot时出错.说是没有python3-tk包. 于是就在shell中装了一下. ...

  10. Linux下安装并启动MongDB

    1.下载MongoDB 下载链接: http://www.mongodb.org/downloads 2.解压下载的压缩包 tar -zxvf mongodb-linux-x86_64-3.2.8.t ...