1. 程式人生 > >POJ 2502 Subway(將各種資料轉化成圖+最短路+迪傑斯特拉演算法)

POJ 2502 Subway(將各種資料轉化成圖+最短路+迪傑斯特拉演算法)

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

題解:

這題是我這場比賽做過最有意思的一題,wa了8次才過。。發現了一個很坑的地方,就是每一條鐵路線上,假設有n個點,那麼只能形成n-1條邊。。體會一下,比如求首位兩站的時間,接首尾連起來用距離除以40這個算出來是錯的,因為鐵路可能是彎的!!我後來意識到了這點改了一下就ac了。。還有要注意一點就是除了鐵路線之間形成的邊是除以40,其他的點互相直接連線都是除以10計算,設dis是兩點間直線距離,怕覆蓋掉就改成min(p[i][j],dis/10),就好了,然後經過老劉的一系列單位換算,得出最後答案乘3.6除60就是分鐘數,注意G++交要%f,這裡因為%.0f自帶四捨五入就不用操心了

ps:

因為當時wa得慌了。。本來應該把算直線距離的公式寫成函式可以漂亮很多簡潔很多的qaq

程式碼:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
using namespace std;
const double INF=200861111;
int vis[305];//存迪傑斯特拉是否遍歷過該節點
double p[305][305];//存圖
double dis[305];//存距離1的距離
double dx[305];//存當前鐵路每個點的x座標
double dy[305];//存當前鐵路每個點的y座標
double xx[305];//存各個點的x座標
double yy[305];//存各個點的y座標
int main()
{
    int i,j,k,n,m,tot=3,d,ans=0,sum,key;
    double sx,ex,sy,ey,minn;
    for(i=1;i<=220;i++)
        for(j=1;j<=220;j++)
          {
              if(i==j)
                p[i][j]=0;
              else
                p[i][j]=INF;//初始化
          }
    scanf("%lf%lf%lf%lf",&sx,&sy,&ex,&ey);
    p[1][2]=sqrt((sx-ex)*(sx-ex)+(sy-ey)*(sy-ey))/10;//記出發點為1
    p[2][1]=sqrt((sx-ex)*(sx-ex)+(sy-ey)*(sy-ey))/10;//記結束點為2
    xx[1]=sx;
    yy[1]=sy;
    xx[2]=ex;
    yy[2]=ey;
    while(scanf("%lf%lf",&dx[ans],&dy[ans])!=EOF)
    {
        if(dx[ans]==-1&&dy[ans]==-1)
        {
            tot+=ans;
            ans=0;
            continue;
        }
        if(ans!=0)
        {
            p[tot+ans][tot+ans-1]=sqrt((dx[ans]-dx[ans-1])*(dx[ans]-dx[ans-1])+(dy[ans]-dy[ans-1])*(dy[ans]-dy[ans-1]))/40;
            p[tot+ans-1][tot+ans]=sqrt((dx[ans]-dx[ans-1])*(dx[ans]-dx[ans-1])+(dy[ans]-dy[ans-1])*(dy[ans]-dy[ans-1]))/40;//記錄時間
        }
        xx[tot+ans]=dx[ans];
        yy[tot+ans]=dy[ans];
        ans++;
    }
    for(i=1;i<tot;i++)
    {
        for(j=1;j<tot;j++)
        {
            p[i][j]=p[j][i]=min(p[i][j],sqrt((xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]))/10);//記錄到各點步行的時間
        }
    }
    memset(vis,0,sizeof(vis));初始化為0
    for(i=1;i<tot;i++)
        dis[i]=INF;
    dis[1]=0;
    for(i=1;i<tot-1;i++)//迪傑斯特拉演算法
    {
        minn=INF;
        for(j=1;j<tot;j++)
        {
            if(!vis[j]&&minn>dis[j])
            {
                minn=dis[j];
                key=j;
            }
        }
        if(minn==INF)
            break;
       vis[key]=1;
        for(j=1;j<tot;j++)
        {
            if(minn+p[key][j]<dis[j])
            {
                dis[j]=minn+p[key][j];
            }
        }
    }
    printf("%.0f\n",dis[2]*3.6/60);//由於老劉神奇的單位換算得到
    return 0;
}