首页 > 代码库 > poj2502Subway(SPFA)

poj2502Subway(SPFA)

题目链接:

huangjing

分析:

首先说我做这题在哪里出现误区。。

【1】 首先一条线路上的地铁站只有相邻的两站可以到达,比如一站和3站就不能直接到达,所以建边的时候应该用步行建边。。。。还有就是数据给的同一条线路上的地铁站的纵坐标都相同,所以比较误导人。。。
【2】最后的结果要四舍五入,就是精度的问题要注意。。。
【3】还有一个小知识  double类型的不能初始化为-1。。。
这个题没a出来,哎  太忧伤了。。。

题目:
Language:
Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6123 Accepted: 1991

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<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;

const int maxn=200+10;
double dis[maxn],gra[maxn][maxn];
bool vis[maxn];
int cal;

struct Point
{
    int x,y;
}point[maxn];

queue<int>Q;

double caldistance(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}


void Spfa()
{
    while(!Q.empty())  Q.pop();
    for(int i=1;i<=cal;i++)
    {
        dis[i]=INF*1.0;
        vis[i]=false;
    }
    dis[1]=0;
    vis[1]=true;
    Q.push(1);
    while (!Q.empty())
    {
        int temp=Q.front();
        Q.pop();
        vis[temp]=false;
        for(int i=1;i<=cal;i++)
        {
            if (dis[temp]+gra[temp][i]<dis[i])
            {
                dis[i]=dis[temp]+gra[temp][i];
                if(!vis[i])
                {
                    Q.push(i);
                    vis[i]=true;
                }
            }
        }
    }
}

int main()
{
    int u,v;
    int last_point;
    double v1=40000.0/60;
    double v2=10000.0/60;
    while(~scanf("%d%d%d%d",&point[1].x,&point[1].y,&point[2].x,&point[2].y))
    {
        cal=2,last_point=-1;
        memset(gra,0,sizeof(gra));
        while(scanf("%d%d",&u,&v)==2)
        {
            if(u==-1&&v==-1)
            {
                last_point=-1;
                continue;
            }
            if(last_point==-1)
            {
                point[++cal].x=u,
                point[cal].y=v;
                last_point=cal;
            }
            else
            {
                point[++cal].x=u;
                point[cal].y=v;
                gra[last_point][cal]=gra[cal][last_point]=caldistance(point[last_point],point[cal])/v1;
                last_point=cal;
            }
        }
        for(int i=1;i<cal;i++)
            for(int j=i+1;j<=cal;j++)
                if(gra[i][j]==0)
                        gra[i][j]=gra[j][i]=caldistance(point[i],point[j])/v2;
        Spfa();
        printf("%d\n",int(dis[2]+0.5));//四舍五入
    }
    return 0;
}


poj2502Subway(SPFA)