HDU - 1875 畅通工程再续【最小生成树】
Problem Description
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
Sample Input
2 2 10 10 20 20 3 1 1 2 2 1000 1000
Sample Output
1414.2 oh!
最小生成树板子题吧,判断一下所有在条件范围内的边,然后跑一遍Kruskal 如果最后边数不是n-1就输出oh!
有空再补一下Prim的做法~~
- #include<iostream>
- #include<cstdio> //EOF,NULL
- #include<cstring> //memset
- #include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
- #include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
- #include<algorithm> //fill,reverse,next_permutation,__gcd,
- #include<string>
- #include<vector>
- #include<queue>
- #include<stack>
- #include<utility>
- #include<iterator>
- #include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
- #include<functional>
- #include<map>
- #include<set>
- #include<limits.h> //INT_MAX
- #include<bitset> // bitset<?> n
- using namespace std;
- #define rep(i,a,n) for(int i=a;i<n;i++)
- #define per(i,a,n) for(int i=n-1;i>=a;i--)
- #define fori(x) for(int i=0;i<x;i++)
- #define forj(x) for(int j=0;j<x;j++)
- #define memset(x,y) memset(x,y,sizeof(x))
- #define memcpy(x,y) memcpy(x,y,sizeof(y))
- #define all(x) x.begin(),x.end()
- #define readc(x) scanf("%c",&x)
- #define read(x) scanf("%d",&x)
- #define read2(x,y) scanf("%d%d",&x,&y)
- #define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
- #define print(x) printf("%d\n",x)
- #define lowbit(x) x&-x
- #define lson(x) x<<1
- #define rson(x) x<<1|1
- #define pb push_back
- #define mp make_pair
- typedef pair<int,int> P;
- typedef long long LL;
- typedef long long ll;
- const double eps=1e-;
- const double PI = acos(1.0);
- const int INF = 0x3f3f3f3f;
- const int inf = 0x3f3f3f3f;
- const int mod = 1e9+;
- const int MAXN = 1e6+;
- const int maxm = ;
- const int maxn = +;
- int T;
- int n;
- double x[maxn],y[maxn];
- int cnt,tot;
- double ans ;
- int pre[maxn];
- struct rood{
- int st,ed;
- double w;
- bool operator < (rood b) const{
- return w < b.w ;
- }
- }rod[maxn];
- struct node{
- double x,y;
- }pot[maxn];
- int find(int x) {return x == pre[x] ? x : pre[x] = find(pre[x]);}
- bool join(int x,int y){
- if(find(x) != find(y)) {
- pre[find(y)] = find(x);
- return true;
- }
- return false;
- }
- double cmp(node a,node b){
- double ans = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
- return sqrt(ans);
- }
- void kruskal(){
- for(int i = ;i < cnt ; i++){
- int mp1 = find(rod[i].st);
- int mp2 = find(rod[i].ed);
- if(join(mp1,mp2)) {
- ans += rod[i].w;
- tot++;
- }
- }
- }
- int main(){
- read(T);
- while(T--){
- read(n);
- for(int i = ; i < n; i++){
- scanf("%lf %lf",&pot[i].x,&pot[i].y);
- }
- memset(rod,);
- ans = ;
- cnt = tot = ;
- for(int i = ; i < maxn ;i ++)
- pre[i] = i;
- for(int i = ; i < n - ; i++){
- for(int j = i + ; j < n ; j++){
- double dist = cmp(pot[i],pot[j]);
- if( dist >= && dist <= ) {
- rod[cnt].st = i;
- rod[cnt].ed = j;
- rod[cnt++].w = dist;
- }
- }
- }
- sort(rod,rod + cnt);
- kruskal();
- if(tot != n-)
- printf("oh!\n");
- else
- printf("%.1lf\n",ans * );
- }
- }
HDU - 1875 畅通工程再续【最小生成树】的更多相关文章
- HDU 1875 畅通工程再续 (最小生成树)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- (step6.1.3)hdu 1875(畅通工程再续——最小生成树)
题目大意:本题是中文题,可以直接在OJ上看 解题思路:最小生成树 1)本题的关键在于把二维的点转化成一维的点 for (i = 0; i < n; ++i) { scanf("%d%d ...
- hdu 1875 畅通工程再续(prim方法求得最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1875 /************************************************* ...
- HDU 1875 畅通工程再续 (prim最小生成树)
B - 畅通工程再续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- HDU 1875 畅通工程再续 (最小生成树)
畅通工程再续 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/M Description 相信大家都听说一个"百岛湖&q ...
- HDU 1875 畅通工程再续(kruskal)
畅通工程再续 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 1875 畅通工程再续 (Prim)
题目链接:HDU 1875 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现 ...
- HDU - 1875 畅通工程再续(最小生成树)
d.c个小岛,通过建立桥,使其全部可达.求所有的桥的最小长度和. s.最小生成树,数据改成double就行了 c.Prim算法:cost[a][b]和cost[b][a]都得赋值. /* Prim算法 ...
- hdu 1875 畅通工程再续(最小生成树,基础)
题目 让人郁闷的题目,wa到死了,必须要把判断10.0和1000.0的条件放到prim函数外面去.如代码所放.... 正确的(放在prim外): //2个小岛之间的距离不能小于10米,也不能大于100 ...
随机推荐
- CSS3实现GIF动画
来自 dribbble 某位大师的作品,GIF图中一个小女孩抱着一只猫在跑步,非常可爱,动作轻巧,过渡自然.DEMO下载 回到项目需求,要实现类似上图卡通人物跑步动画,分析结果如下: 1.跑步动画可以 ...
- C语言---变量与函数
一个C程序是由一个或多个程序模块组成的,每一个程序模块作为一个源程序文件,一个源程序文件是一个编译单元. 源程序文件分为库函数和用户自己定义的函数,以及有参函数.无参函数. 函数调用的过程: 1) 定 ...
- CSS常用样式属性
1.CSS字体和文本相关属性 属性 font-family 规定文本的字体系列,比如:“serif” ''sans-serif" font-size 规定文本的字体尺寸 font-style ...
- html5-字体css
#div1{font-size: 50px;}#div2{font-size: 50%;}#div3{font-size: 300%}#div4{font-size: 3em;}#div5{font- ...
- map 的用法
#include<iostream> #include<map> #include<string> #define s second #define f first ...
- Python数据可视化-seaborn
详细介绍可以看seaborn官方API和example galler. 1 set_style( ) set( ) set_style( )是用来设置主题的,Seaborn有五个预设好的主题: d ...
- 【2017-03-20】HTML基础知识,标记,表格,表格嵌套及布局,超链接
一.HTML 网站(站点),网页基础知识 HTML是一门编程语言的名字:超文本标记语言 可以理解为:超越了文本的范畴,可以有图片.视频.音频.动画特效等其他内容,用标记的方法进行编程的计算机语言 基 ...
- python 查询文本文件的层次
I/O系统有一系列的层次构建而成 下面是操作一个文本文件的例子来查看这种层次 >>> f = open('sample.txt','w') >>> f <_i ...
- redux 数据规律
counter increase info todos 为 reducers 文件名 export default combineReducers({ todos, visibilityFilter ...
- Vue.js是什么,vue介绍
Vue.js是什么,vue介绍 Vue.js 是什么Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用. ...