题意:有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. linux下service+命令和直接去执行命令的区别,怎么自己建立一个service启动

    启动一些程序服务的时候,有时候直接去程序的bin目录下去执行命令,有时候利用service启动. 比如启动mysql服务时,大部分喜欢执行service mysqld start.当然也可以去mysq ...

  2. PV原语操作详解

    from http://www.blogjava.net/wxqxs/archive/2009/05/10/277320.html PV原语通过操作信号量来处理进程间的同步与互斥的问题.其核心就是一段 ...

  3. 利用卷积神经网络(VGG19)实现火灾分类(附tensorflow代码及训练集)

    源码地址 https://github.com/stephen-v/tensorflow_vgg_classify 1. VGG介绍 1.1. VGG模型结构 1.2. VGG19架构 2. 用Ten ...

  4. CAD2020下载安装AutoCAD2020中文版下载地址+安装教程

    AutoCAD2020中文版为目前最新软件版本,我第一时间拿到软件进行安装测试,确保软件正常安装且各项功能正常可以使用,立刻拿出来分享,想用最新版本的话,抓紧下载使用吧: 我把我用的安装包贡献给你下载 ...

  5. Dethe is my Finaunce金融

    英国诗人乔叟Dethe is my Finaunce金融 英语中“金融”在14世纪,金融计算时间价值的手段.就随机结果签约的能力.一个允许转让金融权后的清算.<Lamentation of Ma ...

  6. Docker容器学习梳理 - 应用程序容器环境部署

    关于国内Docker镜像,可以参考:Docker容器学习梳理--基础知识(2) 的Docker镜像使用. 如果我们需要在Docker环境下部署tomcat.redis.mysql.nginx.php等 ...

  7. 898 C. Phone Numbers

    传送门 [http://codeforces.com/contest/898/problem/C] 题意 题意比较难理解直接看样例就知道了,给你个n接下来n行,每行包括一个名字和号码的数量,还有具体的 ...

  8. 第三次Sprint

    Not CHECKED OUT CHECKED OUT DONE!: SPRINT GOAL: BETA-READY 修改bug 完善界面

  9. 第二个spring

    由于第一个spring已经完成,我们现在进去第二个spring! 陈志棚:成绩的统筹 李天麟:界面音乐 徐侃:代码算法   plan好布局,分配任务,控制时间!

  10. Install Kernel 3.10 on CentOS 6.5

    http://bicofino.io/2014/10/25/install-kernel-3-dot-10-on-centos-6-dot-5/ https://gree2.github.io/lin ...