1. 程式人生 > >poj 2502 Dijkstra

poj 2502 Dijkstra

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8667 Accepted: 2801

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

題意:

給出你起點和終點

再給你  若干  條地鐵線,每一條地鐵線有  若干  站點

求從起點到終點的最短的時間

題解:

首先當然是建圖

建圖後就簡單了,直接用dijkstra就行了

注意:

這裡求的是時間(分鐘)

題目中給出了不同的速度

每一個站點間可以步行走過去

只有相鄰的站點才有路通過去

所有的路徑都是雙向的

 時間=路徑/ 速度

這裡的速度單獨列出來還不會出錯,我就是直接寫在上面並且還是化簡的反而出錯了

通過這個題目知道了,自己還是看別人的解題報告看太多了,模板也寫不出,都是套用的

這些都是不好的習慣,希望繼續加油

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define INF 0x3f3f3f3f

struct node
{
    int x,y;
}a[300],e,frist,endn;
int cnt;

double map[300][300];

int find_it(int x,int y)
{
    for(int i=1;i<cnt;i++)
        if(a[i].x==x&&a[i].y==y)
            return i;

    return cnt++;
}

double calc(int i,int j)
{
    double x=a[i].x-a[j].x;
    double y=a[i].y-a[j].y;
    return sqrt(x*x+y*y);
}

double path[300];
int flag[300];

int find_min(int n)
{
    int posi=-1;
    int min_weight=INF;
    for(int i=1;i<=cnt;i++)
        if(min_weight>path[i]&&flag[i]==0){
            posi=i;
            min_weight=path[i];
        }
    return posi;
}

void Dijkstra()
{
    for(int i=1;i<=cnt;i++){
        path[i]=INF;
        flag[i]=0;
    }
    path[1]=0;
    int p=1;
    while(p!=-1)
    {
        flag[p]=1;
        for(int i=1;i<=cnt;i++)
            if(path[i]>map[p][i]+path[p]&&flag[i]==0)
                path[i]=map[p][i]+path[p];

        p=find_min(cnt);
    }
}

int main()
{
    double v1=40000.0/60;
    double v2=10000.0/60;
    //freopen("in.txt","r",stdin);

    for(int i=0;i<=250;i++)
        for(int j=0;j<=250;j++)
            map[i][j]=INF;

    scanf("%d%d%d%d",&a[1].x,&a[1].y,&e.x,&e.y);

    cnt=2;
    int p1,p2;
    while(scanf("%d%d",&frist.x,&frist.y)!=EOF)
    {
        p1=find_it(frist.x,frist.y);
        a[p1].x=frist.x;
        a[p1].y=frist.y;
        while(scanf("%d%d",&endn.x,&endn.y),endn.x!=-1&&endn.y!=-1)
        {
            p2=find_it(endn.x,endn.y);
            a[p2].x=endn.x;
            a[p2].y=endn.y;

            if(p1!=p2){
                double temp=calc(p1,p2)/v1;
                map[p1][p2]=map[p2][p1]=min(map[p1][p2],temp);
            }
            p1=p2;
        }
    }
    a[cnt].x=e.x;
    a[cnt].y=e.y;

    for(int i=1;i<=cnt;i++)
        for(int j=1;j<=cnt;j++)
            if(i!=j)
                map[i][j]=min(map[i][j],calc(i,j)/v2);

    Dijkstra();

    printf("%.0lf",path[cnt]);

    return 0;
}