zoukankan      html  css  js  c++  java
  • hdoj_4463Outlets

    Outlets

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 706    Accepted Submission(s): 330


    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

    连接一条路径后,再求最小生成树。

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    #pragma warning(disable : 4996)
    #define MAX 1000
    typedef struct edge
    {
    	int x, y;
    	double w;
    }edge;
    
    edge e[MAX];
    int father[MAX], ranks[MAX];
    
    bool cmp(edge a,edge b)
    {
    	return a.w < b.w;
    }
    
    void Make_Set(int n)
    {
    	for(int i = 1; i <= n; i++)
    	{
    		father[i] = i;
    		ranks[i] = 0;
    	}
    }
    
    int Find_Set(int x)
    {
    	if(x != father[x])
    		father[x] = Find_Set(father[x]);
    	return father[x];
    }
    
    void Merge_Set(int x, int y)
    {
    	x = Find_Set(x);
    	y = Find_Set(y);
    	if(x == y) return;
    	if(ranks[x] > ranks[y])
    	{
    		father[y] = x;
    	}
    	else if(ranks[x] < ranks[y])
    	{
    		father[x] = y;
    	}
    	else 
    	{
    		ranks[y]++;
    		father[x] = y;
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	double point[MAX][2] = {0.0};
    	int n, count, x, y;
    	double len, sum, temp;
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		scanf("%d %d", &x, &y);
    		count = 0;
    		for(int i = 1; i <= n; i++)
    		{
    			scanf("%lf %lf", &point[i][0], &point[i][1]);
    		}
    		temp = sqrt((point[x][0] - point[y][0]) * (point[x][0] - point[y][0]) + (point[x][1] - point[y][1]) * (point[x][1] - point[y][1]));
    		sum = 0;
    		for(int i = 1; i <= n; i++)
    		{
    			for(int j = i + 1; j <= n; j++)
    			{
    				len = sqrt((point[i][0] - point[j][0]) * (point[i][0] - point[j][0]) + (point[i][1] - point[j][1]) * (point[i][1] - point[j][1]));
    				if(len > 0)
    				{
    					e[count].x = i;
    					e[count].y = j;
    					e[count++].w = len;
    				}
    			}
    		}
    		Make_Set(n);
    		sort(e, e + count , cmp);
    		Merge_Set(x, y);
    		for (int i = 0; i < count; i++)
    		{
    			int x = Find_Set(e[i].x);
    			int y = Find_Set(e[i].y);
    			if(x != y)
    			{
    				Merge_Set(e[i].x, e[i].y);
    				sum += e[i].w;
    			}
    		}
    		sum += temp;
    		printf("%.2lf\n", sum);
    	}
    	return 0;
    }
    



  • 相关阅读:
    Redis企业级数据备份与恢复方案
    使用canal增量同步mysql数据库信息到ElasticSearch
    SpringBoot基于数据库实现简单的分布式锁
    SpringBoot+ShardingSphere实现分库分表 + 读写分离
    SpringBoot 使用JestClient操作Elasticsearch
    Java 操作 MongoDB
    VS C#开发中WinForm中Setting.settings的作用
    Sql 触发器禁用和启用
    ROW_NUMBER over (order by **)
    Aspen 安装
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834980.html
Copyright © 2011-2022 走看看