我们依据每一个人的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. (转)HttpClient 模拟登陆,保持会话并进行后续操作

    转自:http://unmi.cc/httpclient-login-session android实现session保持 SessionID的本质

  2. 在Ubuntu上安装Arena

    安装JDK 首先安装JDK对吧,下面以jdk-7u67-linux-i586.tar.gz为例 在官网上下载JDK,具体依照你的机器而定. 解压掉 tar -zxvf jdk-7u67-linux-i ...

  3. flutter android沉浸式状态栏

    import 'package:flutter/services.dart'; import 'dart:io'; class _MyAppState extends State<MyApp&g ...

  4. navicat for mysql 快捷键

    1.ctrl+q           打开查询窗口2.ctrl+/            注释sql语句3.ctrl+shift +/  解除注释4.ctrl+r           运行查询窗口的s ...

  5. Python实现图片转文字并翻译至剪切板

    一.环境搭建: 1.PySimpleGUI: pip3 install pysimplegui 2.pytesseract需要有tesseract环境才行: 1. 先搭建tesseract: brew ...

  6. Unity 2D游戏开发教程之精灵的死亡和重生

    Unity 2D游戏开发教程之精灵的死亡和重生 精灵的死亡和重生 目前为止,游戏项目里的精灵只有Idle和Walking这两种状态.也就是说,无论精灵在游戏里做什么,它都不会进入其它的状态,如死亡.于 ...

  7. PHP 笔记——Web页面交互

    一.客户端数据提交方法 客户端浏览器的数据通常使用 GET.POST 方式提交到服务器. 1.GET方式 GET方式指直接在URL中提供上传数据或者通过表单采用GET方式上传. http://url? ...

  8. 我的OI生涯 第四章

    第四章 晚上来机房的人越来越多了,我也注意到一个常年独自坐在一个角落的男人————郝哥. 郝哥为人很安静,只是那时我还不知道他好不好,就没有与他交流过什么,这个优秀的男人以后我们还会提到,这里先不讲. ...

  9. [BZOJ4423][AMPPZ2013]Bytehattan(对偶图+并查集)

    建出对偶图,删除一条边时将两边的格子连边.一条边两端连通当且仅当两边的格子不连通,直接并查集处理即可. #include<cstdio> #include<algorithm> ...

  10. 【tarjan】BZOJ2140-稳定婚姻

    又名NTR的故事 [题目大意] n对夫妻Bi和Gi.若某男Bi与某女Gj曾经交往过,他们有私奔的可能性.不妨设Bi和Gj旧情复燃,进而Bj会联系上了他的初恋情人Gk,以此递推.若在Bi和Gi离婚的前提 ...