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 ...
随机推荐
- Mysql之锁、事务绝版详解---干货!
一 锁的分类及特性 数据库锁定机制简单来说,就是数据库为了保证数据的一致性,而使各种共享资源在被并发访问变得有序所设计的一种规则.对于任何一种数据库来说都需要有相应的锁定机制,所以MySQL自然也不能 ...
- 新IT运维时代 | Docker运维之最佳实践-上篇
容器技术的发展可以分为两个阶段,第一个阶段聚焦在IaaS层,仅仅把容器当做更轻量级虚拟机来使用,解决了应用运行时进程级资源隔离的问题:随着Docker的出现,容器虚拟化才有了统一的平台,由此容器技术发 ...
- Windows 下配置 Vagrant 环境
Vagrant是一个基于 Ruby 的工具,用于创建和部署虚拟化开发环境.它使用 Oracle 的开源VirtualBox虚拟化系统. Vagrant 在快速搭建开发环境方面是很赞的,试想一个团队中, ...
- containerd与kubernetes集成
kubernetes集群三步安装 概念介绍 cri (Container runtime interface) cri is a containerd plugin implementation of ...
- Docker最简单入门之(一)——介绍和配置Docker
0. 前言 最近学完了Dokcer,特别记录一下,算是对自己学习成果的一个总结.以便自己能够更好的理解Docker.粗略估计了一下,我大概会分成4个部分,只记录一下常用的操作,至于一些比较难的操作或者 ...
- vSphere Web Client 监控 esxi 主机硬件状态
开启插件能对 vcenter 管理的 esxi 主机的硬件状态进行监控. 以下操作均在 vcenter 主机上操作. 0x00 修改配置 文档中关于启用脚本插件支持的说明: Enabling Scri ...
- 帝国CMS(EmpireCMS) v7.5后台getshell分析(CVE-2018-18086)
帝国CMS(EmpireCMS) v7.5后台getshell分析(CVE-2018-18086) 一.漏洞描述 EmpireCMS 7.5版本及之前版本在后台备份数据库时,未对数据库表名做验证,通过 ...
- ASP.NET Core on K8S深入学习(5)Rolling Update
本篇已加入<.NET Core on K8S学习实践系列文章索引>,可以点击查看更多容器化技术相关系列文章. 一.什么是Rolling Update? 为了服务升级过程中提供可持续的不中断 ...
- jenkins增量更新及重启服务步骤
jenkins增量更新步骤:(以creditsys_service_tomcat为例) 1.SecureCRT 或者Xshell 连接服务器192.168.*.*,账号:test/**** 2.cd ...
- SpingBoot:整合Elasticsearch7.2.0
Spring boot 2.1.X整合Elasticsearch最新版的一处问题 新版本的Spring boot 2的spring-boot-starter-data-elasticsearch中支持 ...