写题过程
- 这道题目的输入算是一个点了,有点晕一开始看到这题的时候,后面写着写着发现还行。
- 这题一定要注意单位,题目给的是xx(km/h),一定要转化为xx(m/s)。
- 还有就是输出的数据我wa了无数遍
思路
不难发现这就是一道最短路的问题,有地铁的路建立双向的快速路,没地铁的路建立步行的路,我们取时间作为边权值value,
地铁边读入边建立快速路,最后把所有的点建立步行的路,一定要注意双向边。
代码
//Powered by Ck 2020:04:05
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<vector>
#include<cmath>
using namespace std;
#define mp make_pair
typedef pair<double, double> PDD;
const int INF = 0x3f3f3f3f;
const int N1 = 210, N2 = 1e5 + 10;
int head[N1], to[N2], nex[N2], visit[N1], n, m, cnt;
double dis[N1], value[N2], x[N1], y[N1];
struct cmp {
bool operator () (const PDD & a, const PDD & b) const {
return a.second > b.second;
}
};
double get_time(double x1, double y1, double x2, double y2, double v) {
int len = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return len / v / 1000.0 * 60.0;
}
void add(int x, int y, double w) {
to[cnt] = y;
nex[cnt] = head[x];
value[cnt] = w;
head[x] = cnt++;
}
void Dijkstra() {
for(int i = 1; i <= n; i++) dis[i] = INF, visit[i] = 0;
priority_queue<PDD, vector<PDD>, cmp> q;
dis[1] = 0;
q.push(mp(1, 0));
while(!q.empty()) {
int temp = q.top().first;
q.pop();
if(visit[temp]) continue;
visit[temp] = 1;
for(int i = head[temp]; i; i = nex[i]) {
if(dis[to[i]] > dis[temp] + value[i]) {
dis[to[i]] = dis[temp] + value[i];
q.push(mp(to[i], dis[to[i]]));
}
}
}
printf("%.0lf
", dis[2] + 0.1);//这点我曾经尝试过.0lf;取ceil;(int)(dis[2] + 0.5);+0.4……
}
int main() {
// freopen("in.txt", "r", stdin);
double dx, dy, w;
scanf("%lf %lf %lf %lf", &x[1], &y[1], &x[2], &y[2]);
n = 2, cnt = 1;
while(scanf("%lf %lf", &dx, &dy) != EOF) {
n++;
x[n] = dx, y[n] = dy;
while(scanf("%lf %lf", &dx, &dy) && dx != -1) {
n++;
x[n] = dx, y[n] = dy;
w = get_time(x[n - 1], y[n - 1], x[n], y[n], 40);
add(n, n - 1, w);
add(n - 1, n, w);
}
}
for(int i = 1; i <= n; i++)
for(int j = i + 1; j <= n; j++) {
w = get_time(x[i], y[i], x[j], y[j], 10);
add(i, j, w);
add(j, i, w);
}
Dijkstra();
return 0;
}