POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)
POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 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
0 0 10000 1000
0 200 5000 200 7000 200 -1 -1
2000 600 5000 600 10000 600 -1 -1
Sample Output
21
Http
POJ:https://vjudge.net/problem/POJ-2502
NBUT:https://vjudge.net/problem/NBUT-1440
SCU:https://vjudge.net/problem/SCU-2186
Source
图论,最短路径
题目大意
给出家和学校的坐标以及若干条地铁线及地铁站,并给出人走路和坐地铁的速度,求从家到学校的最短时间。
解决思路
算法还是比较好想,就是直接跑最短路就可以,但是有些细节比较麻烦。
首先,只有相邻的地铁站可以通过地铁相连,其他的点都要计算欧几里得距离。
另外,最后输出答案只要整数部分,这点题目中并没有说明。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<stack>
#include<cmath>
using namespace std;
const int maxN=300;
const int maxM=maxN*maxN;
const double v1=10000.0/60.0;//人的速度,均统一转换成m/min单位
const double v2=40000.0/60.0;//地铁的速度
const int inf=2147483647;
class Pos//坐标的结构体
{
public:
int x,y;
};
int n,m;
class Graph//图
{
private:
int cnt;
int Head[maxN];
int Next[maxM];
int V[maxM];
double W[maxM];
bool instack[maxN];
stack<int> S;//听说spfa+stack快于spfa+queue
public:
double Dist[maxN];//距离
void init()
{
cnt=0;
memset(Head,-1,sizeof(Head));
memset(Next,-1,sizeof(Next));
}
void Add_Edge(int u,int v,double w)
{
cnt++;
Next[cnt]=Head[u];
V[cnt]=v;
W[cnt]=w;
Head[u]=cnt;
}
void spfa(int s)//Spfa
{
memset(Dist,127,sizeof(Dist));
memset(instack,0,sizeof(instack));
Dist[s]=0;
instack[s]=1;
S.push(s);
do
{
int u=S.top();
S.pop();
instack[u]=0;
for (int i=Head[u];i!=-1;i=Next[i])
{
if (Dist[V[i]]>Dist[u]+W[i])
{
Dist[V[i]]=Dist[u]+W[i];
if (instack[V[i]]==0)
{
instack[V[i]]=1;
S.push(V[i]);
}
}
}
}
while (!S.empty());
}
void OutEdge()//为了方便检查输出的边
{
for (int i=1;i<=n;i++)
{
for (int j=Head[i];j!=-1;j=Next[j])
{
cout<<i<<"->"<<V[j]<<' '<<W[j]<<endl;
}
cout<<endl;
}
}
};
Pos P[maxN];
Graph G;
int read();
inline double Dist(Pos A,Pos B);
istream &operator >> (istream &is,Pos &p)//方便读入,重载一下输入运算符
{
is>>p.x>>p.y;
return is;
}
int main()
{
cin>>P[1]>>P[2];
G.init();
n=2;
while (cin>>P[n+1])//输入处理,有些麻烦
{
n++;
int now=n;
Pos input;
for (int i=1;i<=n;i++)
{
double dist=Dist(P[n],P[i])/v1;
G.Add_Edge(n,i,dist);
G.Add_Edge(i,n,dist);
}
while (cin>>input)
{
if (input.x==-1)
break;
n++;
P[n]=input;
double dist=Dist(P[n],P[n-1])/v2;
G.Add_Edge(n,n-1,dist);
G.Add_Edge(n-1,n,dist);
for (int i=1;i<=n;i++)
{
//cout<<"Link:"<<i<<' '<<n<<endl;
double dist2=Dist(P[n],P[i])/v1;
G.Add_Edge(n,i,dist2);
G.Add_Edge(i,n,dist2);
}
}
}
//G.OutEdge();
G.spfa(1);
printf("%.0f\n",G.Dist[2]);
return 0;
}
int read()
{
int x=0;
int k=1;
char ch=getchar();
while (((ch>'9')||(ch<'0'))&&(ch!='-'))
ch=getchar();
if (ch=='-')
{
k=-1;
ch=getchar();
}
while ((ch>='0')&&(ch<='9'))
{
x=x*10+ch-48;
ch=getchar();
}
return x*k;
}
inline double Dist(Pos A,Pos B)
{
return sqrt((double)((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)));
}
按照我这个方法建出来的图会有部分边重复,但没有关系。
POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)的更多相关文章
- POJ 2502 Subway(迪杰斯特拉)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6692 Accepted: 2177 Descriptio ...
- POJ 2502 Subway (Dijkstra 最短+建设规划)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6689 Accepted: 2176 Descriptio ...
- POJ 2502 Subway
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4928 Accepted: 1602 Descriptio ...
- POJ 2502 - Subway Dijkstra堆优化试水
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...
- POJ 2502 Subway (最短路)
Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...
- (简单) POJ 2502 Subway,Dijkstra。
Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...
- POJ 2502 Subway-经过预处理的最短路
Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...
- Subway POJ 2502
题目链接: http://poj.org/problem?id=2502 题目大意: 你刚从一个安静的小镇搬到一个吵闹的大城市,所以你不能再骑自行车去上学了,只能乘坐地铁或者步行去上学.因为你不想迟到 ...
- Dijkstra+计算几何 POJ 2502 Subway
题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...
随机推荐
- 2017-2018-2 20155230《网络对抗技术》实验5:MSF基础应用
基础问题回答 用自己的话解释什么是exploit,payload,encode. exploit 就是运行该模块吧,在msf的模块中配置好各项属性后exploit一下就开始运行使用该模块了 paylo ...
- cocos2d-x学习记录6——自定义Button
cocos2d-x中封装CCMenuItem等相关按钮,但是有些时候需要自己封装按钮,这样能够更加灵活的实现对应功能. 自定义Button,需要重写OnEnter()和onExit()函数,并在对应函 ...
- [CF981F]Round Marriage[二分+霍尔定理]
题意 洛谷 分析 参考了Icefox 首先二分,然后考虑霍尔定理判断是否有完美匹配.如果是序列的话,因为这里不会出现 \(j<i,L(i)<L(j)\) 或者 \(j<i,R(i)& ...
- [CF1083D]The Fair Nut’s getting crazy[单调栈+线段树]
题意 给定一个长度为 \(n\) 的序列 \(\{a_i\}\).你需要从该序列中选出两个非空的子段,这两个子段满足 两个子段非包含关系. 两个子段存在交. 位于两个子段交中的元素在每个子段中只能出现 ...
- Egret(白鹭引擎)——“TypeError: Cannot read property 'asCom' of null”
前言 相信我,这个错误新手都不陌生:TypeError: Cannot read property 'asCom' of null 还有,一定要看我上一篇,哦不(人家应该是报了这个错,才找到看到这篇文 ...
- 命令行启用IIS Express
我们在调试WEB程序的时候可以把本地web程序挂载到本地IIS,然后访问程序,通过附加进程的方式(w3wp)来调试程序(个人非常喜欢的一种调试方式),还有一种比较传统的方式就是通过VS自带的F5来执行 ...
- ReactJS实用技巧(1):JSX与HTML的那些不同
在项目中使用ReactJS也已经有大半年了,收获很多也踩过不少坑.不想把这个系列写成抄书似的罗列,旨在总结些常用的技巧及常见的坑,以帮助初心者快速入门,想系统学习的同学还是多阅读文档. JSX本质上与 ...
- 菜鸟凉经(华为、firehome、大华)
面试通知都是前一天来的,准备的时间很少,所以表现也不是特别满意,来看面经吧: 华为一面(IT应用工程师): 1.自我介绍:(华为面试都是1对1,面前的是个温柔的小哥,挺放松的) 2.你主要会的it技术 ...
- 通过监控Nginx日志来实时屏蔽高频恶意访问的IP
目前在我的VPS上主要通过两种方式来限制ip的访问次数. 通过Nginx的limit_req配置来限制同一ip在一分钟内的访问次数 通过Nginx deny封禁一天内超过指定次数的ip(需要一个监控脚 ...
- alpha版发布
网站网址:http://doeverying.applinzi.com/