http://poj.org/problem?id=2253
题意:有一只青蛙A想要跳到另一只青蛙B处, 由于青蛙A的跳跃幅度没有那么大, 所以它需要借助它周边的一些石头从而到达青蛙B处。问你在众多可供选择的道路中,哪条道路中跳跃的最大值要比其他道路中的最大值小。(也就是求它的跳跃幅度至少要为多少)
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<queue> using namespace std; typedef long long LL; #define oo 0x3f3f3f3f #define maxn 500 double maps[maxn][maxn], dist[maxn]; int v[maxn]; int n; struct node { int x, y; }A[maxn]; double F(int i, int j) { return (sqrt(pow((double)A[i].x-A[j].x, 2)+pow((double)A[i].y-A[j].y, 2))); } double Dij() { memset(v, 0, sizeof(v)); for(int i=1; i<=n; i++) dist[i] = maps[1][i]; dist[1] = 0; v[1] = 1; for(int i=1; i<n; i++) { int index; double mins = 10000000; for(int j=1; j<=n; j++) { if(!v[j] && dist[j] < mins) { index = j; mins = dist[j]; } } v[index] = 1; for(int j=1; j<=n; j++) { if(!v[j]) { double maxs = max(mins, maps[index][j]); if(dist[j]>maxs) dist[j] = maxs; } } } return dist[2]; } int main() { int cnt = 1; while(scanf("%d", &n), n) { for(int i=1; i<=n; i++) scanf("%d %d", &A[i].x, &A[i].y); for(int i=1; i<=n; i++) { for(int j=1; j<i; j++) { maps[i][j] = maps[j][i] = F(i, j); } maps[i][i] = 0; } double ans = Dij(); printf("Scenario #%d\n", cnt++); printf("Frog Distance = %.3f\n\n", ans); } return 0; }
#include<algorithm> #include<queue> #include<stdio.h> #include<string.h> #include<vector> #include<math.h> using namespace std; const int maxn = 205; const int oo = 0xfffffff; struct point { double x, y; } p[maxn]; struct node { int y; double len; node(int y, double len):y(y), len(len) {} }; vector<node> G[maxn]; double v[maxn]; //求两点间的距离 double Len(point a, point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } void Spfa() { queue<int> Q; Q.push(1); while(Q.size()) { int s = Q.front(); Q.pop(); int len = G[s].size(); for(int i=0; i<len; i++) { node q = G[s][i]; if(v[s] < v[q.y] && q.len < v[q.y]) { v[q.y] = max(v[s], q.len);//要选取一条路上的最大的那条边 Q.push(q.y); } } } } int main() { int N, t=1; while(scanf("%d", &N), N) { int i, j; for(i=1; i<=N; i++) scanf("%lf%lf", &p[i].x, &p[i].y); for(i=1; i<=N; i++) for(j=1; j<=N; j++) { if(i == j) continue; double len = Len(p[i], p[j]); G[i].push_back(node(j, len)); } for(i=1; i<=N; i++) v[i] = oo; v[1] = 0; Spfa(); if(t != 1)printf("\n"); printf("Scenario #%d\n", t++); printf("Frog Distance = %.3f\n", v[2]); for(i=1; i<=N; i++) G[i].clear(); } return 0;
时间: 2024-10-29 03:59:22