题目链接: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. 使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码 (jvm性能调优)

    技术交流群:233513714 本文是<JVM 性能调优实战之:一次系统性能瓶颈的寻找过程> 的后续篇,该篇介绍了如何使用 JDK 自身提供的工具进行 JVM 调优将 TPS 由 2.5 ...

  2. ionic2升级到ionic3并打包APK

    通过IONIC2升级到3的时候,经过我一系列的测试,以及网上各种办法,现将新测有效的方法记录如下,本人按如下方法,对多个项目升级后,都能正常打包成APK IONIC 2到3的升级: 1.拷贝ionic ...

  3. 剑指Offer - 九度1519 - 合并两个排序的链表

    剑指Offer - 九度1519 - 合并两个排序的链表2013-11-30 22:04 题目描述: 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则.(hi ...

  4. Django笔记 —— 视图

    最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...

  5. 【Lowest Common Ancestor of a Binary Tree】cpp

    题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...

  6. js学习日记-各种宽高总结(配图)

    1.窗口和浏览器 window.innerWidth.window.innerHeight   浏览器内部可用宽高 window.outerWidth.window.outerHeight   浏览器 ...

  7. mysql错误:Column ‘id’ in field list is ambiguous的解决方法

    [Err] 1052 - Column 'modify_time' in where clause is ambiguous 出错的语句: SELECT AVG(T.se)%60FROM( SELEC ...

  8. SpringMVC 集成 Freemarker 模板引擎

    本文通过 maven 项目中集成 1.引入 SpringMVC 与 Freemarker 需要的依赖 <!-- SpringMVC --> <dependency> <g ...

  9. Windows7中如何让python2和python3共存并使用pip

    1.下载安装python2和python3 分别下载python2.7.exe.python3.6.exe并安装到C盘.E盘(如图)     2.配置环境变量 打开“系统变量”中的path文本框(如图 ...

  10. sqlalchemy 查询姿势总结

    sqlalchemy查询使用 1.带条件查询 查询是最常用的,对于各种查询我们必须要十分清楚,首先是带条件的查询 #带条件查询 rows = session.query(User).filter_by ...