POJ2502(最短路)(dji)

Subway

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17354   Accepted: 5544

Description

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.

Input

Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.

Output

Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.

Sample Input

0 0 10000 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1

Sample Output

21

Source

Waterloo local 2001.09.22

自己記錄點的出現順序+最短路

#include<iostream.>
#include<stdio.h>
#include<math.h>
#include<cstring>
using namespace std;

int coord[205][2];
int n;
int inf=999999999;
double e[202][202]={0};
double dis_a[1000];
int book[1000];

double dis(int x,int y,int x1,int y1){
    double dx=x-x1;
    double dy=y-y1;
    return sqrt(dx*dx+dy*dy);
}

int takein(int x,int y){
    for(int i=0;i<n;i++){
        if(coord[i][0]==x&&coord[i][1]==y){
           return i;
        }
    }
    coord[n][0]=x;
    coord[n][1]=y;
    n++;
    return n-1;
}

void walk(int a,int b){
    e[a][b]=dis(coord[a][0],coord[a][1],coord[b][0],coord[b][1]);
    e[a][b]*=60.0/10000;
    e[b][a]=e[a][b];
}

void take_sub(int a,int b){
    e[a][b]=dis(coord[a][0],coord[a][1],coord[b][0],coord[b][1]);
    e[a][b]*=60.0/1000/40;
    e[b][a]=e[a][b];
}

double dij(){
    for(int i=1;i<n;i++){
        dis_a[i]=e[0][i];
    }
    memset(book,0,sizeof(book));
    book[0]=1;
    for(int i=0;i<n-1;i++){
        double minn=inf;
        int u=-1;
        for(int j=0;j<n;j++){
            if(book[j]==0&&dis_a[j]<minn){
                u=j;
                minn=dis_a[j];
            }
        }
        book[u]=1;
        if(u==1)return dis_a[u];
        for(int j=0;j<n;j++){
            if(dis_a[j]>e[u][j]+dis_a[u]){
                dis_a[j]=e[u][j]+dis_a[u];
            }
        }
    }
}

int main(){
    n=0;
    for(int i=0;i<2;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        takein(a,b);
    }
    walk(0,1);
    int x,y;
    int lastx=-1,lasty=-1,lastid=-1;
    while(scanf("%d%d",&x,&y)!=EOF){
        if(x==-1&&y==-1){
            lastx=-1;lasty=-1;lastid=-1;
            continue;
        }
        if(lastx==-1&&lasty==-1){
            lastid=takein(x,y);
            lastx=x;lasty=y;
            continue;
        }else{
            int thisid=takein(x,y);
            take_sub(lastid,thisid);
            lastid=thisid;
            lastx=x;lasty=y;
            continue;
        }
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(e[i][j]==0){
                walk(i,j);
            }
        }
    }
    double ans=dij();
    int ansint=floor(ans+0.5);
    cout<<ansint<<endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章