我们依据每一个人的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. 开源IDS系列--snorby 进程正常,但是worker无法启动 The Snorby worker is not currently running

    设置页面报错:   The Snorby worker is not currently running. It's imperative you start the worker immediate ...

  2. LoadRunner 函数大全之中文解释

    LoadRunner 函数大全之中文解释 // sapgui_table_set_column_selected 模拟用户 // 单击表中的列标题. int sapgui_table_set_colu ...

  3. Nodejs JSON.parse()无法解析ObjectID和ISODate的问题

    一个早上搞清楚了一个问题,关于Nodjes JSON.parse()方法只能解析字符串.布尔值.数字等,但不能解析ObjectID及ISODate的值 原因:<How to handle Obj ...

  4. python沙盒逃逸

    前言 最近遇到了很多python沙盒逃逸的题目(不知道是不是因为现在python搭的站多了--),实际使用时发现只会复制别人的payload是不够用的,于是自己来总结一波(顺带一提python沙盒逃逸 ...

  5. 【centos 7】搭FTP服务和web访问

    步骤:安装 vsftpd-->增加用户-->配置vsftpd和用户权限 -->配置iptables 安装httpd,开放http访问 /var/tmp/user1下目录 1.安装和用 ...

  6. JS中关于换行的2个坑

    //第一个为ul/ol使用中的li使用需要注意的地方 1.<ul id="banner_indicator"><li></li><li&g ...

  7. Python开发基础-Day32 进程间通信、进程池、协程

    进程间通信 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 进程队列queue 不同于线程queue,进程 ...

  8. 通过JS实现HTML的转义与反转义

    function HTMLEncode(html) { var temp = document.createElement("div"); (temp.textContent != ...

  9. Redis学习篇(八)之连接相关

    PING 测试客户端和服务器之间的连接是否有效,有效返回PONG ECHO 打印特定的信息, 如: ECHO 'HELLO WORLD' QUIT/EXIT 断开当前客户端与服务器之间的连接,可以重连 ...

  10. [BZOJ3992][SDOI2015]序列统计(DP+原根+NTT)

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1888  Solved: 898[Submit][Statu ...