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 <= 10 5) and C(1 <= C <= 10 3), which is the number of nodes, the number of extra edges and cost of moving between adjacent layers.        The second line has N numbers l i (1 <= l i <= N), which is the layer of i th node belong to.        Then come N lines each with 3 numbers, u, v (1 <= u, v < =N, u <> v) and w (1 <= w <= 10 4), 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

题目大意是是有n层。然后每层会有部分点,点与点可以通过特点的代价互达,相邻层之间的点可以通过C代价到达。

题目一开始没有看到每层可以有多个点。还有需要注意的是同层之间的点不能直接0代价到达。

之前在没考虑同层间不能直接0代价互达的时候,是把每个层看作一个节点,这样某层的节点到这个节点的代价都是0。

如果考虑到这个条件的话,

所以需要构造出下面这样的路径:

为每层设置From和To节点。某层的节点只能从From节点前往,或者前往To节点。

而相邻层之间亦是如此。

这样就能避免同层代价为0这个问题了。

为了避免点的冲突,From从n开始往后,To从2n开始往后。

需要注意的是这样操作以后点的数目上界会变成3N,线段就是6N。

这里采用链式前向星存图,采用STL优先队列的SPFA。

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <vector>
#define N 300005 using namespace std; typedef pair<int, int> pii; struct Edge
{
int to;
int next;
int val;
}edge[*N]; int head[N], cnt; void addEdge(int u, int v, int w)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
edge[cnt].val = w;
head[u] = cnt;
cnt++;
} int dis[N];
bool vis[N];
int n, m, c; inline int idFrom(int i)
{
return i + n;
} inline int idTo(int i)
{
return i + *n;
} void Input()
{
memset(head, -, sizeof(head));
memset(dis, -, sizeof(dis));
memset(vis, , sizeof(vis));
dis[] = ;
cnt = ;
int k, v, w;
scanf("%d%d%d", &n, &m, &c);
for (int i = ; i <= n; ++i)
{
scanf("%d", &k);
addEdge(idFrom(k), i, );
addEdge(i, idTo(k), );
}
for (int i = ; i < n; ++i)
{
addEdge(idTo(i), idFrom(i+), c);
addEdge(idTo(i+), idFrom(i), c);
}
for (int i = ; i < m; ++i)
{
scanf("%d%d%d", &k, &v, &w);
addEdge(k, v, w);
addEdge(v, k, w);
}
} void Work()
{
pii k;
priority_queue <pii, vector<pii>, greater<pii> > q;
q.push(pii(, ));
while (!q.empty())
{
k = q.top();
q.pop();
int x = k.second;
if (vis[x])
continue;
vis[x] = true;
for (int i = head[x]; i != -; i = edge[i].next)
{
if (dis[edge[i].to] != - &&
dis[edge[i].to] <= dis[x] + edge[i].val)
continue;
if (vis[edge[i].to])
continue;
dis[edge[i].to] = dis[x] + edge[i].val;
q.push(pii(dis[edge[i].to], edge[i].to));
}
}
printf("%d\n", dis[n]);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case #%d: ", times);
Input();
Work();
}
return ;
}

ACM学习历程—HDU4725 The Shortest Path in Nya Graph(SPFA && 优先队列)的更多相关文章

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

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

  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. Hdu 4725 The Shortest Path in Nya Graph (spfa)

    题目链接: Hdu 4725 The Shortest Path in Nya Graph 题目描述: 有n个点,m条边,每经过路i需要wi元.并且每一个点都有自己所在的层.一个点都乡里的层需要花费c ...

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

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

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

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

  6. HDU4725 The Shortest Path in Nya Graph dij

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

  7. hdu4725 The Shortest Path in Nya Graph

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

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

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

  9. HDU4725: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. java Map 实现类的对比

    java为数据结构中的映射定义了一个接口 java.util.Map ,他有四个实现类

  2. 《JavaScript》——DOM

    DOM (Document Object Model) 即文档对象模型, 针对 HTML 和 XML 文档的 API (应用程序接口) .DOM 描绘了一个层次化的节点树,执行开发者加入.移除和改动页 ...

  3. conda清华源 pip 清华源ubuntu 清华镜像 R代理以及包的安装

    vim     ~/.condarc channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/ - https:// ...

  4. 底部TabsFooter

    Demo简单描述:点击底部菜单可切换页面,并且底部为共用. 这个是在设置好导航Navigator之后进行的步骤,只是我个人进行Tab切换的一种思路方法,或许不是最好的,仅供参考一下. 首先我们需要一个 ...

  5. servletRequest 常用操作

    package request; import java.io.IOException;import javax.servlet.ServletException;import javax.servl ...

  6. JavaScrip函数与声明表达式

    首先我们看下函数的两种命名方式 1.函数声明,声明一个函数 function test1(){ var a=0; console.log(a); //左一些操作... } 执行结果如下 我们看一下,无 ...

  7. hadoop 相关工具访问端口(转)

    原文:http://www.tuicool.com/articles/BB3eArJ hadoop系统部署时用到不少端口.有的是Web UI所使用的,有的是内部通信所使用的,有的是监控所使用的.实际系 ...

  8. windows 2008配置运行PHP5.5.X

    1.安装web5.0平台安装程序.web5.0平台安装程序:http://www.iis.net/downloads (实际上更方便的是用WebPlalform安装PHP:http://www.mic ...

  9. WCF基础之配置服务

    在WCF应用编程中配置服务是其主要部分. 配置可以定义和自定义如何向客户端公开服务,包括服务地址,发送和接受消息的传输和编码,以及服务的安全类型. 服务的配置有两种:编码和使用config文件,大多数 ...

  10. Struts2常见面试点

    01.  三层和MVC的区别 http://blog.csdn.net/csh624366188/article/details/7183872 http://www.cnblogs.com/zdxs ...