Subway

Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9942 Accepted: 3235

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

spfa()
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 1e6+10;
const int INF = 0x3f3f3f3f;
int cnt,head[maxn],vist[maxn];
double dist[maxn];
struct node{
    double x,y;
}point[maxn];
struct Edge{
    int to,next;
    double w;
}Edge[maxn];
double Distance(node a,node b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void ADD(int u,int v,double w){
    Edge[cnt].to = v;
    Edge[cnt].w = w;
    Edge[cnt].next = head[u];
    head[u] = cnt++;
}
void spfa(int n){
    for(int i = 1; i < n; i++){
        dist[i] = INF*1.0;
        vist[i] = 0;
    }
    dist[1] = 0;
    vist[1] = 1;
    queue<int>Q;
    Q.push(1);
    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        vist[u] = 0;
        for(int i = head[u]; ~i; i = Edge[i].next){
            int v = Edge[i].to;
            if(dist[v] > dist[u]+Edge[i].w){
                dist[v] = dist[u]+Edge[i].w;
                if(!vist[v]){
                    vist[v] = 1;
                    Q.push(v);
                }
            }
        }
    }
    printf("%d\n",int(dist[2]+0.5));
}
int main(){
    double v1 = 40000.0/60;
    double v2 = 10000.0/60;
    memset(head,-1,sizeof(head));
    cnt = 0;
    scanf("%lf %lf %lf %lf",&point[1].x,&point[1].y,&point[2].x,&point[2].y);
    ADD(1,2,(Distance(point[1],point[2])/v2));
    ADD(2,1,(Distance(point[1],point[2])/v2));
    int flag = 1;
    int k = 3;
    while(~scanf("%lf %lf",&point[k].x,&point[k].y)){
        if(point[k].x == -1 && point[k].y == -1)
             flag = 1;
        else{
            if(!flag){
                ADD(k-1,k,(Distance(point[k-1],point[k])/v1));
                ADD(k,k-1,(Distance(point[k-1],point[k])/v1));
            }
            else
                flag = 0;
            k++;
        }
    }
    for(int i = 1; i < k; i++)
        for(int j = 1; j <= i; j++){
            ADD(i,j,(Distance(point[i],point[j])/v2));
            ADD(j,i,(Distance(point[i],point[j])/v2));
        }
    spfa(k);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值