HDU1875(最小生成树)
畅通工程再续
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21693 Accepted Submission(s): 6856
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int MAXN=;
const double INF=1.0e9;
typedef pair<double,int> P;
struct Point{
int x,y;
}ps[MAXN];
int V;
vector<int> G[MAXN];
double mp[MAXN][MAXN];
int par[MAXN];
int rnk[MAXN];
double dist(int x1,int y1,int x2,int y2)
{
return sqrt(double((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
} void prep(int n)
{
for(int i=;i<=n;i++)
{
par[i]=i;
rnk[i]=;
}
} int fnd(int x)
{
if(par[x]==x)
return x;
return par[x]=fnd(par[x]);
} void unite(int x,int y)
{
int a=fnd(x);
int b=fnd(y);
if(a==b)
{
return ;
}
if(rnk[a]<rnk[b])
{
par[a]=b;
}
else
{
par[b]=a;
if(rnk[a]==rnk[b]) rnk[a]++;
}
} bool judge(int n)
{
int f=fnd();
for(int i=;i<=n;i++)
if(f!=par[i]) return false;
return true;
} double d[MAXN];
int vis[MAXN];
double prim(int s)
{
double ans=;
for(int i=;i<=V;i++)
{
d[i]=INF;
vis[i]=;
}
d[s]=;
priority_queue<P,vector<P>,greater<P> > que;
que.push(P(,s)); while(!que.empty())
{
P now=que.top();que.pop();
int v=now.second;
if(vis[v]) continue;
ans+=now.first;
vis[v]=;
for(int i=;i<G[v].size();i++)
{
int to=G[v][i];
if(d[to]>mp[v][to])
{
d[to]=mp[v][to];
que.push(P(d[to],to));
}
}
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&V);
prep(V);
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
if(i==j) mp[i][j]=;
else mp[i][j]=INF;
for(int i=;i<=V;i++)
{
scanf("%d%d",&ps[i].x,&ps[i].y);
}
for(int i=;i<=V;i++)
for(int j=i+;j<=V;j++)
{
double co=dist(ps[i].x,ps[i].y,ps[j].x,ps[j].y);
if(co<||co>) continue;
mp[i][j]=mp[j][i]=co;
G[i].push_back(j);
G[j].push_back(i);
unite(i,j);
}
if(judge(V))
{
double ans=prim();
printf("%0.1lf\n",ans*);
}
else
{
printf("oh!\n");
}
} return ;
}
HDU1875(最小生成树)的更多相关文章
- HDU1875——畅通工程再续(最小生成树:Kruskal算法)
畅通工程再续 Description相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当 ...
- hdu1875(最小生成树prime)
思路:一开始想用贪心来着,发现贪心有缺陷,然后就用了最小生成树来写,这里用了prime算法,首先,先建个图,两点之间的边的权值就是两个点的距离,然后直接prime模板 代码 #include<i ...
- HDU1875 畅通工程再续【最小生成树】
题意: 在这些小岛中建设最小花费的桥,但是一座桥的距离必须在10 -- 1000之间. 思路: 用最小生成树解决吧,就那两个算法. 代码: prim #include <iostream> ...
- hdu1875 畅通工程再续 并查集/最小生成树
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...
- HDU-1875 畅通工程再续(最小生成树+判断是否存在)
http://acm.hdu.edu.cn/showproblem.php?pid=1875 Problem Description 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛 ...
- hdu1875 畅通工程再续 最小生成树并查集解决---kruskal
http://acm.hdu.edu.cn/showproblem.php?pid=1875 New~ 欢迎“热爱编程”的高考少年——报考杭州电子科技大学计算机学院关于2015年杭电ACM暑期集训队的 ...
- hdu1875 畅通工程再续 暴力+基础最小生成树
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; ; ; ; in ...
- HDU1875+Prim模板
https://cn.vjudge.net/problem/HDU-1875 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府 ...
- 最小生成树(Kruskal算法-边集数组)
以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...
随机推荐
- XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' ' is therefore not allowed access.
ajax跨域 禁止访问! 利用Access-Control-Allow-Origin响应头解决跨域请求
- linux history 命令 禁用history
保存在.bash_history文件中,默认1000条,你也可以更改这个 值 !!:上一个指令 !number 运行第几个指令 查看命令历史的时间戳,那么可以执行: # export HISTTIME ...
- 【文献阅读】Augmenting Supervised Neural Networks with Unsupervised Objectives-ICML-2016
一.Abstract 从近期对unsupervised learning 的研究得到启发,在large-scale setting 上,本文把unsupervised learning 与superv ...
- Altera Quartus 13.1 仿真工具路径错误问题解决 Can't launch the ModelSim-Altera software
Altera Quartus 13.1 仿真工具路径错误问题解决 Quartus13.1 自带的ModelSim-Altera 10.1d 版本, 在做仿真时调用 ModelSim-Alteara,发 ...
- 九度OJ 1066:字符串排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5632 解决:2299 题目描述: 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出排序后的 ...
- cocos2d-js添加百度MSSP插屏(通过jsb反射机制)
1.导入jar包.... 2.修改AndroidManifest.xml文件 添加: <meta-data android:name="BaiduMobAd_APP_ID" ...
- 流畅python学习笔记第十八章:使用asyncio包处理并发(二)
前面介绍了asyncio的用法.下面我们来看下如何用协程的方式来实现之前的旋转指针的方法 @asyncio.coroutine def spin(msg): write,flush=sys.stdou ...
- Java for LeetCode 113 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- Android系统文件目录
- empty blank
非nil对象才能调用 empty nil: 对象是否存在empty: ”“ []blank: nil emptypresent: ! blank