POJ

来源:互联网 发布:fx5u有什么编程软件 编辑:程序博客网 时间:2024/05/21 16:59

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.
说几个重要条件。
一条线路上相邻的两个站点才能用地铁直达
计算结果要四舍五入
建图时两点间可能有多条路径,取最小
输入的线路处理到文件结束,可用一次或两次while(~scanf())

在建完图后直接dijkstra就行,给出点数比较少200多就够用

#include <stdio.h>#include <string.h>#include <iostream>#include <vector>#include <queue>#include <string>#include <math.h>#include <stdlib.h>using namespace std;#define INF 0x3f3f3f3f#define mem(arr,a) memset(arr,a,sizeof(arr))#define N 500+5#define pow(a) ((a)*(a))#define walk 3.0/500.0#define bus 3.0/2000.0double cost[N][N];double d[N];int vis[N];struct node{    int x, y;};node vet[N];int n = 0;double dis(node a, node b){    return sqrt((double)pow(a.x - b.x) + (double)pow(a.y - b.y));}void init(){    for (int i = 0; i < N; i++){        for (int j = 0; j < N; j++){            cost[i][j] = INF;        }        cost[i][i] = 0;    }}void dijkstra(){    int i, j, v;    double min;    for (i = 0; i<n; i++){        vis[i] = 0;        d[i] = cost[0][i];    }    for (i = 0; i<n; i++){        min = INF;        for (j = 0; j<n; j++)        if (!vis[j] && d[j]<min){            min = d[j];            v = j;        }        vis[v] = 1;        for (j = 0; j<n; j++)        if (!vis[j] && d[j]>cost[v][j] + d[v])            d[j] = cost[v][j] + d[v];    }}int main(){    scanf("%d%d", &vet[n].x, &vet[n].y); n++;    scanf("%d%d", &vet[n].x, &vet[n].y); n++;    init();    while (~scanf("%d%d", &vet[n].x, &vet[n].y)){        n++;        int cnt = 1; int x, y;        while (~scanf("%d%d", &x, &y)){            if (x == -1 && y == -1)break;            vet[n].x = x, vet[n].y = y; n++; cnt++;        }        for (int i = n - cnt; i < n - 1; i++){            cost[i][i + 1] = cost[i + 1][i] = dis(vet[i], vet[i + 1])*bus;        }    }    for (int i = 0; i < n; i++){        for (int j = 0; j < i; j++){            double dist = dis(vet[i], vet[j])*walk;            if (dist < cost[i][j])cost[i][j] = cost[j][i] = dist;        }    }    dijkstra();    printf("%d\n", (int)(d[1] + 0.5));}
原创粉丝点击