题目链接: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)的更多相关文章

  1. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. (中等) 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 ...

  8. 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 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. 一个项目的SpringCloud微服务改造过程

    SSO是公司一个已经存在了若干年的项目,后端采用SpringMVC.MyBatis,数据库使用MySQL,前端展示使用Freemark.今年,我们对该项目进行了一次革命性的改进,改造成SpringCl ...

  2. lvs+keepalived 高可用及负载均衡

    一.环境准备 VIP:10.18.43.30 dr1:10.18.43.10 dr2:10.18.43.20 web1:10.18.43.13 web2:10.18.43.14 结构图 (一).预处理 ...

  3. Java 设计模式 – Observer 观察者模式

    目录 [隐藏] 1 代码 1.1 观察者接口: 1.2 被观察者: 1.3 观众类 : 1.4 电影类: 1.5 效果如下: 代码 说明都在注释: 观察者接口: package ObserverMod ...

  4. Kafka集群模式安装(二)

    我们来安装Kafka的集群模式,三台机器: 192.168.131.128 192.168.131.130 192.168.131.131 Kafka集群需要依赖zookeeper,所以需要先安装好z ...

  5. Docker 更新版本

    Docker 更新版本 原来版本 1.10 更新后的版本 19.03.1 更新 Docker 版本需要注意的问题: 注意系统是否支持新版本的储存驱动. 19.03.01 版本默认使用的储存驱动是 ov ...

  6. RE最全面的正则表达式----终结篇 特殊处理

    三.特殊需求表达式 Email地址:^w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*$域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0- ...

  7. (二十四)c#Winform自定义控件-单标题窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. 「雕爷学编程」Arduino动手做(10)——敲击传感器模块

    37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...

  9. VMware安装Centos7虚拟机

    首先安装虚拟机很简单,所以呢,具体的安装过程就引用别人的博客,这篇文字很详细,引用之后会在后面加上一些遇到的问题: 原文:https://blog.csdn.net/babyxue/article/d ...

  10. thinkphp model 创建之后访问后的错误···

    解决:在php.ini里边先开启mysql的pdo扩展