题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232

并查集水。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
int n,m;
int father[N]; int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
} int main(){
while(scanf("%d",&n)!=EOF,n){
for(int i=;i<=n;i++){
father[i] = i;
}
scanf("%d",&m);
for(int i=;i<=m;i++){
int a,b;
scanf("%d%d",&a,&b);
int x = _find(a);
int y = _find(b);
father[x] = y;
}
int ans = ;
for(int i=;i<=n;i++){
if(father[i]==i) ans++;
}
printf("%d\n",ans-);
}
}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233

kruskal水

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
int n,m;
int father[N];
struct Edge{
int s,e,len;
}edge[N*(N-)/];
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
int kruskal(int m){
sort(edge+,edge+m+,cmp);
int cost = ;
for(int i=;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost+=edge[i].len;
}
}
return cost;
}
int main(){
while(scanf("%d",&n)!=EOF,n){
for(int i=;i<=n;i++){
father[i] = i;
}
int m = n*(n-)/;
for(int i=;i<=m;i++){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].len);
}
printf("%d\n",kruskal(m));
}
}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863

kruskal水+判断强连通分量

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int M = ;
const int N = M*(M-)/;
struct Edge{
int s,e,len;
}edge[N];
int father[M],n,m;
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
int kruskal(int m){
sort(edge+,edge+m+,cmp);
int cost = ;
for(int i=;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost += edge[i].len;
}
}
return cost;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF,n){
for(int i=;i<=m;i++) father[i] = i;
for(int i=;i<=n;i++){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].len);
}
int cost = kruskal(n);
int ans = ;
for(int i=;i<=m;i++){
if(father[i]==i) ans++;
}
if(ans==) printf("%d\n",cost);
else printf("?\n");
}
}

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1875

将平面上的点都连接起来求最小生成树最后再判断一下强连通分量。

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const double eps = 1e-;
struct Point{
int x,y;
}p[N];
struct Edge{
int s,e;
double len;
}edge[N*(N-)/];
int father[N];
int n;
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool check(double k){
if(k+eps<) return false;
if(k-eps>) return false;
return true;
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
double kruskal(int m){
sort(edge+,edge+m+,cmp);
double cost = ;
for(int i=;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost +=edge[i].len;;
}
}
return cost;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=;i<n;i++) father[i] = i;
for(int i=;i<n;i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
int m =;
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
double len = dis(p[i],p[j]);
if(!check(len)) continue;
edge[m].s = i;
edge[m].e = j;
edge[m++].len = dis(p[i],p[j]);
}
}
m--;
double cost = kruskal(m);
int ans = ;
for(int i=;i<n;i++){
if(father[i]==i) ans++;
}
if(ans==) printf("%.1lf\n",cost*);
else printf("oh!\n");
}
}

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1879

处理一下输入,kruskal水

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
struct Edge{
int s,e,len;
}edge[N*(N-)/];
int n,m;
int father[N];
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
int kruskal(int m){
sort(edge+,edge+m+,cmp);
int cost = ;
for(int i=;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost+=edge[i].len;
}
}
return cost;
}
int main(){
while(scanf("%d",&n)!=EOF,n){
for(int i=;i<=n;i++) father[i] = i;
int m = n*(n-)/;
for(int i=;i<=m;i++){
int c,d;
scanf("%d%d%d%d",&edge[i].s,&edge[i].e,&c,&d);
if(!d) edge[i].len=c;
else edge[i].len =;
}
printf("%d\n",kruskal(m));
}
}

hdu 畅通工程系列题目的更多相关文章

  1. HDU 畅通工程系列

    畅通工程系列都是比较裸的最小生成树问题,且是中文题目,不赘述了. 1.HDU 1863 畅通工程 题意:一个省有很多村庄,其中一些之间是可以建公路的,每条公路都需要不同的代价,问代价最小的情况下将所有 ...

  2. hdu畅通工程(并查集)

    Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道 ...

  3. hdu畅通工程

    传送门 畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. hdu 畅通工程续

    算法:多源最短路(floyd) 题意:有多个城镇,有些之间有通路,给你起点和终点,输出最短路径: Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路 ...

  5. hdu 畅通工程再续

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

  6. hdu 畅通工程

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

  7. hdu 1875 畅通工程再续(prim方法求得最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875 /************************************************* ...

  8. hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...

  9. HDU 1875 畅通工程再续 (最小生成树)

    畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...

随机推荐

  1. shell脚本获取网页快照并生成缩略图

    获取网页快照并生成缩略图可分两步进行: 1.获取网页快照 2.生成缩略图 获取网页快照 这里我们用 phantomjs 来实现.关于 phantomjs 的详细用法可参考官方网站. 1.安装 我的环境 ...

  2. js对象使用

    以下是js对象使用的两种方式 <script type="text/javascript"> var people = function () { } //方法1 pe ...

  3. 转载——一步步学习js

    一步步学习javascript基础篇(0):开篇索引 阅读目录 索引: 一步步学习javascript基础篇(1):基本概念 一步步学习javascript基础篇(2):作用域和作用域链 一步步学习j ...

  4. 每天一个Linux命令(10):mv命令

    mv命令用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中.source表示源文件或目录,target表示目标文件或目录.如果将一个文件移到一个已经存在的目标文件中,则目标文件的内容将被覆 ...

  5. python基础实践(五)

    # -*- coding:utf-8 -*-# Author:sweeping-monk# -*-操作列表-*-Traverse_the_list = ['guanfu','xiaole','fang ...

  6. 孤荷凌寒自学python第二十一天初识python的类

    孤荷凌寒自学python第二十一天初识python的类 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 类是面向对象的编程语言非常重要的概念. 编程语言的进化史中从顺序编程到结构化编程,最后才 ...

  7. Where can I find the IPA logs

    Retrieving the IPA logs will differ depending on which base image was used. Operating system that do ...

  8. 删除maven仓库中的lastUpdate文件

    使用idea时导入hibernate 5.1.0的jar包,然后发现本地仓库中找不到该版本的jar 然后手贱 alt+enter 发现提示 update maven indices 然后以为更新就会好 ...

  9. GridView与ListView冲突

    由于GridView与listView都是继承自ScrollView,所以两个控件放在一起时需要重写控件方法   public class MyGridView extends GridView{  ...

  10. thinkphp3.2 学习笔记 基础篇

    环境要求:PHP5.3以上版本注意:PHP5.3DEV和php6不支持 目录结构 www WEB部署目录(或者子目录)├─index.php 入口文件├─README.md README文件├─App ...