hdu 畅通工程系列题目
题目链接: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 畅通工程系列题目的更多相关文章
- HDU 畅通工程系列
畅通工程系列都是比较裸的最小生成树问题,且是中文题目,不赘述了. 1.HDU 1863 畅通工程 题意:一个省有很多村庄,其中一些之间是可以建公路的,每条公路都需要不同的代价,问代价最小的情况下将所有 ...
- hdu畅通工程(并查集)
Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道 ...
- hdu畅通工程
传送门 畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- hdu 畅通工程续
算法:多源最短路(floyd) 题意:有多个城镇,有些之间有通路,给你起点和终点,输出最短路径: Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路 ...
- hdu 畅通工程再续
http://acm.hdu.edu.cn/showproblem.php?pid=1875 #include <cstdio> #include <cstring> #inc ...
- hdu 畅通工程
http://acm.hdu.edu.cn/showproblem.php?pid=1863 #include <cstdio> #include <cstring> #inc ...
- hdu 1875 畅通工程再续(prim方法求得最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875 /************************************************* ...
- hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...
- HDU 1875 畅通工程再续 (最小生成树)
畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...
随机推荐
- android MotionEvent
getAction() 获取事件的类型,这是一个组合值,由pointer的index值和事件类型值组合而成的 getActionMasked() 获取事件的类型,不具有其他信息 参考: http:// ...
- 剑指Offer - 九度1387 - 斐波那契数列
剑指Offer - 九度1387 - 斐波那契数列2013-11-24 03:08 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项.斐波那契数列的定义如下: ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目4
2014-04-27 20:06 题目:设计一个类,只有在不产生死锁的时候才分配资源. 解法:不太清楚这个题是要分配何种资源,以何种形式?所以没能动手写个可运行的代码,只是闲扯了几句理论分析. 代码: ...
- WPF and Silverlight.ComboBox 如何通过 Binding IsDropDownOpen 实现下拉菜单展开
In the WPF example the Popup and the ToggleButton (the arrow on the right) are bound with the proper ...
- 【APUE】Chapter7 Process Environment
这一章内容是Process的基础准备篇章.这一章的内容都是基于C Programm为例子. (一)进程开始: kernel → C start-up rountine → main function ...
- Python IO关于mode参数的问题
关于open()的mode参数: 'r':读 'w':写 'a':追加 'r+' == r+w(可读可写,文件若不存在就报错(IOError)) 'w+' == w+r(可读可写,文件若不存在就创建) ...
- Python 3基础教程14-在文件尾部更新内容
本文介绍在一个已经存在的文件尾部添加内容,还是用到write方法. 这里exampleFile.txt是前面文件创建的文件,里面有两行文字.
- Java系列学习说明
最近要回顾以前的开发知识了,每天一个案例,争取早日成为一名合格的程序猿,现在就是猴娃子级别的.
- gcc 编译器常用的命令行参数一览
这些常用的 gcc/g++ 命令行参数,你都知道么? 1. gcc -E source_file.c -E,只执行到预编译.直接输出预编译结果. 2. gcc -S source_file.c -S, ...
- div样式
DIV样式汇总 一.常用属性: 1.Height:设置DIV的高度. 2.Width:设置DIV的宽度. 例: <div style="width:200px;height:200px ...