题目: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. javascript 预定义函数

    parseInt() parseFloat() isNaN() isFinite() encodeURI() decodeURI() encodeURIComponent() decodeURICom ...

  2. js中递归解析xml

    xml结构: <RightMenuItems>  <Item Code="New" Name="新建" GroupCode="Edi ...

  3. github--新手使用错误分析

    先上去github 或者任意托管的网站.注册账号,新建仓库, 在本地运行Xcode 新建工程,新建工程的时候 勾上本地 的仓库,然后 在本地的项目根目录执行下边的命令: git remote add ...

  4. Spring 装配Bean

    Spring 装配Bean 装配解释: 创建应用对象之间协作关系的的行为通常称为装配(wiring),这也是依赖注入的本质 依赖注入是Spring的基础要素 一 : 使用spring装配Bean基础介 ...

  5. 用Java实现 ,冒泡排序与普通排序的区别

     冒泡排序与普通排序的区别  /**    *个人网址: http://www.lipengfei2013.tk    * 功能:冒泡排序与普通排序的区别    */ package www.csdn ...

  6. perl 爬取上市公司业绩预告

    <pre name="code" class="python">use LWP::UserAgent; use utf8; use DBI; use ...

  7. DAL – RDBMS 的分区

    编辑人员注释:本文章由AzureCAT 云与企业工程组的高级项目经理Shaun Tinline-Jones 和Chris Clayton 共同撰写. "云服务基础"应用程序也称作& ...

  8. Euromonitor 2013年奢侈品报告精选 |华丽志

    Euromonitor 2013年奢侈品报告精选 |华丽志 Euromonitor 2013年奢侈品报告精选

  9. 吝啬的国度(dfs+vector)

    吝啬的国度 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市, ...

  10. MD5的加密和解密(总结)

    效果图例如以下: package com.test; import java.security.MessageDigest; public class MD5 { // MD5加码.32位 publi ...