Subway
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3679 | Accepted: 1171 |
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.
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
Waterloo local 2001.09.22
//Dijkstra算法
//主要是建图
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 202;
#define INF 1<<30
struct point {
double x, y;
}pnt[N], s, e;
double map[N][N], dist[N];
bool visited[N];
double cal_time_walk(struct point p1, struct point p2){
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return sqrt(x*x+y*y)*0.06/10;
}
double cal_time_subway(struct point p1, struct point p2) {
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return sqrt(x*x+y*y)*0.06/40;
}
void dijkstra(int v, int n) {
int i, j, k;
double min;
if(v < 0 || v > n) return;
for(i = 0; i <= n; i++) {
dist[i] = map[v][i];
visited[i] = false;
}
dist[v] = 0.0; visited[v] = true;
for(i = 0; i < n; i++) {
min = INF;
k = v;
for(j = 0; j <= n; j++) {
if((!visited[j]) && (dist[j] < min)) {
min = dist[j];
k = j;
}
}
visited[k] = true;
for(j = 0; j <= n; j++ ) {
if((!visited[j]) && (map[k][j] < INF)) {
double newdist = dist[k] + map[k][j];
if(newdist < dist[j]) {
dist[j] = newdist;
}
}
}
}
}
int main()
{
int i, j, cnt = 1, pre = 0;
double x, y, tmp;
for(i = 0; i <= N; i++) {
for(j = 0; j <= N; j++)
map[i][j] = INF;
}
scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
while(scanf("%lf%lf", &x, &y) != EOF) {
do {
pnt[cnt].x = x;
pnt[cnt].y = y;
if(cnt-1 > pre) {
tmp = cal_time_subway(pnt[cnt-1], pnt[cnt]);
map[cnt-1][cnt] = map[cnt][cnt-1] = tmp;
}
cnt += 1;
}while(scanf("%lf%lf", &x, &y) && (x != -1 || y != -1));
pre = cnt - 1;
}
for(i = 1; i < cnt; i++) {
for(j = i+1; j < cnt; j++)
if(map[i][j] == INF) {
tmp = cal_time_walk(pnt[i], pnt[j]);
map[i][j] = map[j][i] = tmp;
}
}
for(i = 1; i < cnt; i++)
map[i][0] = map[0][i] = cal_time_walk(s, pnt[i]);
for(i = 1; i < cnt; i++)
map[i][cnt] = map[cnt][i] = cal_time_walk(e, pnt[i]);
map[0][cnt] = map[cnt][0] = cal_time_walk(s, e);
dijkstra(0, cnt);
printf("%.f\n", dist[cnt]);
return 0;
}
//Dijkstra算法
//主要是建图
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 202;
#define INF 1<<30
struct point {
double x, y;
}pnt[N], s, e;
double map[N][N], dist[N];
bool visited[N];
double cal_time_walk(struct point p1, struct point p2){
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return sqrt(x*x+y*y)*0.06/10;
}
double cal_time_subway(struct point p1, struct point p2) {
double x = p1.x - p2.x;
double y = p1.y - p2.y;
return sqrt(x*x+y*y)*0.06/40;
}
void dijkstra(int v, int n) {
int i, j, k;
double min;
if(v < 0 || v > n) return;
for(i = 0; i <= n; i++) {
dist[i] = map[v][i];
visited[i] = false;
}
dist[v] = 0.0; visited[v] = true;
for(i = 0; i < n; i++) {
min = INF;
k = v;
for(j = 0; j <= n; j++) {
if((!visited[j]) && (dist[j] < min)) {
min = dist[j];
k = j;
}
}
visited[k] = true;
for(j = 0; j <= n; j++ ) {
if((!visited[j]) && (map[k][j] < INF)) {
double newdist = dist[k] + map[k][j];
if(newdist < dist[j]) {
dist[j] = newdist;
}
}
}
}
}
int main()
{
int i, j, cnt = 1, pre = 0;
double x, y, tmp;
for(i = 0; i <= N; i++) {
for(j = 0; j <= N; j++)
map[i][j] = INF;
}
scanf("%lf%lf%lf%lf", &s.x, &s.y, &e.x, &e.y);
while(scanf("%lf%lf", &x, &y) != EOF) {
do {
pnt[cnt].x = x;
pnt[cnt].y = y;
if(cnt-1 > pre) {
tmp = cal_time_subway(pnt[cnt-1], pnt[cnt]);
map[cnt-1][cnt] = map[cnt][cnt-1] = tmp;
}
cnt += 1;
}while(scanf("%lf%lf", &x, &y) && (x != -1 || y != -1));
pre = cnt - 1;
}
for(i = 1; i < cnt; i++) {
for(j = i+1; j < cnt; j++)
if(map[i][j] == INF) {
tmp = cal_time_walk(pnt[i], pnt[j]);
map[i][j] = map[j][i] = tmp;
}
}
for(i = 1; i < cnt; i++)
map[i][0] = map[0][i] = cal_time_walk(s, pnt[i]);
for(i = 1; i < cnt; i++)
map[i][cnt] = map[cnt][i] = cal_time_walk(e, pnt[i]);
map[0][cnt] = map[cnt][0] = cal_time_walk(s, e);
dijkstra(0, cnt);
printf("%.f\n", dist[cnt]);
return 0;
}

本文探讨了在城市环境中,通过结合地铁与步行的方式进行通勤路线规划,以确保准时到达学校。文章详细介绍了如何计算最优路径,包括行走时间和地铁旅行时间的计算方法,并使用Dijkstra算法解决最短路径问题。通过实例输入输出,展示了求解过程及最终计算结果。
844

被折叠的 条评论
为什么被折叠?



