The Shortest Path in Nya Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13070    Accepted Submission(s): 2794

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725

Description:

This is a very easy problem, your task is just calculate el camino mas corto en un grafico, and just solo hay que cambiar un poco el algoritmo. If you do not understand a word of this paragraph, just move on.
The Nya graph is an undirected graph with "layers". Each node in the graph belongs to a layer, there are N nodes in total.
You can move from any node in layer x to any node in layer x + 1, with cost C, since the roads are bi-directional, moving from layer x + 1 to layer x is also allowed with the same cost.
Besides, there are M extra edges, each connecting a pair of node u and v, with cost w.
Help us calculate the shortest path from node 1 to node N.

Input:

The first line has a number T (T <= 20) , indicating the number of test cases.
For each test case, first line has three numbers N, M (0 <= N, M <= 105) and C(1 <= C <= 103), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.
The second line has N numbers li (1 <= li <= N), which is the layer of ith node belong to.
Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 104), which means there is an extra edge, connecting a pair of node u and v, with cost w.

Output:

For test case X, output "Case #X: " first, then output the minimum cost moving from node 1 to node N.
If there are no solutions, output -1.

Sample Input:

2 3 3 3 1 3 2 1 2 1 2 3 1 1 3 3 3 3 3 1 3 2 1 2 2 2 3 2 1 3 4

Sample Output:

Case #1: 2

Case #2: 3

题意:

给出一个分层图,每个点只属于一层,点与点之间到达有一定花费,然后相邻两层移动也有一定花费。最后问从1号点到n号点的最小花费是什么。

题解:

朴素的想法就是点与点之间连边,层与层之间连边,但是空间会爆掉。

于是就像将“层”给分离出去,如果点i属于x层,就往n+x连边,这样会节约很多的空间。

但是这也有一个问题,就是跑最短路的时候点跑向相应的层,然后又跑回来。

所以我们考虑只连出边/入边,然后层直接与点相连。

具体代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 2e5+;
int n,m,c,t,tot;
int head[N],d[N],vis[N],belong[N],lay[N];
struct Edge{
int v,w,next ;
}e[N<<];
struct node{
int d,u;
bool operator < (const node &A)const{
return d>A.d;
}
};
void adde(int u,int v,int w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
void Dijkstra(int s){
priority_queue <node> q;memset(d,INF,sizeof(d));
memset(vis,,sizeof(vis));d[s]=;
q.push(node{,s});
while(!q.empty()){
node cur = q.top();q.pop();
int u=cur.u;
if(vis[u]) continue ;
vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
q.push(node{d[v],v});
}
}
}
}
int main(){
cin>>t;
int cnt =;
while(t--){
cnt++;
scanf("%d%d%d",&n,&m,&c);
memset(head,-,sizeof(head));tot=;
memset(belong,,sizeof(belong));
memset(lay,,sizeof(lay));
for(int i=,tmp;i<=n;i++){
scanf("%d",&tmp);
lay[tmp]=;
belong[i]=tmp;
}
for(int i=;i<=n;i++){
if(!lay[i] || !lay[i-]) continue ;
adde(i+n,i+n-,c);
adde(i+n-,i+n,c);
}
for(int i=;i<=n;i++){
int tmp = belong[i];
adde(n+tmp,i,);
if(tmp>) adde(i,tmp+n-,c);
if(tmp<n) adde(i,tmp+n+,c);
//adde(i,n+tmp,0);
}
for(int i=,u,v,w;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);adde(v,u,w);
}
Dijkstra();
printf("Case #%d: ",cnt);
if(d[n]==INF) puts("-1");
else cout<<d[n]<<endl;
}
return ;
}

