POJ 2253 Frogger(最短路变形)
这个题就是找到1 到 2之间的路径上所有经过路的最大值中的最小值。。。有点绕,反正就是最短路的变形。。。没太想清楚怎么写,写了spfa和floyd都错了,没想清楚怎么变形啊。。。
一个地方想错了。。。改了都过了。。
1 #include <cstdio>//spfa算法 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #define eps 1e-7 8 double dp[201][201],d[201]; 9 int x[201],y[201],n; 10 double judge(int i,int j) 11 { 12 return sqrt((x[i]-x[j])*(x[i]-x[j])*1.0+(y[i]-y[j])*(y[i]-y[j])*1.0); 13 } 14 void spfa() 15 { 16 int u,i; 17 int in[201]; 18 queue<int> que; 19 memset(in,0,sizeof(in)); 20 for(i = 1;i <= n;i ++) 21 { 22 d[i] = INF; 23 } 24 d[1] = 0; 25 que.push(1); 26 in[1] = 1; 27 while(!que.empty()) 28 { 29 u = que.front(); 30 que.pop(); 31 in[u] = 0; 32 for(i = 1;i <= n;i ++) 33 { 34 if(d[i] > max(d[u],dp[u][i])) 35 { 36 d[i] = max(d[u],dp[u][i]); 37 if(!in[i]) 38 { 39 in[i] = 1; 40 que.push(i); 41 } 42 } 43 } 44 } 45 } 46 int main() 47 { 48 int i,j,cas = 0; 49 while(scanf("%d",&n)!=EOF) 50 { 51 cas ++; 52 if(n == 0) break; 53 for(i = 1;i <= n;i ++) 54 scanf("%d%d",&x[i],&y[i]); 55 for(i = 1;i <= n;i ++) 56 { 57 for(j = 1;j <= n;j ++) 58 dp[i][j] = judge(i,j); 59 } 60 spfa(); 61 printf("Scenario #%d\nFrog Distance = ",cas); 62 printf("%.3lf\n\n",d[2]); 63 } 64 return 0; 65 }
1 #include <cstdio>//floyd算法 2 #include <cstring> 3 #include <cmath> 4 #include <queue> 5 using namespace std; 6 #define INF 0x3f3f3f3f 7 #define eps 1e-7 8 double dp[201][201]; 9 int x[201],y[201],n; 10 double judge(int i,int j) 11 { 12 return sqrt((x[i]-x[j])*(x[i]-x[j])*1.0+(y[i]-y[j])*(y[i]-y[j])*1.0); 13 } 14 int main() 15 { 16 int i,j,k,cas = 0; 17 while(scanf("%d",&n)!=EOF) 18 { 19 cas ++; 20 if(n == 0) break; 21 for(i = 1;i <= n;i ++) 22 scanf("%d%d",&x[i],&y[i]); 23 for(i = 1;i <= n;i ++) 24 { 25 for(j = 1;j <= n;j ++) 26 dp[i][j] = judge(i,j); 27 } 28 for(i = 1;i <= n;i ++) 29 { 30 for(j = 1;j <= n;j ++) 31 { 32 for(k = 1;k <= n;k ++) 33 { 34 if(dp[j][k] > max(dp[j][i],dp[i][k])&&i != j&&j != k&&i != k) 35 { 36 dp[j][k] = max(dp[j][i],dp[i][k]); 37 } 38 } 39 } 40 } 41 printf("Scenario #%d\nFrog Distance = ",cas); 42 printf("%.3lf\n\n",dp[1][2]); 43 } 44 return 0; 45 }