我们依据每一个人的layer把同样layer的人分配到同一个层次中。

然后记录走到每一个层次的最小值。

假设这个最小值被更新了。 那么我们就更新与这个层次相连的层次上的点。

其它的就是最普通的spfa求最短路了。

只是要用优先队列优化一下。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
using namespace std;
#define maxn 110000
#define INF 99999999
struct list
{
int next;
int u,v,w;
} edge[maxn*2];
vector<int>vec[maxn];
struct listt
{
int u;
int w;
friend bool operator <(const listt &a,const listt &b)
{
return a.w>b.w;
}
} p,q;
priority_queue<struct listt>que;
int head[maxn];
int nums;
map<int,int>mp;
void init()
{
for(int i=0; i<maxn; i++)vec[i].clear();
memset(head,-1,sizeof(head));
nums=1;
while(!que.empty())que.pop();
mp.clear();
}
void add(int u,int v,int w)
{
edge[nums].u=u;
edge[nums].v=v;
edge[nums].w=w;
edge[nums].next=head[u];
head[u]=nums++;
}
int dis[maxn];
int minn[maxn];
int belong[maxn];
int ans[maxn];
int spfa(int st,int ed,int k)
{
for(int i=0; i<=ed; i++)dis[i]=INF;
for(int i=0; i<=maxn; i++)minn[i]=INF;
p.u=st;
p.w=0;
que.push(p);
dis[st]=0;
while(!que.empty())
{
q=que.top();
que.pop();
if(q.u==ed)
{
return q.w;
}
int x=belong[q.u];
if(q.w<minn[x])
{
minn[x]=q.w;
p.w=minn[x]+k;
if(ans[x-1]+1==ans[x])
{
for(int i=0; i<vec[x-1].size(); i++)
{
p.u=vec[x-1][i];
if(dis[p.u]>p.w)
{
dis[p.u]=p.w;
que.push(p);
} }
}
if(ans[x]+1==ans[x+1])
{
for(int i=0; i<vec[x+1].size(); i++)
{
p.u=vec[x+1][i];
if(dis[p.u]>p.w)
{
dis[p.u]=p.w;
que.push(p);
}
}
}
}
for(int i=head[q.u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v;
int w=edge[i].w;
if(dis[v]>dis[q.u]+w)
{
dis[v]=dis[q.u]+w;
p.u=v;
p.w=dis[v];
que.push(p);
}
}
}
return -1;
}
vector<int>all;
int main()
{
int cas=0;
int T,m,n,k,u,v,w;
scanf("%d",&T);
while(T--)
{
cas++;
init();
scanf("%d%d%d",&n,&m,&k);
all.clear();
for(int i=1; i<=n; i++)
{
scanf("%d",&belong[i]);
//vec[belong[i]].push_back(i);
all.push_back(belong[i]);
}
//-----离散化
sort(all.begin(),all.end());
int st=1;
mp[all[0]]=1;
ans[1]=all[0];
for(int i=1; i<all.size(); i++)
{
if(all[i]!=all[i-1])
{
mp[all[i]]=++st;
ans[st]=all[i]; }
}
//-------------------
for(int i=1; i<=n; i++)
{
int x=mp[belong[i]];
vec[x].push_back(i);
belong[i]=x;
}
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
printf("Case #%d: %d\n",cas,spfa(1,n,k));
}
return 0;
}

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. Hadoop案例(七)MapReduce中多表合并

    MapReduce中多表合并案例 一.案例需求 订单数据表t_order: id pid amount 1001 01 1 1002 02 2 1003 03 3 订单数据order.txt 商品信息 ...

  2. chrome浏览器插件开发经验(一)

    最近在进行chrome浏览器插件的开发,一些小的经验总结随笔. 1.首先,推荐360的chrome插件开发文档:http://open.chrome.360.cn/extension_dev/over ...

  3. 自动化测试框架Cucumber和Robot Framework的实战对比

    自动化测试框架Cucumber和RobotFramework的实战对比 一.摘要 自动化测试可以快速自动完成大量测试用例,节约巨大的人工测试成本:同时它需要拥有专业开发技能的人才能完成开发,且需要大量 ...

  4. Java利用Redis实现消息队列

    应用场景 为什么要用redis?二进制存储.java序列化传输.IO连接数高.连接频繁 一.序列化 这里编写了一个java序列化的工具,主要是将对象转化为byte数组,和根据byte数组反序列化成ja ...

  5. chakra在vs2017中编译出现的问题

    转:http://blog.csdn.net/ink_cherry/article/details/73437981 1.无法找到vs2010生成工具 MSB8020 无法找到 Visual Stud ...

  6. 湖南大学ACM程序设计新生杯大赛(同步赛)J - Piglet treasure hunt Series 2

    题目描述 Once there was a pig, which was very fond of treasure hunting. One day, when it woke up, it fou ...

  7. 关于字体剥离和精简工具 FontSubsetGUI 和 FontPruner 的比较。

    在 Unity 中制作游戏时对动态字体的剥离和精简是现在常用的手段,现在有两篇博客是大家阅读和参照较多的,分别是 如何精简Unity中使用的字体文件 和 FontPruner 字体精简工具.他们各自提 ...

  8. 【模式匹配】更快的Boyer

    1. 引言 前一篇中介绍了字符串KMP算法,其利用失配时已匹配的字符信息,以确定下一次匹配时模式串的起始位置.本文所要介绍的Boyer-Moore算法是一种比KMP更快的字符串匹配算法,它到底是怎么快 ...

  9. dp 神奇的口袋

    有一个神奇的口袋,总的容积是40,用这个口袋可以变出一 些物品,这些物品的总体积必须是40.  John现在有n(1≤n ≤ 20)个想要得到的物品,每个物品 的体积分别是a1,a2--an.Joh ...

  10. redux 初步理解

    派发一个 action 给 reducer, reducer 生成了一个新的 state; redux 通过 Store 来保存数据, store.getState 获得数据, 而要更新 state, ...