这道题看了下很多人都是把每一层拆成两个点然后建图做的。

我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度。

这里我用了两个邻接表,一个是邻接边,一个是邻接层,最后用优先队列优化下。

下面是代码

#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
const int M=2*111111;
int fir[M],u[M],v[M],w[M],nxt[M],e;//边的邻接
int n,m,c;
int lfir[M],lu[M],lnxt[M],le;//层的邻接
int lay[M];//表示每个点所在的层
int dis[M],vis[M];//dis表示与起点距离,vis标记边是否访问过
int lvis[M];//表示层是否访问过
struct cmp
{
bool operator()(int a,int b)
{
return dis[a]>dis[b];
}
}; priority_queue<int,vector<int>,cmp>Q;
void linsert(int la,int num)//插入层
{
lu[le]=num;
lnxt[le]=lfir[la];
lfir[la]=le++;
}
void insert(int a,int b,int c)//插入边
{
u[e]=a;v[e]=b;w[e]=c;
nxt[e]=fir[a];
fir[a]=e++;
}
const int inf=2000000000;
int bfs()
{
int i,j,k,tm;
for(i=1;i<=n;i++)dis[i]=(i==1?0:inf);
memset(vis,0,sizeof(vis));
memset(lvis,0,sizeof(lvis));
while(!Q.empty())Q.pop();
Q.push(1);
while(!Q.empty())
{
int p=Q.top();Q.pop();
if(p==n)break;
if(vis[p])continue;
vis[p]=1;
for(k=fir[p];k!=-1;k=nxt[k])if(dis[ v[k] ]>dis[p]+w[k]){//更新边
dis[ v[k] ]=dis[p]+w[k];
Q.push(v[k]);
} if(lvis[lay[p]])continue;
lvis[lay[p]]=1; tm=lay[p]-1;
for(k=lfir[tm];k!=-1;k=lnxt[k])if(dis[ lu[k] ]>dis[p]+c){//更新上一层
dis[ lu[k] ]=dis[p]+c;
Q.push(lu[k]);
} tm=lay[p]+1;
for(k=lfir[tm];k!=-1;k=lnxt[k])if(dis[ lu[k] ]>dis[p]+c){//更新下一层
dis[ lu[k] ]=dis[p]+c;
Q.push(lu[k]);
}
}
if(dis[n]==inf)return -1;
return dis[n];
}
int main()
{
int t,i,j,k,cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&c);
le=0;
memset(lfir,-1,sizeof(lfir));
for(i=1;i<=n;i++){
scanf("%d",&lay[i]);
linsert(lay[i],i);
}
int a,b,c;
e=0;
memset(fir,-1,sizeof(fir));
for(i=1;i<=m;i++){
scanf("%d%d%d",&a,&b,&c);
insert(a,b,c);
insert(b,a,c);
}
int ans=bfs();
printf("Case #%d: %d\n",cas++,ans);
}
return 0;
}

hdu4725 The Shortest Path in Nya Graph的更多相关文章

  1. HDU-4725 The Shortest Path in Nya Graph (拆点+dji)

    HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...

  2. ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  3. HDU4725 The Shortest Path in Nya Graph SPFA最短路

    典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...

  4. HDU-4725 The Shortest Path in Nya Graph 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...

  5. HDU4725 The Shortest Path in Nya Graph dij

    分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...

  6. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...

  7. 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)

    职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...

  8. HDU4725:The Shortest Path in Nya Graph(最短路)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

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

随机推荐

  1. How do I Find Out Linux CPU Utilization?

    From:http://www.cyberciti.biz/tips/how-do-i-find-out-linux-cpu-utilization.html Whenever a Linux sys ...

  2. 第四节 二维条码与磁卡、IC卡、光卡之比较

    二维条码同其他几种自动识别技术的比较可见下表: 比较点 二维条形码 磁卡 IC卡 光卡 抗磁力 强 弱 中等 强 抗静电 强 中等 中等 强 抗损性 强可折叠可局部穿孔可局部切割 弱不可折叠不可穿孔不 ...

  3. F#实现的单链表(函数式的思想)

    // 在 http://fsharp.net 上了解有关 F# 的更多信息 // 请参阅“F# 教程”项目以获取更多帮助. type list = | Nil | Cons of int * list ...

  4. 暂时和永久改动oracle sysdate的默认输出格式

    1.当前会话有效 alter session set NLS_DATE_FORMAT='YYYY-MM-DD:HH24:MI:SS'; 2.永久生效 sys用户登入后运行例如以下命令 然后重新启动数据 ...

  5. css基础(二)

    一.元素内容的字体属性 1.font-family   字体名称,例如:宋体,新罗马字体等 注意:1.不能使用偏僻的字体,要使用安全字体,如:arial;verdana;times new roman ...

  6. 【Oracle】wmsys.wm_concat函数字段值为空

    这个是因为字符集的问题,和空值是没关系的.其实已经取到了数据,可以验证一下返回的不是0,但是由于这个里面有个chr(0)字符,而且可能第一个字符就是chr(0),所以就显示得怪异的空现象.至于为何会出 ...

  7. [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)

    题目链接:http://acm.swust.edu.cn/problem/0247/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  8. ContentProvider的一些总结

    ContentProvider中的URI, The URI that identifies the provider   一个特定的uri对应着唯一一个内容提供者, 谷歌官方文档里的说明, Query ...

  9. [转] iOS (OC) 中 KVC 与 KVO 理解

    转自: http://magicalboy.com/kvc_and_kvo/ KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解. Key-Value ...

  10. bresenham算法的FPGA的实现2

    在上一篇里http://www.cnblogs.com/sepeng/p/4045593.html <bresenham算法的FPGA的实现1>已经做了一个整体框架的布局,但是那个程序只是 ...