hdu 1162 Eddy's picture

最小生成树裸题,没有初始化都能AC,看来测试数据只有一组

#include<iostream>
#include<vector>
#include<cmath>
#include<cstdio>
#define inf 1<<30
#define maxn 105
using namespace std;
struct stu
{
	double x,y;
};
stu mapp[maxn];
int n;
vector<int>root[maxn];
double vaule[maxn][maxn];
int visit[maxn];
void prim()
{
	double d[maxn];
	fill(d,d+maxn,inf);
	fill(visit,visit+maxn,0);
	double re=0;
	d[0]=0;
	while(1)
	{
		int v=-1;
		for(int i=0;i<n;i++)
		{
			if(!visit[i]&&(v==-1||d[i]<d[v])) v=i;
		}
		if(v==-1) break;
		re+=sqrt(d[v]);
		visit[v]=1;
		for(int i=0;i<root[v].size();i++)
		{
			int x=root[v][i];
			d[x]=min(d[x],vaule[v][x]);
		}
	}
	printf("%.2lf\n",re);
}
int main()
{

	while(cin>>n)
	{
		for(int i=0;i<maxn;i++) root[i].clear();
		for(int i=0;i<n;i++)
		{
			cin>>mapp[i].x>>mapp[i].y;
			for(int j=0;j<i;j++)
			{
				int l=pow(mapp[i].x-mapp[j].x,2)+pow(mapp[i].y-mapp[j].y,2);
				root[i].push_back(j);
				root[j].push_back(i);
				vaule[i][j]=l;
				vaule[j][i]=l;
			}

		}
		prim();
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 1162 Eddy's picture

时间: 2024-12-24 20:13:51

hdu 1162 Eddy's picture的相关文章

hdu 1162 Eddy&#39;s picture 最小生成树入门题 Prim+Kruskal两种算法AC

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7428    Accepted Submission(s): 3770 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

hdu 1162 Eddy&#39;s picture(最小生成树算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6866    Accepted Submission(s): 3469 Problem Description Eddy begins to like p

HDU 1162 Eddy&#39;s picture【最小生成树,Prime算法+Kruskal算法】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9334    Accepted Submission(s): 4711 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

HDU 1162 Eddy&#39;s picture(图论-最小生成树)

题目如下: Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7267    Accepted Submission(s): 3676 Problem Description Eddy begins to like painting pictures recently ,he is sure of himse

hdu 1162 Eddy&#39;s picture(最小生成树)

Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result i

HDU 1162 Eddy&#39;s picture (最小生成树)(java版)

Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 --每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点连在一起的最短总距离. 思路: 假设每两两点之间都有路径,求最小生成树. AC代码:(Java) 1 import java.util.Scanner; 2 import java.math.*; 3 public class Main { 4 public static final int MAX

HDU 1162 Eddy&#39;s picture (prime算法_裸题)

Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result i

hdu 1162 Eddy&#39;s picture (Kruskal算法,prim算法,最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法,保证联通的线路最短,典型的最小生成树问题. 方法一 , 通过不断找到最小的边来找到最终结果. Kruskal 算法 #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespac

HDU 1162 Eddy&#39;s picture (最小生成树 prim)

题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the res