poj 2253 Frogger

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9968 Accepted: 3327

     本题就是求最短路中最长的那一段,prim算法当U=2是即可跳出

代码:

 

  
    
1 #include < stdio.h >
2 #include < math.h >
3
4   int x[ 205 ],y[ 205 ],max,n;
5   void prim( int a, int b)
6 {
7 int visit[ 205 ],dist[ 205 ],min,u,i,j,distance;
8 for (i = 1 ;i <= n;i ++ )
9 {
10 dist[i] = (x[i] - a) * (x[i] - a) + (y[i] - b) * (y[i] - b);
11 visit[i] = 0 ;
12 }
13 visit[ 1 ] = 1 ;
14 for (i = 1 ;i < n;i ++ )
15 {
16 min = 0xfffffff ;
17 for (j = 1 ;j <= n;j ++ )
18 {
19 if ( ! visit[j] && dist[j] < min)
20 {
21 u = j;
22 min = dist[j];
23 }
24 }
25 if (min > max)
26 max = min;
27 if (u == 2 )
28 return ;
29 visit[u] = 1 ;
30 for (j = 1 ;j <= n;j ++ )
31 {
32 distance = (x[u] - x[j]) * (x[u] - x[j]) + (y[u] - y[j]) * (y[u] - y[j]);
33 if ( ! visit[j] && distance < dist[j])
34 {
35 dist[j] = distance;
36 }
37 }
38 }
39 }
40   int main()
41 {
42 int count = 0 ,i;
43 while (scanf( " %d " , & n) != EOF)
44 {
45 max = 0 ;
46 count ++ ;
47 if (n == 0 )
48 break ;
49 for (i = 1 ;i <= n;i ++ )
50 {
51 scanf( " %d%d " , & x[i], & y[i]);
52 }
53 prim(x[ 1 ],y[ 1 ]);
54 printf( " Scenario #%d\n " ,count);
55 printf( " Frog Distance = %.3f\n " ,sqrt(max * 1.0 ));
56 printf( " \n " );
57 }
58 return 0 ;
59 }
60  

 

 

 

你可能感兴趣的:(poj)