L - Subway POJ - 2502
题意: 从 家出发步行与坐 subway 交替进行,问最小的时间花费,最终四舍五入答案为 整数
存图难
四舍五入:
- double b
int a = b + 0.5; - printf("%.0f", b);
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
const int maxn = 1e5+10;
int sx, sy, nx, ny;
struct node{
int x, y;
}no[maxn];
double getlen(int x1, int y1, int x2, int y2) {
return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
double dis[maxn];
int head[maxn], cnt;
bool vis[maxn];
struct edge {
int to, next;
double w;
}e[maxn];
void add(int u, int v, double w) {
e[++cnt].next = head[u];
e[cnt].to = v;
e[cnt].w = w;
head[u] = cnt;
}
typedef struct po{
int u;
double dist;
}cam;
bool operator < (const cam & a, const cam & b) {
return a.dist > b.dist;
}
void dijkstra() {
priority_queue<cam> heap;
cam s;
dis[1] = 0;
s.dist = 0; s.u = 1;
heap.push(s);
while(heap.size()) {
cam now = heap.top(); heap.pop();
if(vis[now.u]) continue;
vis[now.u] = 1;
for(int i = head[now.u]; i; i = e[i].next) {
int to = e[i].to;
if(dis[to] > now.dist + e[i].w) {
dis[to] = now.dist + e[i].w;
cam tmp; tmp.dist = dis[to]; tmp.u = to;
heap.push(tmp);
}
}
}
}
int main() {
// freopen("test.in", "r", stdin);
scanf("%d%d%d%d", &sx, &sy, &nx, &ny);
int cnt = 2, flag = 0; // 起点为1号, 终点为 cnt 号
while(~scanf("%d%d", &no[cnt].x, &no[cnt].y)) {
if(no[cnt].x == -1 && no[cnt].y == -1) {
flag = 0;
}
else {
if(!flag) flag = 1;
else {
double w = getlen(no[cnt-1].x, no[cnt-1].y, no[cnt].x, no[cnt].y)/4;
add(cnt-1, cnt, w); add(cnt, cnt-1, w);
}
cnt++;
}
}
no[1].x = sx; no[1].y = sy;
no[cnt].x = nx; no[cnt].y = ny;
for(int i = 2; i <= cnt; i++) {
for(int j = 1; j < i; j++) {
double w = getlen(no[i].x, no[i].y, no[j].x, no[j].y);
add(i, j, w); add(j, i, w);
}
}
for(int i = 1; i <= cnt; i++) dis[i] = 1e9+10;
dijkstra();
printf("%.0f\n", dis[cnt]*6/1000);
return 0;
}