【建模】【最短路】
2015-08-24 11:45
302 查看
Subway
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.
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
Sample Output
Source
Waterloo local 2001.09.22
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7252 | Accepted: 2362 |
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.
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
#include <iostream> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <list> #include <map> #include <set> #include <string> #include <cstdlib> #include <cstdio> #include <algorithm> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define mp push_back const int maxn = 310; struct Node { double x,y; }node[maxn]; double g[maxn][maxn]; double d[maxn]; bool ok[maxn]; int n; double dis(int a,int b) { return sqrt((node[a].x-node[b].x)*(node[a].x-node[b].x) + (node[a].y - node[b].y)*(node[a].y-node[b].y)); } double dij() { for(int i=1;i<=n;i++) { d[i] = 0x3f3f3f3f; ok[i] = false; } d[1] = 0; for(int i=0;i<n-1;i++) { int mins = 0x3f3f3f3f; int p = -1; for(int j=1;j<=n;j++) { if(!ok[j] && d[j] < mins) { mins = d[j]; p = j; } } if(p == -1) break; ok[p] = true; for(int j=1;j<=n;j++) { if(!ok[j] && d[j] > d[p] + g[j][p]) { d[j] = d[p] + g[j][p]; } } } return d[2]; } int main() { double v1 = 10000.0/60; double v2 = 40000.0/60; scanf("%lf%lf%lf%lf",&node[1].x,&node[1].y,&node[2].x,&node[2].y); { int tx,ty; n = 2; bool beg = true; for(int i=0;i<maxn;i++) { for(int j=0;j<maxn;j++) { if(i == j) g[i][j] = 0; else g[i][j] = 0x3f3f3f3f; } } while(scanf("%d%d",&tx,&ty)!=EOF) { if(tx == -1 && ty == -1) { beg = true; continue; } n ++; node .x = tx; node .y = ty; if(!beg) { g[n-1] = g [n-1] = min(g [n-1],dis(n,n-1)/v2); } beg = false; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { g[i][j] = min(g[i][j],dis(i,j)/v1); } } printf("%d\n",int(0.5+dij())); } return 0; }
相关文章推荐
- JavaScript中,本周、上周、本月、上月日期判断
- POJ 1068 Parencodings 模拟 难度:0
- 生成大小为100的数组,从1到100,随机插入,不连续,也不重复[C#]
- 粒子
- 【DATAGUARD】物理dg在主库丢失归档文件的情况下的恢复(七)
- 在C语言中比较两个字符串是否相等的方法
- POJ 1410 Intersection(判断线段和矩形是否相交)
- X-code注释插件VVDocument工具的安装
- 数据结构之-堆
- Topcoder SRM660,DIV1 250,找准突破口,暴力
- html-firefox与IE对javascript和CSS的区别(浏览器兼容)
- pdf转换成txt的转换方法介绍
- webpy学习之serving images
- Rendering Linear lighting and color
- 色彩校正中的 gamma 值是什么?
- JAVA I/O使用方法
- SharePoint 2013 IT Professional——如何部署My Site
- [教程]DLL注入的方法及通讯[2009-8-14]
- intel编译器编译cp2k
- TCP 出现大量 LAST_ACK 导致apache不能重启解决小结