http://poj.org/problem?id=2502
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7886 | Accepted: 2545 |
Description
Input
Output
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
题目大意:首先给你两个坐标,一个是你家里的坐标,一个是你学校的坐标,然后接下来有若干条地铁线,每条地铁线上有若干个站点,给出每个站点的坐标,有这些点,这些点当中有距离,这个距离的单位是米,现在告诉你走路是10km/h,做地铁的话是40km/h,问你从家里到学校所花费的最短时间(分钟)
解题思路:这个主要是卡的输入,只要将输入弄好了,构成一张图就好做了,然后需要注意的是,对于地铁线,你从1站到2站的速度是40km/h,但是不代表你从1站到3站的速度是40km/h,你必须经过2站才能到3站,然后我们构图的时候权值保存的是时间多少分钟(可能会有从a到b点有多个分钟,取最小的),这里单位换算也要注意下,然后就是直接模版了
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <stack> using namespace std; #define N 300 #define INF 0x3f3f3f3f #define met(a, b) memset (a, b, sizeof (a)) struct node { double x, y; }val[N]; double g[N][N], dist[N]; int vis[N], cnt; void Dij () { for (int i=0; i<=cnt; i++) dist[i] = INF; for (int i=1; i<=cnt; i++) { vis[i] = 0; dist[i] = min (g[1][i], dist[i]); } vis[1] = 1; dist[1] = 0; for (int i=1; i<=cnt; i++) { double minx = INF; int Index = 0; for (int j=1; j<=cnt; j++) if (!vis[j] && dist[j] < minx) minx = dist[Index = j]; vis[Index] = 1; for (int j=1; j<=cnt; j++) if (!vis[j] && dist[j] > dist[Index] + g[Index][j]) dist[j] = dist[Index] + g[Index][j]; } printf ("%.0f\n", dist[cnt]); } int main () { double x1, y1, x2, y2; while (scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2) != EOF) { for (int i=1; i<=N; i++) for (int j=1; j<=N; j++) { if (i==j) g[i][j] = 0; else g[i][j] = INF; } met (val, 0); val[1] = (node) {x1, y1}; cnt = 2; int k = 2; while (scanf ("%lf %lf", &val[k].x, &val[k].y) != EOF) { if (val[k].x ==-1 && val[k].y == -1) { cnt = k; continue; } if (cnt != k) g[k][k-1] = g[k-1][k] = min (g[k-1][k], sqrt((val[k-1].x-val[k].x)*(val[k-1].x-val[k].x)+(val[k-1].y-val[k].y)*(val[k-1].y-val[k].y))/40000.0*60); k++; } val[cnt] = (node) {x2, y2}; for (int i=1; i<=cnt; i++) for (int j=1; j<=cnt; j++) g[i][j] = min (g[i][j], sqrt((val[i].x-val[j].x)*(val[i].x-val[j].x)+(val[i].y-val[j].y)*(val[i].y-val[j].y))/10000.0*60); Dij(); } return 0; }