Outlets
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2504 Accepted Submission(s): 1160
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.
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
Source
Recommend
题意:求最小生成树,p点和q点必须连着
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cmath> 6 #include <cstdlib> 7 #include <limits> 8 #include <queue> 9 #include <stack> 10 #include <vector> 11 #include <map> 12 13 using namespace std; 14 15 #define N 55 16 #define INF 0xfffffff 17 #define PI acos (-1.0) 18 #define EPS 1e-8 19 #define Lson rt<<1, l, tree[rt].mid () 20 #define Rson rt<<1|1, tree[rt].mid () + 1, r 21 22 struct node 23 { 24 int x, y; 25 } P[N]; 26 27 int n, vis[N]; 28 29 double maps[N][N], dist[N]; 30 31 void init() 32 { 33 for(int i = 1; i <= n; i++) 34 { 35 vis[i] = 0; 36 for(int j = 1; j <= n; j++) 37 maps[i][j] = maps[j][i] = INF; 38 maps[i][i] = 0; 39 } 40 } 41 42 int main() 43 { 44 int p, q; 45 46 while(scanf("%d", &n), n) 47 { 48 init(); 49 50 scanf("%d%d", &p, &q); 51 52 for(int i = 1; i <= n; i++) 53 scanf("%d%d", &P[i].x, &P[i].y); 54 55 int x = abs(P[p].x-P[q].x); 56 int y = abs(P[p].y-P[q].y); 57 58 double tmp = 1.0*sqrt(x*x+y*y); 59 60 for(int i = 1; i <= n; i++) 61 { 62 for(int j = 1; j <= n; j++) 63 { 64 int f = abs(P[i].x-P[j].x), g = abs(P[i].y-P[j].y); 65 66 maps[i][j] = 1.0*sqrt(f*f+g*g); 67 maps[j][i] = maps[i][j]; 68 } 69 } 70 71 maps[p][q] = maps[q][p] = 0; 72 73 double ans = 0; 74 75 dist[1] = 0; 76 77 for(int i = 1; i <= n; i++) 78 dist[i] = maps[i][1]; 79 80 vis[1] = 1; 81 82 for(int i = 1; i < n; i++) 83 { 84 int index; 85 double Min = INF; // double Min, 不知道在这错多少回了 86 for(int j = 1; j <= n; j++) 87 { 88 if(!vis[j] && dist[j] < Min) 89 Min = dist[j], index = j; 90 } 91 vis[index] = 1; 92 ans += Min; 93 for(int j = 1; j <= n; j++) 94 { 95 if(!vis[j] && dist[j] > maps[j][index]) 96 dist[j] = maps[j][index]; 97 } 98 } 99 printf("%.2f ", ans+tmp); 100 } 101 return 0; 102 }