hdu-4725-The Shortest Path in Nya Graph-层次网络
我们依据每一个人的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-层次网络的更多相关文章
- Hdu 4725 The Shortest Path in Nya Graph (spfa)
题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- (中等) 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- MySQL增删改数据
1.增加数据 ,); /*插入所有字段.一定依次按顺序插入--字符串与日期需要加单引号,数字不需要,各个字段之间用逗号分隔*//*注意不能少或者多字段值*/ ,) /*按字段名插入数据,中间用逗号隔开 ...
- JS 数据类型转换以其他
JavaScript 是一种弱类型的语言,也就是没有类型限制,变量可以随时被赋予任意值. 同时,在程序运行过程中,类型会被自动确认的.因此,这就是涉及到数据的类型转换.在 JS 的世界中,数据类型转换 ...
- 【LOJ】#2131. 「NOI2015」寿司晚宴
题解 怎么NOI2015D1--全是一眼秒的sb题--然后我代码全都写跪一遍= = 要是NOI2015是IOI赛制我就可以AK啦(大雾) 代码能力直线下降,NOI2018滚粗预定了啊TAT 我是不是要 ...
- char *s 和char s[]的区别
char *s 和 char s[] 的区别小结 博客分类: C语言 c教育 . 最近的项目中有不少c的程序,在与项目新成员的交流中发现,普遍对于char *s1 和 char s2[] 认识有误区( ...
- 基于jquery扩展漂亮的CheckBox
大家都知道默认的html复选框控件样式可定义相当有限,无法满足大多用户的美观度.今天跟大家一起分享前一段时间自己编写的CheckBox控件.喜欢的朋友可以拿去使用,有什么好的建议希望你给我留言.废话不 ...
- MySQL表设计:每一种商品有不确定个数的属性
I personally would use a model similar to the following: The product table would be pretty basic, yo ...
- CodeForcs 797E Array Queries
$dp$预处理,暴力. 如果$k > sqrt(n)$,那么答案不会超过$sqrt(n)$,暴力模拟即可.如果$k <= sqrt(n)$,那么可以$dp$预处理打表. #include ...
- 初拾Java(问题二:缺类异常,无法编译)
昨天,在看JSP页面包含的元素(JSP指令,生命,表达式,动作等)时,拷贝了一个别人的例子来在Myeclipse里运行,结果出现了如下的缺类错误: 多调试两次也会出现如下无法编译的错误: 具体代码如下 ...
- express中间件的理解
参考 :https://blog.csdn.net/huang100qi/article/details/80220012 Express中间件分为三种内置中间件.自定义中间件.第三方中间件 可以与n ...
- 最短路 之 floyd 算法
Floyd 在我认为这是最短路算法中最简单的一个,也是最low的一个. 所以我们组一位大佬给他起了一个新的名字,叫做超时!!! (其实如果数据范围很小的话,这个算法还是蛮好用的!!) 这个算法比较简单 ...