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 ...
随机推荐
- struts2官方 中文教程 系列三:使用struts2 标签 tag
避免被爬,先贴上本帖地址:struts2 官方系列教程一:使用struts2 标签 tag http://www.cnblogs.com/linghaoxinpian/p/6901316.html 本 ...
- P3365 改造二叉树
P3365 改造二叉树 链接 分析: 求出中序遍历后,然后使其变成上升子序列.过程:每个点减去坐标,然后nlogn求出最长不下降子序列,n-ans即答案. 做题时一直认为二叉树就是完全二叉树,然后一直 ...
- Spring Boot :Request请求处理流程
技术交流群:233513714
- 斐波那契数列(递归)&求100以内的素数
Java 5 添加了 java.util.Scanner 类,这是一个用于扫描输入文本的新的实用程序.它是以 前的 StringTokenizer 和 Matcher 类之间的某种结合.由于任何数据都 ...
- USACO Section2.3 Zero Sum 解题报告 【icedream61】
zerosum解题报告----------------------------------------------------------------------------------------- ...
- ADB连接手机遇到的问题:list of devices attached
今天工作时想尝试一下使用ADB无线连接手机,结果遇到了下面这样的问题,浪费了几十分钟的时间,挺闹心的,因此想分享出来... 首先 第一步:使用USB数据线连接手机,手机弹出选项时,选择仅充电,然后wi ...
- Python编码、流程控制、格式化输出
Python编码 初始编码: 电脑的传输,还有储存,实际上都是010101010 ASCII码: (American Standard Code for Information Interchange ...
- python-压缩解压
压缩解压包 #导入模块 import zipfile #新建压缩包并将db与ooo.xml压缩到文件中 z = zipfile.ZipFile('laxi.zip','w') z.write('db' ...
- NGUI-使用UILabel呈现图片和不同格式的文字
1.可以使用BBCode标记 [b]Bold[/b] 粗体[i]italic[/i] 斜体[u]underli ...
- Action参数和View、Json、重定向
一.Action 1.Action参数: 普通参数.Model类.FormCollection (1).普通参数 Index(string name,int age) 框架会自动把用户请求的Que ...