最短路径题集2

来源:互联网 发布:赢时胜软件笔试题 编辑:程序博客网 时间:2024/05/18 22:45

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;}
原创粉丝点击