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. thymeleaf模版的使用

    thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果. ...

  2. python coding style guide 的高速落地实践

    python coding style guide 的高速落地实践 机器和人各有所长,如coding style检查这样的可自己主动化的工作理应交给机器去完毕,故发此文帮助你在几分钟内实现coding ...

  3. iOS自己定义对象保存到本地文件

    我是将聊天记录存到本地,里边用到了自己定义的对象.把数据转成Data格式存到本地.在转Data格式的时候报错了.这时候须要先将自己定义对象进行归档才干够转Data格式. 方法例如以下: 一.在.h文件 ...

  4. LINUX线程初探

     LINUX程序设计最重要的当然是进程与线程.本文主要以uart程序结合键盘输入控制uart的传输. 硬件平台:树莓派B+ 软件平台:raspberry 须要工具:USB转TTL(PL2303)+ ...

  5. Github的基本功能:

    作者:Fadeoc Khaos链接:http://www.zhihu.com/question/20070065/answer/30521531来源:知乎著作权归作者所有,转载请联系作者获得授权. G ...

  6. iOS_39_触摸解锁

    终于效果图: 控制器: // // BeyondViewController.m // 39_触摸解锁 // // Created by beyond on 14-9-17. // Copyright ...

  7. NVR硬件录像机web无插件播放方案功能实现之相关接口注意事项说明

    该篇博文主要用来说明EasyNVR硬件录像回放版本的相关接口说明和调用的demo: 方便用户的二次开发和集成. 软件根目录会包含接口文档的,因此,本文主要是对一些特定接口的说明和接口实现功能的讲解以及 ...

  8. Bootstrap aggregating Bagging 合奏 Ensemble Neural Network

    zh.wikipedia.org/wiki/Bagging算法 Bagging算法 (英语:Bootstrap aggregating,引导聚集算法),又称装袋算法,是机器学习领域的一种团体学习算法. ...

  9. a positive definite matrix

    https://en.wikipedia.org/wiki/Definite_quadratic_form https://www.math.utah.edu/~zwick/Classes/Fall2 ...

  10. Python爬虫-- BeautifulSoup库

    BeautifulSoup库 beautifulsoup就是一个非常强大的工具,爬虫利器.一个灵活又方便的网页解析库,处理高效,支持多种解析器.利用它就不用编写正则表达式也能方便的实现网页信息的抓取 ...