题意:有n个点,每个点都在一个层内,层与层之间的距离为c,一个层内的点可以到达与它相邻的前后两个层的点,还有m条小路

。。时间真的是卡的很恶心啊。。。

借一下别人的思路思路:
这题主要难在建图上,要将层抽象出来成为n个点(对应编号依次为n+1~n+n),然后层与层建边,点与点建边,层与在该层上的点建边(边长为0),点与相邻层建边(边长为c)。
ps:这样处理就不用拆点了。不过要注意的是相邻两层必须都要有点才建边(不然会WA,可以参考我贴的数据)
 借鉴自:http://www.myexception.cn/program/1403919.html
 
主要是把层也抽象为点
spfa:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn = , INF = 0xfffffff;
int n,m,c;
struct node{
int u, v, d, next;
}Node[maxn*];
int d[maxn], vis[maxn], head[maxn], tmp[maxn], vv[maxn];
void add(int u,int v,int d,int i)
{
Node[i].u = u;
Node[i].v = v;
Node[i].d = d;
Node[i].next = head[u];
head[u] = i;
} void spfa(int s)
{
queue<int> Q;
mem(vis,);
fill(d,d+maxn,INF);
d[s] = ;
Q.push(s);
vis[s] = ;
while(!Q.empty())
{
int x = Q.front();Q.pop();
vis[x] = ;
for(int i=head[x]; i!=-; i=Node[i].next)
{
node e = Node[i];
if(d[e.v] > d[x] + e.d)
{
d[e.v] = d[x] + e.d;
if(!vis[e.v])
{
Q.push(e.v);
vis[e.v] = ;
}
}
}
} } int main()
{
int T;
int res = ;
scanf("%d",&T);
while(T--)
{
int ans = ;
mem(tmp,);
mem(vv,);
mem(head,-);
scanf("%d%d%d",&n,&m,&c);
for(int i=; i<=n; i++)
{
scanf("%d",&tmp[i]);
vv[tmp[i]] = ;      //若这层有点 则标记
}
for(int i=;i<=n;i++)
{
if(vv[i] && vv[i+]) //判断相邻两层是否有点 若有 则连接相邻两层
{
add(n+i,n+i+,c,ans++);
add(n+i+,n+i,c,ans++);
}
}
for(int i=;i<=n;i++)
{
add(n+tmp[i],i,,ans++); // 连接层与点
if(tmp[i] > ) add(i,n+tmp[i]-,c,ans++);  //连接点与相邻层
if(tmp[i] < n) add(i,n+tmp[i]+,c,ans++);
}
for(int i=; i<m; ++i)    //连接点与点
{
int u,v,d;
scanf("%d%d%d",&u,&v,&d);
add(u,v,d,ans++);
add(v,u,d,ans++);
}
spfa();
printf("Case #%d: ",++res);
if(d[n]!=INF)
printf("%d\n",d[n]);
else
printf("-1\n"); } return ;
}

HDU - 4725 (The Shortest Path in Nya Graph)层次网络的更多相关文章

  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. HiKey软硬件开发环境及其调试

    HiKey是一款搭载华为海思麒麟620芯片,符合Linaro 96Boards标准的SBC开发板.它采用8核64位Cortex-A53处理器,主频高达1.2GHz. HiKey作为AOSP支持的一款产 ...

  2. 【小程序】模拟数据支持(mockjs配置模拟服务器接口数据)

    utils目录 ①下载mockjs(地址)放置utils目录中 ②新建api.js :配置模拟数据以及后台接口,通过DEBUG=ture;  //切换数据接口 配置如下: let API_HOST = ...

  3. jsp界面的继承与否剖析

    引入页面时候 ${pageContext.request.contextPath}为页面上下文路径:也可以用js来实现: <script type="text/javascript&q ...

  4. Perhaps you are running on a JRE rather than a JDK

    在Eclipse中跑maven项目时,出现上面的问题: 1.有可能你的环境变量配置是在jre上面的,所以你要检查一下你配置文件,PATH和CLASSPATH都要检查 2.eclipse默认是跑在jre ...

  5. 算法相关——Java排序算法之快速排序(三)

    0. 前言 本系列文章将介绍一些常用的排序算法.排序是一个非常常见的应用场景,也是开发岗位面试必问的一道面试题,有人说,如果一个企业招聘开发人员的题目中没有排序算法题,那说明这个企业不是一个" ...

  6. Codeforces Edu Round 63(Rated for Div. 2)

    感觉现在Edu场比以前的难多了…… A: 温暖人心 /* basic header */ #include <iostream> #include <cstdio> #incl ...

  7. 面试3——java集合类总结(Map)

    1.概述: Java 中的map集合使用键值对(key-value)来保持数据,其中值(value)可以重复,键(key)必须唯一,但最多只能有一个key为空,它的主要实现类有HashMap.Hash ...

  8. Codeforces 996E Leaving the Bar (随机化)

    题目连接:Leaving the Bar 题意:给你n个向量,你可以加这个向量或减这个向量,使得这些向量之和的长度小于1.5e6. 题解: 按照正常的贪心方法,最后的结果有可能大于1.5e6 .这里我 ...

  9. myeclipse、maven、tomcat、jdk技巧和坑【待完善】

    公司使用前后不分离或半分离的springmvc + maven ,自己不得不研究研究myeclipse.maven.tomcat等等 开发环境搭建:坑一: Unable to process Jar ...

  10. Nginx反向代理的简单实现

    1)nginx的反向代理:proxy_pass2)nginx的负载均衡:upstream 下面是nginx的反向代理和负载均衡的实例: 负载机:A机器:103.110.186.8/192.168.1. ...