poj 2253 frogger 最短路

有N个点,青蛙A 在点1上,青蛙B在点2上,然后青蛙可以通过剩下的点来到达2点,求各个通路中最大的一段中的最小值~

题目连接http://poj.org/problem?id=2253

我的代码:

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <math.h>
 4 #include<algorithm>
 5 #include <iostream>
 6 using namespace std;
 7 
 8 int n;
 9 
10 struct node
11 {
12     int x,y;
13 } p[10000];
14 struct edge
15 {
16     int u,v;
17     double dis;
18 } e[100000];
19 
20 int set[10000];
21 int cmp(struct edge a,struct edge b)
22 {
23     return a.dis < b.dis;
24 }
25 
26 int find(int x)
27 {
28    if(set[x] != x)
29    set[x] = find(set[x]);
30    return set[x];
31 }
32 void merge(int a,int b)
33 {
34     int fa,fb;
35     fa = find(a);
36     fb = find(b);
37         set[fa] = fb;
38 
39     return ;
40 }
41 int main()
42 {
43     int n;
44     int cas;
45     cas = 1;
46     while(scanf("%d",&n)&&n)
47     {
48         int i,j,count;
49 
50         for(i = 0;i < n;i++)
51         set[i] = i;
52         for(i = 0; i < n; i++)
53             scanf("%d %d",&p[i].x,&p[i].y);
54         for(i = 0,count = 0; i < n; i++)
55         {
56             for(j = 0; j < i; j++)
57             {
58                 double a,b;
59                 e[count].u = i;
60                 e[count].v = j;
61                 a = (p[i].x-p[j].x)*(p[i].x-p[j].x);
62                 b = (p[i].y-p[j].y)*(p[i].y-p[j].y);
63                 e[count++].dis = sqrt(a+b);
64             }
65         }
66         double ans;
67         ans = 0;
68         sort(e,e+count,cmp);
69 
70         for(i = 0; i < count; i++)
71         {
72 
73             if(find(0) == find(1))
74             {
75                 ans = e[i].dis;
76                 break;
77             }
78             merge(e[i].u,e[i].v);
79         }
80         printf("Scenario #%d\nFrog Distance = %.3f\n\n",cas++,e[i-1].dis);
81     }
82     return 0;
83 }
posted @ 2012-11-10 21:03  某某。  阅读(132)  评论(0编辑  收藏  举报