HDU4725:The Shortest Path in Nya Graph(最短路)的更多相关文章

  1. HDU-4725 The Shortest Path in Nya Graph 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 如果直接建图复杂度过大,但是考虑到每层之间的有效边很少,只要在每层增加两个虚拟节点n+i和2*n ...

  2. HDU-4725 The Shortest Path in Nya Graph (拆点+dji)

    HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...

  3. ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  4. HDU4725 The Shortest Path in Nya Graph SPFA最短路

    典型的最短路问题,但是多了一个条件,就是每个点属于一个layer,相邻的layer移动,如x层移到x+1层需要花费c. 一种显而易见的转化是我把这些边都建出来,但是最后可能会使得边变成O(n^2); ...

  5. HDU4725 The Shortest Path in Nya Graph dij

    分析:对于每一层,原来n个点,然后扩展为原来的三倍,每一层扩展一个入点,一个出点,然后跑最短路 注:tmd我把一个n写成m了,然后wa了7次,我都要怀疑人生了 #include<cstdio&g ...

  6. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...

  7. hdu4725 The Shortest Path in Nya Graph

    这道题看了下很多人都是把每一层拆成两个点然后建图做的. 我的思路很直接,也不用建图,直接在更新每个点时更新他相邻的边和相邻的层,当然前提是每个点只更新一次,每个层也只更新一次,这样才能确保时间复杂度. ...

  8. 2013成都邀请赛J称号||HDU4725 The Shortest Path in Nya Graph(spfa+slf最短的优化)

    职务地址:HDU 4725 这题卡了好长时间了,建图倒是会建,可是不会最短路的算法优化,本以为都须要堆去优化的,打算学了堆之后再来优化.可是昨晚CF的一道题..(那题也是不优化过不了..)然后我就知道 ...

  9. 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 ...

随机推荐

  1. QOS-CBQ概述

     QOS-CBQ概述 2018年7月7日    19:56 CBQ(基于类的对列)是一种基于QOS policy实现的拥塞管理技术. CBQ中包含一个LLQ(低延迟队列),用来支撑EF(快速转发)类业 ...

  2. POJ2186 强连通分量+缩点

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40234   Accepted: 16388 De ...

  3. 内存泄漏导致程序killed

    示例程序: #include<stdio.h> #include<unistd.h> int main() { ) { *]; } ; } 执行结果: 程序消耗完内存会被kil ...

  4. git将本地项目上传到远程仓库

    1.cd mygit 打开项目文件夹 2.git init 初始化git 3.git remote add origin xxx(远程仓库地址)  添加远程库   git remote -v 查看远程 ...

  5. python基础之多线程

    概念 进程:进程就是一个程序在一个数据集上的一次动态执行过程 程序:代码 数据集:程序执行过程中需要的资源 进程控制块:完成状态保存的单元 线程:线程是寄托在进程之上,为了提高系统的并发性 线程是进程 ...

  6. 4364: [IOI2014]wall砖墙

    4364: [IOI2014]wall砖墙 链接 分析: 线段树,维护一个最大值,一个最小值. 代码: #include<bits/stdc++.h> ],*p1 = buf,*p2 = ...

  7. HI2115软件开发板V150版本AT+NSOST指令

    1. 在HI2115里面,由于内存空间比较大,所以支持UDP发送指令AT+NSOST的分包 ret = sendto(socket, seq_num, data_string, length, msg ...

  8. OrCAD把原理图中的器件添加到原理图库

    1. 在使用OrCAD的时候,有时需要把别人的原理图里面的器件添加到自己的原理图库,方便以后使用,具体操作如下,依次选择Design Cache---元器件--Copy 2. 选中要存放的原理图库,鼠 ...

  9. VIN码识别:助力汽车后市场转型升级

    随着中国汽车市场的成熟,汽车后市场发展迅速,呈“井喷”式增长.据最新数据统计,2015年,中国汽车后市场产值突破8000亿规模,到2018年有望突破万亿. 所谓汽车后市场是指汽车销售以后,围绕汽车使用 ...

  10. 虚拟现实-VR-UE4-获取UE4

    UE4现在虽然是开源,但是并不是免费的,在你的游戏成功后,回收取5%费用和每个月19美元的费用 所以,第一步,进去UE4官网:https://www.unrealengine.com/zh-CN/wh ...