Outlets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 351 Accepted Submission(s): 174
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
42 30 01 00 -1 1 -10
Sample Output
3.41
Source
2012 Asia Hangzhou Regional Contest
Statistic |Submit |Discuss |Note
最小生成树简单应用
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define eps (1e-8)
#define dg(i) cout << "*" << i << endl;
using namespace std;
struct Position
{
int x, y;
}pos[51];
struct Edge
{
int u, v;
double dis;
}e[49 * 25 + 1];
int set[51];
double Sqrt(const Position a, const Position b)
{
return sqrt(1.0*(a.x - b.x)*(a.x - b.x) + 1.0*(a.y - b.y)*(a.y - b.y));
}
int Find(int x)
{
if(x != set[x]) set[x] = Find(set[x]);
return set[x];
}
bool operator < (const Edge& x, const Edge& y)
{
return x.dis < y.dis;
}
int main()
{
int N, p, q;
double minDis;
int i, j, k, cnt;
while(scanf("%d", &N) && N)
{
scanf("%d %d", &p, &q);
for(i = 0; i < N; ++i)
{
set[i] = i;
scanf("%d %d", &pos[i].x, &pos[i].y);
}
minDis = Sqrt(pos[p-1], pos[q-1]);
set[p-1] = q - 1;
if(p > q) swap(p, q);
for(k = 0, i = 0; i < N - 1; i++)
{
for(j = i + 1; j < N; j++, k++)
{
e[k].u = i;
e[k].v = j;
if(i == p - 1 && j == q - 1) e[k].dis = 0.0;
else e[k].dis = Sqrt(pos[i], pos[j]);
//cout << i << " " << j << " " << e[k].dis << endl;
}
}
sort(e, e + k);
for(cnt = 1, i = 1; i < k; i++)
{
int su = Find(e[i].u);
int sv = Find(e[i].v);
if(su != sv)
{
if(su > sv) set[su] = sv;
else if(su < sv) set[sv] = su;
minDis += e[i].dis;
if(++cnt == N - 1) break;
}
}
printf("%.2lf\n", minDis);
}
return 0;
}
本文介绍了一种在特定条件下构建购物场所的最短路径问题。通过将Nike店与Apple店直接连接,并在此基础上构造整个商场的最小生成树,实现了总路径长度的最小化。
1493

被折叠的 条评论
为什么被折叠?



