L - Subway POJ - 2502

L - Subway POJ - 2502

题意: 从 家出发步行与坐 subway 交替进行,问最小的时间花费,最终四舍五入答案为 整数
存图难

四舍五入:

  1. double b
    int a = b + 0.5;
  2. 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;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值