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

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

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

下面是代码

#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. Code one 码

    Code one是一种用成像设备识别的矩阵式二维条码.Code one符号中包含可由快速性线性探测器识别的识别图案.每一模块的宽和高的尺寸为X.  Code one符号共有10种版本及时14种尺寸.最 ...

  2. SpringBoot接口服务处理Whitelabel Error Page(转)

    To switch it off you can set server.error.whitelabel.enabled=false http://stackoverflow.com/question ...

  3. 在Xcode中使用C++与Objective-C混编

    有时候,出于性能或可移植性的考虑,需要在iOS项目中使用到C++. 假设我们用C++写了下面的People类: // //  People.h //  MixedWithCppDemo // //   ...

  4. JAVA 软件升级版本号比较

    简单版本号比较: public class CompareVersion {   public static void main(String[] args) { // TODO Auto-gener ...

  5. BZOJ 2134: 单选错位( 期望 )

    第i个填到第i+1个的期望得分显然是1/max(a[i],a[i+1]).根据期望的线性性, 我们只需将每个选项的期望值累加即可. ---------------------------------- ...

  6. 断开/删除 SVN 链接(.svn)的几种方法

    上传到正式的服务器时需要去掉这些不必要的文件,找到了几种方法: 1.windows下: xcopy project_dir project_dir_1 /s /i (从project_dir 复制文件 ...

  7. 设置QPushButton的平面与突出(遍历控件)

    #include "ui_maindialog.h" #include "maindialog.h" #include <QState> #incl ...

  8. 【JSP】JSTL核心标签库的使用方法和示例

    JSTL 核心标签库 JSTL 核心标签库标签共有13个,功能上分为4类: 1. 表达式控制标签:out.set.remove.catch 2. 流程控制标签:if.choose.when.other ...

  9. 模拟红外协议C程序——接收模块

    目的:方便程序的调试,提供效率,减少工作累,可以不在线调试编程时显示实时数据,特别产品不带显示的或者MCU是OPT的,有很大的帮助. 过程:将要看的数据发送出来,另一个板(一个带有显示的就OK了,显示 ...

  10. iOS swift lazy loading

    Why bother lazy loading and purging pages, you ask? Well, in this example, it won't matter too much ...