hdu 畅通工程系列题目
2024-09-06 02:44:27
题目链接: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));
}
}
最新文章
- Django 中 如何使用 settings.py 中的常量
- Linux 启动过程分析
- 第三百二十七天 how can I 坚持
- 新手常见的python报错及解决方案
- MyBatis3入门
- 关于 Integer 值比较的问题
- Chrome浏览器中autocomplete=";off";不起作用解决方案
- Mac 端配置 Lua 环境
- 获取邮箱的DNS和MX 工具类
- AWR报告学习示例
- BTrace学习总结
- [Coderforces600E] Lomsat gelral
- Android P 功能和 API
- Changing the Language Used in ODI Studio
- Oracle两个数据库互相访问,DBLink使用-转
- VAE demo
- 鼠标经过的图片高亮显示,其余变暗效果[xyytit]
- poj 1849 Two 树形dp
- MongoDB查询用法大全
- 爬取千万淘宝商品的python脚本
热门文章
- Java语言基础---变量与数据类型
- PowerCmd
- 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(二)
- centos 服务器内存管理 服务于端口状态
- C# 6.0/7.0 的新特性
- 《Cracking the Coding Interview》——第5章:位操作——题目7
- Scrapy使用示例
- ASP.NET Core 2.1 源码学习之 Options[2]:IOptions 【转】
- Java Web Action DAO Service层次理解
- jQuery Ajax(load,post,get,ajax)