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. ruby OpenURI模块使用

    OpenURI is an easy-to-use wrapper for Net::HTTP, Net::HTTPS and Net::FTP(OpenURI支持重定向) 像打开普通文件那样打开ht ...

  2. python入门——Anaconda安装

    初学Python,可以选择python原始的IDE,但原始的IDE在使用过程中需要自己安装各种包,个人觉得初学者不需要将时间花在这些上面,而是应该直接学习python程序,这些比较杂的事情可以在以后的 ...

  3. c++实验3类和对象

     实 验 3: part 1:验证 part 2:graph #include <iostream> #include "graph.h" using namespac ...

  4. EF更新时出错,An error occurred while updating the entries. See the inner exception for details

           在使用EF进行更新数据时出错,报出的异常是 "An error occurred while updating the entries. See the inner excep ...

  5. java 判断上午/下午

    //结果为“0”是上午 结果为“1”是下午 public class GregorianTest { public static void main(String args[]) { Gregoria ...

  6. jmeter☞文件目录(一)

    Jmeter的文件目录如下图: 1.bin:可执行文件目录 a.jmeter.bat:Windows环境下的启动文件 b.jmeter.log:日志文件 c.jmeter.sh:Linux环境下的启动 ...

  7. Win10下Pytorch的安装和使用[斗之力三段]

    简介: 看到paper的代码是用Pytorch实现的,试图理解代码,但是看不懂,只能先学一些基础教程来帮助理解.笔记本电脑配置较低,所以安装一个没有CUDA的版本就可以了.安装完之后,就可以跟着教程边 ...

  8. [leetcode-646-Maximum Length of Pair Chain]

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  9. NO10——各种欧几里得

    int gcd(int n,int m)//n>m { //最大公约数 int r; while(m) { r = n%m; n = m; m = r; } return n; } int kg ...

  10. eniac世界第二台计算机

    ENIAC,全称为Electronic Numerical Integrator And Computer,即电子数字积分计算机.ENIAC是世界上第一台通用计算机,也是继ABC(阿塔纳索夫-贝瑞计算 ...