L - Subway(最短路spfa)
L - Subway(最短路spfa)
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
思路
- 这一题难在输入和建图,,,,,
- 对于建图,我说一些需要注意的地方:
1.首先 起点要与所有的火车站 建了一条边(起点 -> 火车站)权值为人走过这段距离所用的时间
2.汽车 所有的火车站与终点 建了一条边 (火车站-> 终点)权值为人走过这段距离所用的时间
3. 这题一条最需要注意: 在任意一组 车站点之间,相邻的 站点之间 要建立双向边(权值为:火车的行走的时间)
4. 所有火车站之间应该 也应该建了 双向边,这个边的权值(时间),一定是人走过两个车站的时间
题解一
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define Pir pair<double, double>
const int maxm = 100005;
struct Pos
{
double x,y;
} pos[maxm];
int k = 2;
double Dis(Pos a, Pos b)
{
return sqrt( (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) );
}
struct Edge
{
int v;
double w;
int next;
} edge[maxm];
int head[maxm];
double dis[maxm];
int cnt = 0;
void Add(int u, int v, double w)
{
edge[++ cnt] = (Edge){ v, w, head[u]}; head[u] = cnt;
}
double Spfa(int s, int e)
{
int use[maxm];
for(int i = 0; i <= k; i ++)
dis[i] = INF, use[i] = 0;
dis[s] = 0;
queue<int> q;
q.push(s);
int u,v;
double w;
while(! q.empty())
{
u = q.front(); q.pop();
use[u] = 0;
for(int i = head[u]; i; i = edge[i].next)
{
v = edge[i].v;
w = edge[i].w;
if(dis[v] > dis[u] + w)
{
dis[v] = dis[u] + w;
if(! use[v])
{
q.push(v);
use[v] = 1;
}
}
}
}
return dis[e];
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
//freopen("T.txt","r",stdin);
cin >> pos[0].x >> pos[0].y >> pos[1].x >> pos[1].y;
double x,y;
while(cin >> x >> y)
{
pos[k].x = x; pos[k ++].y = y;
while(cin >> x >> y && (x!=-1 && y!=-1))
{
pos[k].x = x; pos[k].y = y;
Add(k - 1, k, Dis(pos[k-1], pos[k])/2000*3);
Add(k, k - 1, Dis(pos[k], pos[k-1])/2000*3);
k ++;
}
}
for(int i = 0; i < 2; i ++)
{
for(int j = 2; j < k-1; j ++)
{
if(i == 0)
Add(i, j, Dis(pos[i], pos[j])/500*3);
if(i == 1)
Add(j, i, Dis(pos[i], pos[j])/500*3);
}
}
for(int i = 0; i < k; i ++)
for(int j = i + 1; j < k; j ++)
{
Add(i, j, Dis(pos[i], pos[j])/500*3);
Add(j, i, Dis(pos[i], pos[j])/500*3);
}
Add(0, 1, Dis(pos[0], pos[1])/500*3);
cout << int( Spfa(0, 1) + 0.5) << endl;
return 0;
}
题解二(别人的)
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 205;
const double wsp = 10 * 1000 / 60;
const double ssp = 40 * 1000 / 60;
struct Node{
double x, y;
}node[MAXN];
struct ff{
int x, d;
ff(){}
ff( int a, double b ){ x = a; d = b; }
bool operator <( const ff & a )const{
return d > a.d;
}
};
int cnt;
double cost[MAXN][MAXN];
double dis[MAXN];
double gdis( int pre, int pos ){
double dx = node[pre].x - node[pos].x;
double dy = node[pre].y - node[pos].y;
return sqrt( dx * dx + dy * dy );
}
void dij(){
for( int i = 1; i < MAXN; i++ )
dis[i] = INF;
dis[1] = 0;
priority_queue<ff> Q;
Q.push( ff( 1, dis[1]) );
while( !Q.empty() ){
ff temp = Q.top(); Q.pop();
int x = temp.x;
if( temp.d > dis[x] ) continue;
for( int i = 1; i < cnt; i++ ){
if( dis[i] > dis[x] + cost[x][i] ){
dis[i] = dis[x] + cost[x][i];
Q.push( ff( i, dis[i] ) );
}
}
}
}
int main(){
ios::sync_with_stdio( false );
for( int i = 0; i < MAXN; i++ )
for( int j = 0; j < MAXN; j++ )
cost[i][j] = INF;
cin >> node[1].x >> node[1].y >> node[2].x >> node[2].y;
cnt = 3;
while( cin >> node[cnt].x >> node[cnt].y ){
cnt++;
while( cin >> node[cnt].x >> node[cnt].y, !( node[cnt].x == -1 && node[cnt].y == -1 ) ){
cost[cnt][cnt - 1] = cost[cnt - 1][cnt] = gdis( cnt - 1, cnt ) / ssp;
cnt++;
}
}
for( int i = 1; i < cnt - 1; i++ ){
cost[i][i] = 0;
for( int j = i + 1; j < cnt; j++ ){
cost[i][j] = cost[j][i] = min( cost[i][j], gdis( i, j ) / wsp );
}
}
dij();
cout << int( dis[2] + 0.5 );
return 0;
}
L - Subway(最短路spfa)的更多相关文章
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- 【wikioi】1269 匈牙利游戏(次短路+spfa)
http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...
- 次短路[SPFA]
Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样, ...
- HDU1874 最短路 SPFA
最短路 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 24 Solved: 17 [Submit][Status][Web Board] Descr ...
- (poj 2502) Subway 最短路
题目链接: 题意:在一个城市里有许多地铁,现在你知道每条地铁的起点 终点与停站点的坐标,知道我们的起始坐标与终点坐标,问加上走路最快到达终点的时间是多少? 方法:求出任意两点的车速时间与步行时间,再 ...
- 30-算法训练 最短路 spfa
http://lx.lanqiao.cn/problem.page?gpid=T15 算法训练 最短路 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个n个顶点, ...
- 有限制的最短路spfa+优先队列
poj1724 ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10751 Accepted: 3952 De ...
- 蓝桥杯 最短路 spfa
问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个 ...
- hdu 2962 Trucking (二分+最短路Spfa)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others ...
随机推荐
- js 面向对象中,定义一个函数的过程
定义一个函数做的两件事:1: 实例化一个Function对象:2: 实例化一个Object对象,并给该函数扩展prototype属性指向这个构造函数 大致过程如图所示: 每一种引用类型(函数,对象,数 ...
- python框架Django实战商城项目之工程搭建
项目说明 该电商项目类似于京东商城,主要模块有验证.用户.第三方登录.首页广告.商品.购物车.订单.支付以及后台管理系统. 项目开发模式采用前后端不分离的模式,为了提高搜索引擎排名,页面整体刷新采用j ...
- 快速排序python实现总结
背景:数据结构与算法是IT相关的工程师一直以来的基础考察重点,很多经典书籍都是用c++或者java来实现,出于对python编码效率的喜爱,于是取search了一下python的快排实现,发现大家写的 ...
- Adobe Premiere Pro 2020破解教程
首先官网下载Adobe Creative Cloud,安装完之后使用它继续安装Pr.注意在安装之前,点击文件→首选项,先设置一下你的安装路径,没有设置则默认安装在C盘. 接着下载网上良心博主推荐的破解 ...
- 《一步步成为 Hacker_Day 01》
环境搭建 传统运行模式 - 一台机器同时只能运行一个操作系统 |:----------|----------:| | 应用程序 | 应用程序 | |:----------|----------:| | ...
- 02 layui 下载和搭建环境
Layui官方网站 官方网站:https://www.layui.com/ 下载地址:https://res.layui.com/static/download/layui/layui-v2.5.5. ...
- NLP(二十五)实现ALBERT+Bi-LSTM+CRF模型
在文章NLP(二十四)利用ALBERT实现命名实体识别中,笔者介绍了ALBERT+Bi-LSTM模型在命名实体识别方面的应用. 在本文中,笔者将介绍如何实现ALBERT+Bi-LSTM+CRF ...
- Redis系列三 - 缓存雪崩、击穿、穿透
前言 从学校出来,做开发工作也有一定时间了,最近有想系统地进一步深入学习,但发现基础知识不够扎实,故此来回顾基础知识,进一步巩固.加深印象. 最初开始接触编程时,总是自己跌跌撞撞.不断摸索地去学习,再 ...
- windows上用putty从linux上下载文件
我之前使用putty都是直接从网上下的putty.exe,其实如果想下载windows的mis二进制文件,系统安装的话会包含,pscp.psftp.puttygen等一系列的文件. 今天下从服务器上, ...
- 【剑指Offer】简单部分每日五题 - Day 1
今天开始更新leetcode上<剑指Offer>的题解,先从简单难度开始.预计按下列顺序更新: 简单难度:每日5题 中等难度:每日3题 困难难度:每日1题 17 - 打印从1到最大的n位数 ...