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. 研发团队如何借助Gitlab来做代码review

    代码review是代码质量保障的手段之一,同时开发成员之间代码review也是一种技术交流的方式,虽然会占用一些时间,但对团队而言,总体是个利大于弊的事情.如何借助现有工具在团队内部形成代码revie ...

  2. mybatis的两种分页方式:RowBounds和PageHelper

    原理:拦截器. 使用方法: RowBounds:在mapper.java中的方法中传入RowBounds对象. RowBounds rowBounds = new RowBounds(offset, ...

  3. PHP网站常见安全漏洞 及相应防范措施总结

    一.常见PHP网站安全漏洞 对于PHP的漏洞,目前常见的漏洞有五种.分别是Session文件漏洞.SQL注入漏洞.脚本命令执行漏洞.全局变量漏洞和文件漏洞.这里分别对这些漏洞进行简要的介绍. 1.se ...

  4. 【转】【Pycharm大全】

    感谢:陈俊岭的程序员之路 [Pycharm大全]:http://blog.csdn.net/u013088062/article/details/50388329

  5. 献给写作者的 Markdown 新手指南及语法

    烈推荐所有写作者学习和掌握该语言.为什么?可以参考: 『为什么作家应该用 Markdown 保存自己的文稿』. 『Markdown写作浅谈』 让你专注于文字而不是排版. 标题 只需要在文本前面加上 # ...

  6. Error -27728: Step download timeout (120 seconds)的解决方法(转)

    LR中超时问题解决方法 超时错误在LoadRunner录制Web协议脚本回放时超时经常出现. 现象1:Action.c(16): Error -27728: Step download timeout ...

  7. 【BZOJ2083】[Poi2010]Intelligence test 二分

    [BZOJ2083][Poi2010]Intelligence test Description 霸中智力测试机构的一项工作就是按照一定的规则删除一个序列的数字,得到一个确定的数列.Lyx很渴望成为霸 ...

  8. EasyDSS高性能流媒体服务器前端重构(六)- webpack-dev-server 支持手机端访问

    很多时候,前端开发的页面,不仅要在PC端测试效果, 还要在手机端测试效果. 在开发阶段, 我们以 webpack-dev-server 来启动浏览器, 打开正在开发的页面. webpack-dev-s ...

  9. d[k]=eval(k)

    lk = ['oid', 'timestamp', 'signals', 'area', 'building', 'city', 'name', 'floor', 'industry', 'regio ...

  10. ES5中的类与继承

    最近在重新复习TypeScript,看到类这块的时候自然会和ES5中的类写法进行对比加深印象. 发现ES5的类与继承一些细节还是挺多的,时间久了容易忘记,特此记录下. 首先是ES5的类定义,这没什么好 ...