POJ2253 Frogger
Frogger
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17471 | Accepted: 5692 |
Description
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.
You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone.
Input
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
Sample Input
2 0 0 3 4 3 17 4 19 4 18 5 0
Sample Output
Scenario #1 Frog Distance = 5.000 Scenario #2 Frog Distance = 1.414
Source
思路:首先使用KRUSKAL求出最小生成树,然后再使用DFS遍历刚刚求出的最小生成树,找出从开始点到结束点的路径,找出这条路径上的最大长度的边。
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <algorithm> 7 8 9 #define MAXDOUBLE 1.0e30 10 using namespace std; 11 12 13 struct Pos{ 14 double x,y; 15 }pos[200+4];//存储各个点的坐标 16 17 18 struct Edge{ 19 int v1,v2; 20 double cost; 21 }edge[200*200];//使用kruskal的数据结构 22 23 int len=0; 24 25 26 27 double g[200+1][200+1];//存储kruskal求出的那些边 28 29 30 int fa[200+4]; 31 32 int findfa(int v) 33 { 34 if(fa[v]!=v) 35 {fa[v]=findfa(fa[v]);} 36 return fa[v]; 37 } 38 39 40 41 42 bool cmp(Edge x,Edge y) 43 { 44 if(x.cost<y.cost) 45 return true; 46 return false; 47 } 48 49 int vis[200+1];//用于DFS时起标记作用 50 51 int dfs(int v,int n,double &mincost) 52 { 53 int i,j,k; 54 55 double tmp; 56 57 if(v==1) 58 return 1; 59 60 for(i=1;i<n;i++) 61 {if((vis[i]==0)&&(g[v][i]!=-1)) 62 {vis[i]=1; 63 tmp=mincost; 64 65 if(mincost<g[v][i]) 66 mincost=g[v][i]; 67 68 69 70 if(1==dfs(i,n,mincost)) 71 {return 1; 72 73 } 74 mincost=tmp; 75 76 } 77 } 78 79 return 0; 80 } 81 82 83 84 85 86 87 88 int main(int argc, char *argv[]) 89 { 90 91 int n; 92 int i,j,k; 93 94 int count=0; 95 96 while(scanf("%d",&n)!=EOF) 97 { 98 if(n==0) 99 break; 100 101 count++; 102 103 for(i=0;i<n;i++) 104 {cin>>pos[i].x>>pos[i].y;} 105 106 len=0; 107 108 109 for(i=0;i<n;i++) 110 for(j=0;j<i;j++) 111 { 112 edge[len].v1=i; 113 edge[len].v2=j; 114 edge[len].cost=sqrt((pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y)); 115 len++; 116 } 117 118 119 sort(edge,edge+len,cmp); 120 121 122 for(i=0;i<n;i++) 123 {fa[i]=i;} 124 125 126 int num=0; 127 for(i=0;i<n;i++) 128 for(j=0;j<n;j++) 129 {g[i][j]=-1;} 130 131 132 for(i=0;i<len;i++) 133 { 134 int fv1=findfa(edge[i].v1); 135 int fv2=findfa(edge[i].v2); 136 if(fv1!=fv2) 137 {fa[fv1]=fv2; 138 g[edge[i].v1][edge[i].v2]=edge[i].cost; 139 g[edge[i].v2][edge[i].v1]=edge[i].cost; 140 } 141 142 } 143 144 145 146 147 for(i=0;i<n;i++) 148 {vis[i]=0;} 149 150 double maxmincost=0; 151 152 dfs(0,n,maxmincost); 153 float maxmincost2=(float)maxmincost; 154 printf("Scenario #%d\nFrog Distance = %.3f\n\n",count,maxmincost2); 155 } 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 //system("PAUSE"); 188 return EXIT_SUCCESS; 189 }