天天看点

POJ 2502 (Dij Floyd Ballman-Ford)

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

题意:

    你从家往学校赶,可以用步行和乘坐地铁这两种方式,步行速度为10km/h,乘坐地铁的速度为40KM/h。

    输入数据的第一行数据会给你起点和终点的x和y的坐标。然后会给你数目不超过200的双向地铁线路的站点,

    你可以从一个站点乘坐地铁到下一个站点,你可以在同一线路上乘坐地铁经过不同的站点,在不同线路的站点之间,

    你只能用步行的方式。让你求利用你可以利用的方式,求解到校花费的最短时间。

解题思路:

       求从家到学校的最短时间,即求最短路的问题。但必须注意:输入问题:(求两站之间的时间时,注意变量何时进行sqrt,灵活使用变量)

       方法一:Dijsktra   map[][],dis[],vis[];

       方法二:Floyd  map[][];

       方法三:Ballman-Ford  结构体代替数组;

注意:循环变量数值的开始值是0 还是 1;

//Dijkstra
#include<iostream>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const double INF=100000000.0;
bool v[205];
double d[205];
double map[205][205];
int n;
struct F
{
    double x,y;
} p[205];
void Dijkstra()
{
    int i,j;
    memset(v,0,sizeof(v));
    v[0]=true;
    for(i=0; i<n; i++)
        d[i]=map[0][i];
    for(i=1; i<n; i++)
    {
        double min=INF;
        int x;
        for(j=0; j<n; j++)
            if(!v[j]&&d[j]<min)
            {
                min=d[j];
                x=j;
            }
        v[x]=true;
        for(j=0; j<n; j++)
            if(!v[j])
            {
                if(d[x]+map[x][j]<d[j])
                    d[j]=d[x]+map[x][j];
            }
    }
}
int main()
{
    int i,j;
    scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y);
    n=2;
    int flag=0;
    memset(map,0,sizeof(map));
    while(scanf("%lf%lf",&p[n].x,&p[n].y)!=EOF)
    {
        if(p[n].x==-1&&p[n].y==-1)
        {
            flag=0;
            continue;
        }
        if(flag)
            map[n][n-1]=map[n-1][n]=(sqrt((p[n].x-p[n-1].x)*(p[n].x-p[n-1].x)+(p[n].y-p[n-1].y)*(p[n].y-p[n-1].y)))/40000.0;
        n++;
        flag=1;
    }
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            if(i!=j&&map[i][j]==0.0)
                map[i][j]=map[j][i]=(sqrt((p[j].x-p[i].x)*(p[j].x-p[i].x)+(p[j].y-p[i].y)*(p[j].y-p[i].y)))/10000.0;
    Dijkstra();
    double  sum=60.0*d[1];
    printf("%0.0lf\n",sum);
    return 0;
}
           
//Floyd
#include<iostream>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const double INF=100000000.0;
double map[205][205];
int n;
struct F
{
    double x,y;
} p[205];
void Floyd()
{
    int i,j,k;
    for(k=0; k<n; k++)
        for(i=0; i<n; i++)
            for(j=0; j<n; j++)
            {
                if(map[i][k]!=-1&&map[k][j]!=-1)
                {
                    map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
                }
            }
}
int main()
{
    int i,j;
    scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y);
    n=2;
    int flag=0;
    memset(map,0,sizeof(map));
    while(scanf("%lf%lf",&p[n].x,&p[n].y)!=EOF)
    {
        if(p[n].x==-1&&p[n].y==-1)
        {
            flag=0;
            continue;
        }
        if(flag)
            map[n][n-1]=map[n-1][n]=(sqrt((p[n].x-p[n-1].x)*(p[n].x-p[n-1].x)+(p[n].y-p[n-1].y)*(p[n].y-p[n-1].y)))/40000.0;
        n++;
        flag=1;
    }
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
            if(i!=j&&map[i][j]==0.0)
                map[i][j]=map[j][i]=(sqrt((p[j].x-p[i].x)*(p[j].x-p[i].x)+(p[j].y-p[i].y)*(p[j].y-p[i].y)))/10000.0;
    Floyd();
    double  sum=60.0*map[0][1];  // map[][];
    printf("%0.0lf\n",sum);
    return 0;
}
           
//Ballman-Ford
#include<iostream>
#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
const double INF=100000000.0;
int v[310][310];
double dis[90100];
int n,sum;
struct F
{
    double x,y;
} p[310];
struct FF
{
    int u,v;
    double w;
} e[90100];
void Ford()
{
    int i,j;
    for(i=0; i<n; i++)
        dis[i]=INF;
    dis[0]=0;
    for(i=0; i<n-1; i++)
        for(j=0; j<sum; j++)
        {
            int t1=e[j].u;
            int t2=e[j].v;
            if(dis[t2] > dis[t1] + e[j].w && dis[t1] < INF)
                dis[t2]=dis[t1]+e[j].w;
        }
}
int main()
{
    int i,j;
    scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y);
    n=2;
    int flag=0;
    sum=0;
    double temp;
    memset(v,0,sizeof(v));
    while(scanf("%lf%lf",&p[n].x,&p[n].y)!=EOF)
    {
        if(p[n].x==-1&&p[n].y==-1)
        {
            flag=0;
            continue;
        }
        if(flag)
        {
            temp=(sqrt((p[n].x-p[n-1].x)*(p[n].x-p[n-1].x)+(p[n].y-p[n-1].y)*(p[n].y-p[n-1].y)))/40000.0;
            e[sum].u=n;
            e[sum].v=n-1;
            e[sum++].w=temp;
            v[n][n-1]=1;
            e[sum].u=n-1;
            e[sum].v=n;
            e[sum++].w=temp;
            v[n][n-1]=1;
        }
        n++;
        flag=1;
    }
    for(i=0; i<n; i++)
        for(j=0; j<n; j++)
        {
            if(v[i][j]==0)
            {
                temp=(sqrt((p[j].x-p[i].x)*(p[j].x-p[i].x)+(p[j].y-p[i].y)*(p[j].y-p[i].y)))/10000.0;
                e[sum].u=i;
                e[sum].v=j;
                e[sum++].w=temp;
                v[i][j]=1;
                e[sum].u=j;
                e[sum].v=i;
                e[sum++].w=temp;
                v[j][i]=1;
            }
        }
    Ford();
    double  sum=60.0*dis[1];
    printf("%0.0lf\n",sum);
    return 0;
}
           

继续阅读