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 ...
随机推荐
- Spring Cloud 之 Stream.
一.简介 Spring Cloud Stream 是一个用来为微服务应用构建消息驱动能力的框架. Spring Cloud Stream 为一些供应商的消息中间件产品(目前集成了 RabbitMQ 和 ...
- 【转】C++文件读写详解(ofstream,ifstream,fstream)
转:http://blog.csdn.net/kingstar158/article/details/6859379 摘要:具体用法,上面链接中,文章写的很详细,讲解ofstream,ifstream ...
- eclipse Maven配置以及使用方法
简述: 现需要在Eclipse中配置Maven插件,同时安装maven应用,配置Maven环境变量,建立Maven管理的工程,并用Maven导入Gson包, 编写简易Json输出程序 步骤: 1. 首 ...
- SpringBoot第二十三篇:安全性之Spring Security
作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/11350255.html 版权声明:本文为博主原创文章,转载请附上博文链接! 引言 系统的安全 ...
- STL 队列
头文件 #include <queue> 定义 普通队列: queue < int > q; 优先队列: priority_queue < int, vector< ...
- Appium+python自动化(三十二)- 代码写死一时爽,框架重构火葬场 - PageObject+unittest(超详解)
简介 江湖有言:”代码写死一时爽,框架重构火葬场“,更有人戏言:”代码动态一时爽,一直动态一直爽
- 【数据结构】线段树(Segment Tree)
假设我们现在拿到了一个非常大的数组,对于这个数组里面的数字要反复不断地做两个操作. 1.(query)随机在这个数组中选一个区间,求出这个区间所有数的和. 2.(update)不断地随机修改这个数组中 ...
- Mysql优化(出自官方文档) - 第八篇(索引优化系列)
目录 Mysql优化(出自官方文档) - 第八篇(索引优化系列) Optimization and Indexes 1 Foreign Key Optimization 2 Column Indexe ...
- 帝国CMS(EmpireCMS) v7.5 后台XSS漏洞分析
帝国CMS(EmpireCMS) v7.5 后台XSS漏洞分析 一.漏洞描述 该漏洞是由于代码只使用htmlspecialchars进行实体编码过滤,而且参数用的是ENT_QUOTES(编码双引号和单 ...
- Selenium + python 测试环境搭建扩展-HTMLUNIT的使用
尝试给公司的网站写每日例行检查的脚本时,不需要去打开浏览器,这是就用到HTMLUNIT的使用 HTMLUNIT是基于Selenium服务端的,所以需要selenium-server-standalon ...