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 ...
随机推荐
- loadrunner 分用户日志
loadrunner 分用户日志 loadrunner在run脚本时,模拟多用户并发场景下,通常需要分别关注每个用户的脚本执行日志,可以按照以下操作进行: 在“Run Load Tests”中选择需要 ...
- Mybatis异常处理之MySQL Connector Java] will not be managed by Spring
很长时间没写后台代码有点生疏了,这不今天又出点小插曲,写个文章记录下. 由于要上传点数据到后台,顺手整了个mybatis+springmvc.在保存数据时出现了异常. Creating a new S ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) G - Xor-matic Number of the Graph 线性基好题
G - Xor-matic Number of the Graph 上一道题的加强版本,对于每个联通块需要按位算贡献. #include<bits/stdc++.h> #define LL ...
- bzoj 1108
思路:水题, 将所有点按x轴对称反转,就变成了两堆点的坐标和的差.. #include<bits/stdc++.h> #define LL long long #define fi fir ...
- Python之路【第三篇】:文件操作
一.文件操作步骤 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 歌名:<大火> 演唱:李佳薇 作词:姚若龙 作曲:马奕强 歌词: 有座巨大的停了的时钟 倾倒在赶 ...
- COMP COMP-3
Comp (Computational) Comp (with no suffix) leaves the choice of the data type to the compiler writer ...
- 如何正确地使用Java的@deprecated标注
没有什么事情比看到一个没有任何说明的@deprecated标注更让人愤怒的事情了.这种做法只能让人困惑,我到底还要不要用这个已经‘废弃’的方法?如果开发者不希望某个方法再被人用的话,就要好好地为@de ...
- 【Python】单例模式Singleton
前两天一个面试被问到python中单例模式有几种实现方式,只答出了可以用元类实现...然后就想不起来了. 之后翻书,原来这些之前都见过的啊.... 1.手动实现真正创建实例的方法__new__()来实 ...
- HDU3853 LOOPS 期望DP 简单
http://acm.hdu.edu.cn/showproblem.php?pid=3853 有一点坑的地方是如果一个地方停在原地的概率为1,那么该地的期望为0,就相当于这个地方也是一个出口... ...
- CodeForces - 1017C The Phone Number
题面在这里! 一开始有一种构造猜想,可以把答案降到 sqrt(N) 级别. 考虑把 {1,2,...,n} 分成 sqrt(N) 段,每一段是连续的sqrt(N)个数.然后我们倒着把每一段数放上. 比 ...