最短路径题集2

POJ 2502

原题链接: http://poj.org/problem?id=2502
大意:一个同学从家到学校,有几条地铁,给出家和学校的位置坐标和每条地铁线上的车站坐标,同学步行速度和地铁运行速度也给出。求同学从家到学校的最短时间。
原题中只有200个车站,所以直接用矩阵存储就可以。然后重点是如何建立图结构。车站与车站之间,必须是在一条地铁线上才想通。每有一个地铁站,就要计算家和学校步行到地铁站的时间,也作为一个距离。建图之后就是Dijkstra算法了。
我原来想存储边的信息,但是这样在本题中过于麻烦。(下面写了部分代码,还没写Dijkstra,)

#include <iostream>
#include <algorithm>
#include <cstring>
#include "stdio.h"
#include <math.h>
using namespace std;
struct edge{
    int x1, y1;
    int x2, y2;
    float t;
}a[40000];
int main(){
    int startx, starty, endx, endy;
    scanf("%d%d%d%d", &startx, &starty, &endx, &endy);
    int num = 0;
    int x1 = -1, y1 = -1, x2 = -1, y2 = -1;
    while ((scanf("%d%d", &x2, &y2)) != EOF){//这个题,建边是个大问题
        if (x1 == -1 && y1 == -1) {
            x1 = x2; y1 = y2;   
            a[num].x1 = startx; a[num].y1 = starty; a[num].x2 = x2; a[num].y2 = y2;
            a[num++].t = (float)(sqrt((startx - x2)*(startx - x2) + (starty - y2)*(starty - y2))) * 60 / 1000 / 10;
            a[num].x1 = x2; a[num].y1 = y2; a[num].x2 = endx; a[num].y2 = endy;
            a[num++].t = (float)(sqrt((endx - x2)*(endx - x2) + (endy - y2)*(endy - y2))) * 60 / 1000 / 10;
            continue;
        }
        a[num].x1 = x1;     a[num].y1 = y1;
        a[num].x2 = x2;      a[num].y2 = y2;
        a[num++].t = (float)(sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)))*60/1000/40;

        a[num].x1 = startx; a[num].y1 = starty; a[num].x2 = x2; a[num].y2 = y2;
        a[num++].t = (float)(sqrt((startx - x2)*(startx - x2) + (starty - y2)*(starty - y2))) * 60 / 1000 / 10;
        a[num].x1 = x2; a[num].y1 = y2; a[num].x2 = endx; a[num].y2 = endy;
        a[num++].t = (float)(sqrt((endx - x2)*(endx - x2) + (endy - y2)*(endy - y2))) * 60 / 1000 / 10;
        x1 = x2;   y1 = y2;
    }//处理完输入

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值