Frogger(最短路 + Dijkstra + 邻接表 + 优先队列)

Frogger
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23230   Accepted: 7550

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. 

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

 

     题意:

     给出 N(2 ~ 200),说明有 N 块石头,后给出每块石头 x,y (0 ~ 1000)坐标。第一块石头代表起点坐标,第二块石头代表终点坐标。求从起点到达终点的所有路径中,直接相连最小的距离范围。

     比如1到2有两条路,分别为1 -> 2距离为2,故这条路的直接相连所需最大范围为2;

     还有一条为1 -> 3 -> 2 距离分别为根号2 和根号2,故这条路直接相连所需最大范围为根号2;

     因为2 < 根号2,故最小距离范围为2。

 

     思路:

     本质是最短路。题目略有点难理解。每个节点保存当前的最大所需范围,若每次更新新的最大所需范围比当前小,则更新这个最大所需范围的值。Dijkstra + 邻接表 + 优先队列。用 double 型保存距离,输出的时候注意输出最后三位即可。

 

    AC:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAX 50000
#define INF 99999999
using namespace std;

typedef pair pii;
typedef struct {
    double x,y;
}node;

node no[205];
int v[MAX],next[MAX],fir[205],vis[205];
double w[MAX],d[205];
int ind,n;

double Distance(node a,node b) {
    double x = a.x - b.x;
    double y = a.y - b.y;
    return sqrt(x * x + y * y);
}

void add_edge(int f,int t,double val) {
    v[ind] = t;
    w[ind] = val;
    next[ind] = fir[f];
    fir[f] = ind;
    ind++;
}

void Dijkstra() {
    memset(vis,0,sizeof(vis));
    for(int i = 1;i <= n;i++) d[i] = INF;
    priority_queue,greater > q;
    d[1] = 0;
    q.push(make_pair(d[1],1));
    while(!q.empty()) {
        pii k = q.top();q.pop();
        int x = k.second;
        if(vis[x]) continue;
        vis[x] = 1;
        for(int e = fir[x];e != -1;e = next[e]) {
            int y = v[e];
            double max_ran = max(w[e],d[x]);
            if(d[y] > max_ran) {
               d[y] = max_ran;
               q.push(make_pair(d[y],y));
            }
        }
    }

}

int main() {
    int t = 0;
    while(~scanf("%d",&n) && n) {
        t++;
        ind = 0;
        memset(fir,-1,sizeof(fir));
        for(int i = 1;i <= n;i++)
            scanf("%lf%lf",&no[i].x,&no[i].y);

        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= n;j++) {
            if(i == j) continue;
            double s = Distance(no[i],no[j]);
            add_edge(i,j,s);
        }

        Dijkstra();

        printf("Scenario #%d\n",t);
        printf("Frog Distance = %.3lf\n\n",d[2]);
    }
    return 0;
}

 

 

 

你可能感兴趣的:(POJ)