hdu 4463 Outlets Prim 次小生成树 简单题

Outlets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2200    Accepted Submission(s): 1028

Problem Description

In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the
United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.

So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can‘t match the demand of Chinese. So they want to build a new outlets
in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be
directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.

Input

There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating
that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate
of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.

Output

For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.

Sample Input

4
2 3
0 0
1 0
0 -1
1 -1
0

Sample Output

3.41

转载请申明博客原地址:http://blog.csdn.net/lionel_d

数据范围小。水的不能再水,没什么可说的,就是次小生成树

#include <stdio.h>
#include <string.h>
#include <cmath>
#define MAX 60

const double INF = 1000000000.0 ;

struct Point{
	int x,y;
}p[MAX];

double graph[MAX][MAX] ,max[MAX][MAX] ;
bool used[MAX][MAX] , visited[MAX];

double prim(int n)
{
	int closest[MAX];
	double lowCost[MAX] , sum = 0.0;
	memset(visited,false,sizeof(visited)) ;
	memset(max,0,sizeof(max)) ;
	memset(used,false,sizeof(used)) ;
	for(int i = 1 ; i <= n ; ++i)
	{
		lowCost[i] = graph[1][i] ;
		closest[i] = 1 ;
	}
	visited[1] = true ;
	for(int i = 0 ; i < n-1 ; ++i)
	{
		double min = INF ;
		int index = -1 ;
		for(int j = 1 ; j <= n ; ++j)
		{
			if(!visited[j] && lowCost[j]<min)
			{
				min = lowCost[j];
				index = j ;
			}
		}
		if(index == -1)
		{
			break ;
		}
		sum += lowCost[index] ;
		visited[index] = true ;
		used[index][closest[index]] = used[closest[index]][index] = true ;
		for(int j = 1 ; j <= n ; ++j)
		{
			if(visited[j] && j != index)
			{
				max[j][index] = max[index][j] = lowCost[index]>max[j][closest[index]]?lowCost[index]:max[j][closest[index]] ;
			}
			if(!visited[j] && lowCost[j]>graph[index][j])
			{
				lowCost[j] = graph[index][j] ;
				closest[j] = index ;
			}
		}
	}
	return sum ;
}

double dis(const Point &p1 , const Point &p2)
{
	int x = p1.x-p2.x , y = p1.y-p2.y ;
	return sqrt((x*x+y*y)*1.0) ;
}

int main()
{
	int n ;
	while(~scanf("%d",&n) && n)
	{
		Point p[MAX] ;
		int pt,qt;
		scanf("%d%d",&pt,&qt);
		for(int i = 1 ; i <= n ; ++i)
		{
			scanf("%d%d",&p[i].x,&p[i].y) ;
		}
		for(int i = 1 ; i <= n ; ++i)
		{
			graph[i][i] = 0 ;
			for(int j = 1 ; j < i ; ++j)
			{
				graph[i][j] = graph[j][i] = dis(p[i],p[j]) ;
			}
		}
		double sum = prim(n) ;
		if(used[pt][qt])
		{
			printf("%.2lf\n",sum) ;
		}
		else
		{
			printf("%.2lf\n",sum+graph[pt][qt]-max[pt][qt]) ;
		}
	}
	return 0 ;
}

与君共勉

时间: 2025-01-02 04:28:10

hdu 4463 Outlets Prim 次小生成树 简单题的相关文章

hdu 1679 The Unique MST 次小生成树 简单题

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21737   Accepted: 7692 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

HDU 4463 Outlets (prime_杭州区域赛水题)

Problem Description In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on t

hdu 1234 开门人和关门人(简单题)

开门人和关门人 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10658    Accepted Submission(s): 5434 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一行

poj1679次小生成树入门题

次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即每次删去1,2,4边之后求得的最大边 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstd

HDU 4463: Outlets

http://acm.hdu.edu.cn/showproblem.php?pid=4463 1 #include <iostream> 2 #include <algorithm> 3 #include <cmath> 4 #include <iomanip> 5 //n:= # of vertices 6 using namespace std; 7 const int MAXN = 55; 8 typedef pair<int, int>

【HDU 4463 Outlets】最小生成树

以(x,y)坐标的形式给出n个点,修建若干条路使得所有点连通(其中有两个给出的特殊点必须相邻),求所有路的总长度的最小值. 因对所修的路的形状没有限制,所以可看成带权无向完全图,边权值为两点间距离.因是稠密图,故用邻接矩阵存储更好(完全图,边数e达到n(n-1)/2). 至此,可将问题抽象为求最小生成树的边权和. 邻接矩阵版Prim算法求最小生成树,时间复杂度为O(n*n).邻接表版prim用堆优化后理论上可达O(elogn),边数组版kruscal理论也为O(elogn),但此题是完全图,e=

hdu 4463 Outlets(最小生成树)

题意:n个点修路,要求总长度最小,但是有两个点p.q必须相连 思路:完全图,prim算法的效率取决于节点数,适用于稠密图.用prim求解. p.q间距离设为0即可,最后输出时加上p.q间的距离 prim算法: #include<iostream> #include<stdio.h> #include<string.h> #include<math.h> using namespace std; #define INF 15000//计算得最长值 #defin

HDU 4463 Outlets(一条边固定的最小生成树)

Outlets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2594    Accepted Submission(s): 1196 Problem Description In China, foreign brand commodities are often much more expensive than abroad. Th

hdu 4463 Outlets 最小生成树

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma