题目:http://acm.hdu.edu.cn/showproblem.php?pid=4725

题意:有N个点和N层..一层有X个点(0<=X<=N).两邻两层间有一条路花费C。还有M条小路在两个点之间。问从第一个点走到第N个点最短路是多少...

题解:依然是在点之间SPFA。不过在跑的时候不仅要跑与当前点相连接的点。还有把当前点所在层的相邻层的点判断是否加入队列...

CODE:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue> #define mkp make_pair
#define fst first
#define scd second using namespace std;
int dis[100011];
int vis[100011];
int lay[100011];
vector<int>vec[100011];
struct Edge_t{
int to,next,cap;
}edge[500000];
int head[100011],et; inline void adde(int u,int v,int w){
edge[et].to=v;
edge[et].cap=w;
edge[et].next=head[u];
head[u]=et++;
} inline void spfa(int n,int C){
queue<int>q;
q.push(1);
memset(vis,0,sizeof vis);
memset(dis,-1,sizeof dis);
dis[1]=0;
int e,u,v,size,i,k;
while(!q.empty()){
u=q.front();q.pop();
vis[u]=0;
for(e=head[u];~e;e=edge[e].next){
v=edge[e].to;
if(dis[v]<0 || dis[v]>dis[u]+edge[e].cap){
dis[v]=dis[u]+edge[e].cap;
if(!vis[v]){
vis[v]=1;
q.push(v);
}
}
}
if(lay[u]>1){
size=vec[k=(lay[u]-1)].size();
for(i=0;i<size;++i){
v=vec[k][i];
if(dis[v]<0 || dis[v]>dis[u]+C){
dis[v]=dis[u]+C;
if(!vis[v]){
vis[v]=1;
q.push(v);
}
}
}
}
if(lay[u]<n){
size=vec[k=lay[u]+1].size();
for(i=0;i<size;++i){
v=vec[k][i];
if(dis[v]<0 || dis[v]>dis[u]+C){
dis[v]=dis[u]+C;
if(!vis[v]){
vis[v]=1;
q.push(v);
}
}
} }
}
} int main(){
int t,tt=0,C;
int n,m,u,v,i,size,w;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&C);
memset(head,-1,sizeof head);et=0;
memset(vis,0,sizeof vis);
for(i=1;i<=n;++i){
scanf("%d",&lay[i]);//第i个点所以层为lay[i]
vec[i].clear();
}
while(m--){
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);adde(v,u,w);
if(!vis[u]){//把点加入层..
vec[lay[u]].push_back(u);
vis[u]=1;
}
if(!vis[v]){
vec[lay[v]].push_back(v);
vis[v]=1;
}
}
if(!vis[1])//始点和终于一定要在某一层内..
vec[lay[1]].push_back(1);
if(!vis[n])
vec[lay[n]].push_back(n);
for(i=1;i<=n;++i)//如果某一层没有点
if(vec[lay[i]].empty())
vec[lay[i]].push_back(i);
spfa(n,C);
printf("Case #%d: %d\n",++tt,dis[n]);
}
return 0;
}

HDU 4725 The Shortest Path in Nya Graph-【SPFA最短路】的更多相关文章

  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 (最短路)

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

  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 (最短路 )

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

  6. HDU 4725 The Shortest Path in Nya Graph(最短路拆点)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意:n个点,某个点属于某一层.共有n层.第i层的点到第i+1层的点和到第i-1层的点的代价均是 ...

  7. HDU 4725 The Shortest Path in Nya Graph(最短路建边)题解

    题意:给你n个点,m条无向边,每个点都属于一个层,相邻层的任意点都能花费C到另一层任意点,问你1到n最小路径 思路:没理解题意,以为每一层一个点,题目给的是第i个点的层数编号.这道题的难点在于建边,如 ...

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

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

  10. (中等) 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 ...

随机推荐

  1. html mysql special character

    function html_encode(str) { var s = ""; if (str.length == 0) return ""; s = str. ...

  2. 一篇入门的php Class 文章

    刚在大略浏览了一下首页更新的那篇有关Class的文章(指PHPE的那篇 http://www.phpe.net/articles/389.shtml ),很不错,建议看看.  对类的摸索--俺用了半年 ...

  3. 利用Console来调试JS程序、Console用法总结

    http://blog.163.com/zhangmihuo_2007/blog/static/27011075201452522824347/ http://blog.163.com/zhangmi ...

  4. 个人收集资料整理-WebForm

    [2016-03-23 20:35:53] C#实现局域网文件传输    win7系统中桌面图标显示不正常问题

  5. Linux Apache绑定多域名

    1 网上查到资源不符 网上查到的Apache绑定域名都说要修改http.conf文件,但是我的服务器上的apache是通过apt-get install安装的,安装方法应该是没错的,但是通过find ...

  6. 为过程或函数sp_Adduser指定了过多的参数

    前些天写用户注册模块,用存储过程添加用户,一开始就报“为过程或函数sp_Adduser指定了过多的参数”.仔细检查数据层的用户添加函数,结果在为存储过程添加sqlparameter参数的时候,数组给写 ...

  7. css3_note

    css3基础 css3选择器 属性选择器 属性选择器基本上IE7+都支持,可以放心的使用,参见caniuse [attr] [attr=val] [attr*=val] [attr^=val] [at ...

  8. Scale-up(纵向扩展) vs Scale-out(横向扩展)

    转载:http://wuaner.iteye.com/blog/1843799 http://www.javaworld.com/article/2077780/java-web-developmen ...

  9. 51nod 1237 最大公约数之和 V3(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...

  10. 基于ZooKeeper的Dubbo简单抽样登记中心

    一:设备zookeeper 系统环境 Ubuntu 14.04.2 LTS x64 IP : 192.168.1.102 下载zookeeper-3.4.6.tar.gz到文件夹/opt.拉开拉链 m ...