hdu4725 The Shortest Path in Nya Graph
这道题看了下很多人都是把每一层拆成两个点然后建图做的。
我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度。
这里我用了两个邻接表,一个是邻接边,一个是邻接层,最后用优先队列优化下。
下面是代码
#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的更多相关文章
- 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 题意: 在一个图中跑最 ...
- 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 ...
- HDU4725 The Shortest Path in Nya Graph SPFA最短路
典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...
- HDU-4725 The Shortest Path in Nya Graph 最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...
- HDU4725 The Shortest Path in Nya Graph dij
分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...
- hdu4725 The Shortest Path in Nya Graph【最短路+建图】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html ---by 墨染之樱花 题目链接:http://acm.hdu ...
- 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)
职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...
- 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 ...
- 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 ...
随机推荐
- Cygwin ssh
http://www.evalumation.com/blog/86-cygwin-windows7-sshd
- QT基本数据类型(以前没见过qintptr和qlonglong)
QT的基本数据类型 qint8:signed char 有符号8比特数据 qint16:signed short 16位数据类型 qint32:signed int. 32位有符号数据类型 qint6 ...
- boost::bind的使用方法
bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...
- Battle ships(二分图,建图,好题)
Battle ships Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Tot ...
- Save Princess(丑数)
Save Princess 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Yesterday, the princess was kidnapped by a de ...
- ubuntu使用postgist,pgrouting
安装过程 一 :所需组件1 PostgreSQL 9.4 or higher.2 Proj4 .reprojection library.3 GEOS .geometry library.4 LibX ...
- 【centos6 , 7】 网络原理、网络配置
第一部分:网络原理: 一.网络编址 (主要使用IP编址) 1.ip编址是一个双层编址方案,一个ip地址标识一个主机(或一个网卡接口) 2.现在应用最为广泛的是IPv4编址,已经开始逐渐向IPv6编址转 ...
- PHP学习笔记1-常量,函数
常量:使用const(php5)声明,只能被赋值一次,php5以下版本使用define: <?php const THE_VALUE = 100;//PHP5中才有const echo THE_ ...
- Python3.5.1 下使用HTMLParser报错
pip 安装HTMLParser之后,import HTMLParser 使用的时候,报错"ImportError:Can't not find module markupbase" ...
- Chapter 11 迪米特法则
迪米特法则也叫最少知识原则:如果两个类不必彼此直接通信,那么这两个类就不应当发生直接的相互作用.如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用. 迪米特法则首先强调的前提是 ...