POJ 2502 Subway 单源点最短路模板


                                                                                                                                   Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5917 Accepted: 1915

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

这题输入麻烦,WA了好多发的原因竟然又是建图建成了有向图,调了一个多小时 大哭,下次一定得吸取教训。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<ctype.h>
# pragma comment (linker,"/STACK:16777216")

using namespace std;

const int MAXN = 220;
const int INF  = 2100000000;

double sx, sy, ex, ey;

int n;
double mat[MAXN][MAXN];
double lowdis[MAXN];
bool vis[MAXN];
class Point
{
    public:

    double x, y;
};
Point line[MAXN];
Point point[MAXN];
int cnt;

void initial()
{
    for(int i = 0; i < MAXN; i++)
    {
        for(int j = 0; j < MAXN; j++)
        mat[i][j] = -1;
    }
    memset(vis, 0, sizeof(vis));


}

void caldis(int src, int end, Point a, Point b)
{
    double x1 = a.x, y1 = a.y, x2 = b.x, y2 = b.y;

    mat[src][end] = mat[end][src]= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))/(40.0/3.6);
}

double caldis(Point a, Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)) /(10.0/3.6);
}

void calleftdis()
{
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(mat[i][j] < 0)
            {
                mat[i][j] = mat[j][i]= caldis(point[i], point[j]);
            }
        }
    }
}

double spfa()
{
    queue<double> q;

    for(int i = 1; i <= n; i++)
    {
        q.push(i);
        vis[i] = 1;
    }
    for(int i = 1; i <= n; i++) lowdis[i] = mat[1][i];

    while(!q.empty())
    {
        int cur = q.front();
        q.pop();
        vis[cur] = 0;

        for(int i = 1; i <= n; i++)
        {
            if(lowdis[cur] + mat[cur][i] < lowdis[i])
            {
                lowdis[i] = lowdis[cur] + mat[cur][i];
                if(!vis[i])
                q.push(i);
            }
        }
    }

    return lowdis[n];
}

int main()
{
    //freopen("C:/Users/zts/Desktop/in.txt", "r", stdin);
    n = 1;

    while(cin >> sx >> sy >> ex >> ey)
    {
        double x, y;
        cnt = 0;
        initial();
        while(cin >> x >> y)
        {
            if(x == -1 && y == -1) { cnt = 0; continue;}

            line[cnt].x = x;
            line[cnt].y = y;
            n++;

            if(cnt >= 1)
                caldis(n-1, n, line[cnt-1], line[cnt]);

            point[n].x = x;
            point[n].y = y;
            cnt++;
        }
        n++;
        point[1].x = sx; point[1].y = sy;
        point[n].x = ex; point[n].y = ey;

        calleftdis();

        int temp = (int)(spfa()/60.0+0.5);
        printf("%d\n", temp);

    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值