51nod--1459 迷宫游戏 (dijkstra)
1459 迷宫游戏
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注
你来到一个迷宫前。该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数。还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定了你的起点和终点房间,你首要目标是从起点尽快到达终点,在满足首要目标的前提下,使得你的得分总和尽可能大。现在问题来了,给定房间、道路、分数、起点和终点等全部信息,你能计算在尽快离开迷宫的前提下,你的最大得分是多少么?
Input
第一行4个整数n (<=500), m, start, end。n表示房间的个数,房间编号从0到(n - 1),m表示道路数,任意两个房间之间最多只有一条道路,start和end表示起点和终点房间的编号。
第二行包含n个空格分隔的正整数(不超过600),表示进入每个房间你的得分。
再接下来m行,每行3个空格分隔的整数x, y, z (0< z <=200)表示道路,表示从房间x到房间y(双向)的道路,注意,最多只有一条道路连结两个房间, 你需要的时间为z。
输入保证从start到end至少有一条路径。
Output
一行,两个空格分隔的整数,第一个表示你最少需要的时间,第二个表示你在最少时间前提下可以获得的最大得分。
Input示例
3 2 0 2
1 2 3
0 1 10
1 2 11
Output示例
21 6
标准的最短路, dijkstra算法变一下, 多重判断就好了
实现代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 131;
const int INF = 0x7fffffff;
int Va[maxn]; /// 房间价值
struct Edge { /// 图的边结构
int from, to, Cost, Value;
Edge(int f, int t, int c, int v) :\
from(f), to(t), Cost(c), Value(v) {}
};
struct Get {
int time, value;
};
struct HeapNode { /// 优先队列的节点
int t, d, u;
HeapNode(int _t, int _d, int _u) :\
t(_t), d(_d), u(_u) {}
bool operator < (const HeapNode& rhs) const {
if (t == rhs.t) return d < rhs.d;
return t > rhs.t;
}
};
struct Dijkstra {
int n, m; /// 点数和边数
vector<Edge> edges; /// 边列表
vector<int> G[maxn]; /// 每个节点出发的编号(从 0 开始)
bool done[maxn]; /// 是否已永久标号
Get d[maxn]; /// s 到各个节点的距离, 价值
int p[maxn]; /// 最短路中的上一条边。
void init(int n) {
this -> n = n;
for (int i = 0; i < n; ++i) G[i].clear();
edges.clear();
}
void AddEdges(int from, int to, int dist) {
edges.push_back(Edge(from, to, dist, Va[to]));
m = edges.size();
G[from].push_back(m-1);
}
void dijkstra(int s) {
priority_queue<HeapNode> Q;
for(int i = 0; i <n; ++i) d[i].time = INF, d[i].value = -1;
d[s].time = 0, d[s].value = Va[s];
memset(done, 0, sizeof(done));
Q.push(HeapNode(0,Va[s],s));
while(!Q.empty()) {
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = 0; i < G[u].size(); ++i) {
Edge& e = edges[G[u][i]];
if(d[e.to].time > d[u].time + e.Cost) { ///时间多就更新
d[e.to].time = d[u].time + e.Cost;
d[e.to].value = d[u].value + e.Value;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to].time, d[e.to].value, e.to));
} else if(d[e.to].time == d[u].time + e.Cost && d[e.to].value < d[u].value + e.Value) {
/// 时间相同, 价值大的更新。
d[e.to].value = d[u].value + e.Value;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to].time, d[e.to].value, e.to));
}
}
}
}
}Dj;
int main() {
int m, n, st, ed, fr, to, co;
while(cin >> n >> m >> st >> ed) {
Dj.init(n);
for(int i = 0; i < n; ++i) {
cin >> Va[i];
}
for(int i = 0; i < m; ++i) {
cin >> fr >> to >> co;
Dj.AddEdges(fr, to, co);
Dj.AddEdges(to, fr, co);
}
Dj.dijkstra(st);
cout << Dj.d[ed].time << ' ' << Dj.d[ed].value << endl;
}
}
51nod--1459 迷宫游戏 (dijkstra)的更多相关文章
- 51nod 1459 迷宫游戏 dijkstra模板
链接:迷宫游戏 问题 - 51Nod http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 1459 迷宫游戏 基准 ...
- 51nod 1459 迷宫游戏(dij)
题目链接:51nod 1459 迷宫游戏 dij裸题. #include<cstdio> #include<cstring> #include<algorithm> ...
- 51nod 1459 迷宫游戏【最短路拓展】
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数.还有若干双向道路连 ...
- 51nod 1459 迷宫游戏 (最短路径—Dijkstra算法)
题目链接 中文题,迪杰斯特拉最短路径算法模板题. #include<stdio.h> #include<string.h> #define INF 0x3f3f3f3f ],v ...
- 51 NOd 1459 迷宫游戏 (最短路径)
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间, ...
- 51Nod 1459:迷宫游戏(最短路)
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间, ...
- 51nod1459 迷宫游戏
1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你 ...
- c语言迷宫游戏的实现
// // main.c // 迷宫游戏代码实现 // #include <stdio.h> #define ROW 6 //宏定义行 #define COL 6 //宏定义列 /** * ...
- 用webgl打造自己的3D迷宫游戏
用webgl打造自己的3D迷宫游戏 2016/09/19 · JavaScript · WebGL 原文出处: AlloyTeam 背景:前段时间自己居然迷路了,有感而发就想到写一个可以让人迷路 ...
随机推荐
- springBoot打包发布项目------jar包
这两年微服务很流行,这里简单介绍一下如何将自己使用idea写的微服务打包成一个可执行的jar包,并发布到linux服务器的步骤.因为spring boot有内置的tomcat所以一般使用内置的tomc ...
- 细说log4j之log4j 1.x
官网:http://logging.apache.org/log4j/1.2/manual.html 三大组件:loggers,appenders,layouts. LoggersLogger是一个层 ...
- IP白名单
一.什么是IP白名单 公众平台后台新增了IP白名单功能.通过开发者ID及密码调用获取access_token接口时,需要设置访问来源IP为白名单. IP白名单是指一组IP列表,只有该列表中的IP地址的 ...
- Groovy 类名称赋值为变量使用(newInstance & new)
类创建实例一般方式 http://groovy-lang.org/objectorientation.html#_class class Person { String name Integer ag ...
- 十一、移植优化---CONFIG 优化进 menuconfig(2)
11.3 jz2440.h 中的剩余宏移植 11.3.1 CONFIG_SYS_TEXT_BASE CONFIG_SYS_TEXT_BASE:设置系统代码段的基地址,设为 0x0:menuconfig ...
- UML之涉众/参与者(角色/执行者)(Actor)/业务主角(BusinessActor)/业务工人(BusinessWorker)/用户/角色辨析【图解】
参考文档: [业务建模](http://www.baike.com/wiki/%E4%B8%9A%E5%8A%A1%E5%BB%BA%E6%A8%A1) [UML 核心元素之参与者](http://w ...
- luogu P4091 [HEOI2016/TJOI2016]求和
传送门 这一类题都要考虑推式子 首先,原式为\[f(n)=\sum_{i=0}^{n}\sum_{j=0}^{i}S(i,j)*2^j*j!\] 可以看成\[f(n)=\sum_{j=0}^{n}2^ ...
- 关于ajax及相关数据传输问题
之前整理的ajax相关应用笔记,一直没有时间整理,今天突然翻到特此将初稿大概的整理了一下,可能有点乱,欢迎指出不足之处. jQuery的ajax请求:complete函数一般无论服务器有无数据返回都会 ...
- 使用Protobuf定义网络协议
准备工具: 工具下载地址如下:https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1,主要使用到的文件有: protoc.exe ...
- linux 下的init 0,1,2,3,4,5,6知识介绍
一. init是Linux系统操作中不可缺少的程序之一. 所谓的init进程,它是一个由内核启动的用户级进程. 内核自行启动(已经被载入内存,开始运行,并已初始化所有的设备驱动程序和数据结构等)之后, ...