题意:有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. 浅谈Java泛型中的extends和super关键字

    泛型是在Java 1.5中被加入了,这里不讨论泛型的细节问题,这个在Thinking in Java第四版中讲的非常清楚,这里要讲的是super和extends关键字,以及在使用这两个关键字的时候为什 ...

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

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

  3. P3830 [SHOI2012]随机树

    P3830 [SHOI2012]随机树 链接 分析: 第一问:f[i]表示有i个叶子结点的时候的平均深度,$f[i] = \frac{f[i - 1] + 2 + f[i - 1] * (i - 1) ...

  4. (一)ABP添加控制器和页面(有时候页面不出来)

    1:添加控制器后需要写[Area("AppAreaName")] 2:继承  WebControllerBase 3:创建视图就可以出现index页面了

  5. 个人博客作业Week2(代码规范,代码复审)

    Q:是否需要有代码规范 首先我们来搞清楚什么是“代码规范”,它和“代码风格”又有什么关系.依据个人的审美角度,我可能更喜欢在函数与函数之间空出一行,可能在命名习惯和代码注释上更加的internatio ...

  6. Linux实践:模块

    标签(空格分隔): 20135321余佳源 一.实践原理 Linux模块是一些可以作为独立程序来编译的函数和数据类型的集合.之所以提供模块机制,是因为Linux本身是一个单内核.单内核由于所有内容都集 ...

  7. pl/sql破解方法

    转载源:http://blog.csdn.net/oscar999/article/details/2123803 打开注册表在run下输入regedit删除1.HKEY_CURRENT_USER/S ...

  8. 非post请求时整个url作为参数传递出现bug

    在非post请求使用整个url作为参数传递到后台时会出现url被截断的bug,这时通过encodeURIComponent进行url的编码可以解决.示例如下: <!--参数url-->Ur ...

  9. 关于RESTful 的概念

    1.REST 是面向资源的,这个概念非常重要,而资源是通过 URI 进行暴露.URI 的设计只要负责把资源通过合理方式暴露出来就可以了.对资源的操作与它无关,操作是通过 HTTP动词来体现,所以RES ...

  10. .NET 使用 RabbitMQ 图文简介

    前言 最近项目要使用RabbitMQ,园里里面已经有很多优秀的文章,Rabbitmq官网也有.net实例.这里我尝试下图文并茂之形式记录下使用的过程. 安装 RabbitMQ是建立在erlang OT ...