题目链接

题意

给起点和终点的坐标,然后给出多条地铁每一站的坐标,每站地铁只能到相邻的地铁站,地铁的速度是40km/h,人行走的速度是10km/h,求起点到终点的最小时间(给出的坐标单位是m,最后求的时间单位是分钟)

解题思路

这题关键点在与建图,把图建好后跑dijkstra就很简单了,之前一直wa是想复杂了,以为要考虑起点和终点在地铁站的情况和不同线路的地铁站相交的情况,后来想不需要这么麻烦,同一条线路相邻的40,不同的10就可以了,相交的话,以10km/h走时间花费是0。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<vector>
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<set>
#include<cmath>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<double,int> pii;
typedef pair<int,pii> PII;
const int inf = 1<<30;
const int maxn = 1e5+5;
struct Node
{
int x,y;
Node(){}
Node(int a,int b):x(a),y(b){}
};
Node beg,en;
vector<Node> node;
Node D[maxn];
struct Edge
{
int from,to;
double val;
double cost;
};
vector<Edge> G[maxn];

double getval(Node &a,Node& b)
{
return sqrt((double)(abs(a.x-b.x)*abs(a.x-b.x)+abs(a.y-b.y)*abs(a.y-b.y)));
}
double getcost(double cnt,bool issubway)
{
double ans = 0;
if(issubway){
ans = cnt/40000 * 60;
}else{
ans = cnt/10000 * 60;
}
return ans;
}

double dis[maxn];
bool vis[maxn];
int cnt = 0;

double dijkstra()
{
memset(vis,0,sizeof(vis));
fill(dis,dis+maxn,inf);
priority_queue<pii,vector<pii>,greater<pii> > Q;
Q.push(pii(0,cnt));
while(!Q.empty()){
pii t = Q.top();
Q.pop();
int u = t.second;
double d = t.first;
if(vis[u])continue;
vis[u] = 1;
for(int i=0;i<G[u].size();i++){
Edge e = G[u][i];
if(e.cost + d < dis[e.to]){
dis[e.to] = e.cost + d;
Q.push(pii(dis[e.to],e.to));
}
}
}
return dis[cnt+1];
}

int main(int argc, char const *argv[])
{
scanf("%d%d",&beg.x,&beg.y);
scanf("%d%d",&en.x,&en.y);
int x,y;
int k = 0;
Edge e;
while(~scanf("%d%d",&x,&y)){
if(x==-1&&y==-1){
for(int i=k;i<cnt-1;i++){
e.val = getval(D[i], D[i+1]);
e.cost = getcost(e.val, 1);
e.from = i;
e.to = i+1;
G[e.from].push_back(e);
swap(e.from, e.to);
G[e.from].push_back(e);
}
k = cnt;
continue;
}
D[cnt] = Node(x,y);
cnt++;
}
D[cnt] = beg;
D[cnt+1] = en;
for(int i=0;i<cnt+2;i++){
for(int j=i+1;j<cnt+2;j++){
e.val = getval(D[i], D[j]);
e.cost = getcost(e.val, 0);
e.from = i;
e.to = j;
G[e.from].push_back(e);
swap(e.from, e.to);
G[e.from].push_back(e);
}
}

double ans = dijkstra();
printf("%d\n",(int)(ans+0.5));
return 0;
}