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 ...
随机推荐
- OOM和JVM配置优化
OOM这个缩写就是Java程序开发过程中让人最头痛的问题:Out of Memory.在很多开发人员的开发过程中,或多或少的都会遇到这类问题,这类问题定位比较困难,往往需要根据经验来判断可能出现问题的 ...
- [TCP/IP]DNS解析
DNS解析主机的IP地址 host -t A www.baidu.com
- 【游记】NOIP2018复赛
声明 我的游记是一个完整的体系,如果没有阅读过往届文章,阅读可能会受到障碍. ~~~上一篇游记的传送门~~~ 前言 参加完NOIP2018的初赛过后,我有点自信心爆棚,并比之前更重视了一点(也仅仅是一 ...
- Flink 源码解析 —— 源码编译运行
更新一篇知识星球里面的源码分析文章,去年写的,周末自己录了个视频,大家看下效果好吗?如果好的话,后面补录发在知识星球里面的其他源码解析文章. 前言 之前自己本地 clone 了 Flink 的源码,编 ...
- java并发编程(七)----(JUC)ReadWriteLock
前面我们已经分析过JUC包里面的Lock锁,ReentrantLock锁和semaphore信号量机制.Lock锁实现了比synchronized更灵活的锁机制,Reentrantlock是Lock的 ...
- 转载 | float 清除浮动的7种方法
什么叫浮动:浮动会使当前标签脱离文档流,产生上浮的效果,同时还会影响周边元素(前后标签)及父级元素的位置和width,height属性.下面用一个小例子来看一看浮动的全过程:1.首先我们新建一个网页, ...
- 基于python语言使用余弦相似性算法进行文本相似度分析
编写此脚本的目的: 本人从事软件测试工作,近两年发现项目成员总会提出一些内容相似的问题,导致开发抱怨.一开始想搜索一下是否有此类工具能支持查重的工作,但并没找到,因此写了这个工具.通过从纸上谈兵到着手 ...
- LayDate使用
layDate非常愿意和您成为工作伙伴.她致力于成为全球最用心的web日期支撑,为国内外所有从事web应用开发的同仁提供力所能及的动力.她基于原生JavaScript精心雕琢,兼容了包括IE6在内的所 ...
- 利用DoHome APP和音箱控制小车的实验参考步骤
准备材料: Arduino Uno 一块 Arduino 扩展板 购买链接 DT-06模块一个 购买链接 安卓手机一个 小度音箱一个 小车一个 杜邦线若干 1.DT-06固件 ...
- SpringBoot:Mybatis + Druid 数据访问
西部开源-秦疆老师:基于SpringBoot 2.1.7 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! 简介 对于数据访问层 ...