hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725
题意:有n个点和n层,m条边,每一层的任意一个点都可以花费固定的值到下一层或者上一层的任意点
然后m条边链接的点可以花费给出的值进行转移,最后问从i点到n点最少要花费多少。
这题点的个数有100000,层数也是100000,不算额外边暴力建边肯定要爆。
所以可以把层数也当成一个点比如说i点在j层于是n+j就与i链接花费0然后i点可以和上下层任意一个点链接
及i与n+j+1链接i与n+j-1链接花费为c,最后额外边就正常处理。
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stdio.h>
#define inf 0X3f3f3f3f
using namespace std;
const int M = 1e5 + 10;
int n , m , c , u , v , w , l , pos[M << 1] , dis[M << 1];
struct qnode {
int v , w;
qnode(int v , int w):v(v) , w(w) { }
bool operator <(const qnode &r) const{
return w > r.w;
}
};
struct TnT {
int v , w , next;
TnT(int v , int w):v(v) , w(w) { }
};
vector<TnT> vc[M << 1];
void add(int u , int v , int w) {
vc[u].push_back(TnT(v , w));
}
bool vis[M << 1];
void dij(int sta) {
priority_queue<qnode>q;
for(int i = 1 ; i <= 2 * n ; i++) {
dis[i] = inf;
vis[i] = false;
}
dis[sta] = 0;
q.push(qnode(sta , 0));
while(!q.empty()) {
int u = q.top().v;
q.pop();
if(vis[u])
continue;
vis[u] = true;
for(int i = 0 ; i < vc[u].size() ; i++) {
TnT gg = vc[u][i];
if(dis[gg.v] > dis[u] + gg.w && !vis[gg.v]) {
dis[gg.v] = dis[u] + gg.w;
q.push(qnode(gg.v , dis[gg.v]));
}
}
}
}
int main() {
int t , ans = 0;
scanf("%d" , &t);
while(t--) {
ans++;
scanf("%d%d%d" , &n , &m , &c);
for(int i = 1 ; i <= 2 * n ; i++) { vc[i].clear();
}
for(int i = 1 ; i <= n ; i++) {
vis[i] = false;
}
for(int i = 1 ; i <= n ; i++) {
scanf("%d" , &l);
pos[i] = l;
vis[l] = true;
}
for(int i = 1 ; i <= n ; i++) {
if(pos[i] == 1) {
add(pos[i] + n , i , 0);
if(n > 1 && vis[pos[i] + 1]) {
add(i , pos[i] + 1 + n , c);
}
}
else if(pos[i] == n) {
add(pos[i] + n , i , 0);
if(n > 1 && vis[pos[i] - 1]) {
add(i , pos[i] - 1 + n , c);
}
}
else {
add(pos[i] + n , i , 0);
if(1 < n && vis[pos[i] + 1]) {
add(i , pos[i] + 1 + n , c);
}
if(n > 1 && vis[pos[i] - 1]) {
add(i , pos[i] - 1 + n , c);
}
}
}
for(int i = 1 ; i <= m ; i++) {
scanf("%d%d%d" , &u , &v , &w);
add(u , v , w);
add(v , u , w);
}
dij(1);
if(dis[n] == inf)
dis[n] = -1;
printf("Case #%d: %d\n" , ans , dis[n]);
}
return 0;
}
hdu 4725 The Shortest Path in Nya Graph(建图+优先队列dijstra)的更多相关文章
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]
HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...
- HDU 4725 The Shortest Path in Nya Graph
he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...
- HDU 4725 The Shortest Path in Nya Graph(构图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4725 The Shortest Path in Nya Graph (最短路)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 4725 The Shortest Path in Nya Graph (最短路+建图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 4725 The Shortest Path in Nya Graph(最短路径)(2013 ACM/ICPC Asia Regional Online ―― Warmup2)
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- HDU 4725 The Shortest Path in Nya Graph (最短路 )
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
- HDU - 4725 The Shortest Path in Nya Graph 【拆点 + dijkstra】
This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just ...
随机推荐
- 异步编程CompletableFuture实现高并发系统优化之请求合并
先说场景: 根据Redis官网介绍,单机版Redis的读写性能是12万/秒,批量处理可以达到70万/秒.不管是缓存或者是数据库,都有批量处理的功能.当我们的系统达到瓶颈的时候,我们考虑充分的压榨缓存和 ...
- dubbo文档笔记
配置覆盖关系 以 timeout 为例,显示了配置的查找顺序,其它 retries, loadbalance, actives 等类似: 方法级优先,接口级次之,全局配置再次之. 如果级别一样,则消费 ...
- spark shuffle写操作之SortShuffleWriter
提出问题 1. spark shuffle的预聚合操作是如何做的,其中底层的数据结构是什么?在数据写入到内存中有预聚合,在读溢出文件合并到最终的文件时是否也有预聚合操作? 2. shuffle数据的排 ...
- PythonDay05
第五章 今日内容 字典 字典 语法:{'key1':1,'key2':2} 注意:dict保存的数据不是按照我们添加进去的顺序保存的. 是按照hash表的顺序保存的. ⽽hash表 不是连续的. 所以 ...
- 史上最全面的SignalR系列教程-2、SignalR 实现推送功能-永久连接类实现方式
1.概述 通过上篇史上最全面的SignalR系列教程-1.认识SignalR文章的介绍,我们对SignalR技术已经有了一个全面的了解.本篇开始就通过SignalR的典型应用的实现方式做介绍,例子虽然 ...
- maven的不同版本下载及环境配置
Maven不同版本下载及环境配置 Maven下载 去到官网 https://maven.apache.org/ 会发现是最新版本,但是一般下载的话,都会下载比最新的版本要低两到三个小版本的,这里就下载 ...
- win10 将硬盘工作模式由IDE调整到AHCI模式
第1步:重启进入安全模式 1)点击“开始”按钮 进入设置 2)进入“更新和安全”,“恢复-高级启动”,点击“立即高级启动”, 依次选择“疑难解答”-“高级选项”-“启动设置”-点击“重启” 第2步:进 ...
- Linux expect 介绍和用法
expect是一个自动化交互套件,主要应用于执行命令和程序时,系统以交互形式要求输入指定字符串,实现交互通信. expect自动交互流程: spawn启动指定进程---expect获取指定关键字--- ...
- LoRaWAN_stack移植笔记(三)__SPI
stm32相关的配置 由于例程使用的主控芯片为STM32L151C8T6,而在本设计中使用的主控芯片为STM32L051C8T6,内核不一样,并且Cube库相关的函数接口及配置也会有不同,所以芯片的驱 ...
- @RequestBody 注意的问题
contentType : "application/json", //只能是这个 RequestBody 不能和form/data共存: @RequestMapping(valu ...