题目传送门

题意:有两种路径,每个点会分别在某一层,层相邻之间权值c.还有直接两点传送,花费w.问1到n的最短距离.

分析:1~n正常建边.然后n + a[i]表示i点在第a[i]层.然后再优化些就不会超时了.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. const int N = 2e5 + 5;
  8. const int E = N * 20;
  9. const int INF = 0x3f3f3f3f;
  10. struct Edge {
  11. int v, w, nex;
  12. Edge() {}
  13. Edge(int v, int w, int nex) : v (v), w (w), nex (nex) {}
  14. }edge[E];
  15. int head[N];
  16. int d[N];
  17. int a[N];
  18. bool vv[N];
  19. bool vis[N];
  20. int n, m, e;
  21.  
  22. void init(void) {
  23. memset (head, -1, sizeof (head));
  24. memset (vv, false, sizeof (vv));
  25. e = 0;
  26. }
  27.  
  28. void add_edge(int u, int v, int w) {
  29. edge[e] = Edge (v, w, head[u]);
  30. head[u] = e++;
  31. }
  32.  
  33. void SPFA(int s) {
  34. memset (vis, false, sizeof (vis));
  35. memset (d, INF, sizeof (d));
  36. d[s] = 0; vis[s] = true;
  37. queue<int> que; que.push (s);
  38. while (!que.empty ()) {
  39. int u = que.front (); que.pop ();
  40. vis[u] = false;
  41. for (int i=head[u]; ~i; i=edge[i].nex) {
  42. int v = edge[i].v, w = edge[i].w;
  43. if (d[v] > d[u] + w) {
  44. d[v] = d[u] + w;
  45. if (!vis[v]) {
  46. vis[v] = true; que.push (v);
  47. }
  48. }
  49. }
  50. }
  51. }
  52.  
  53. int main(void) {
  54. int T, cas = 0; scanf ("%d", &T);
  55. while (T--) {
  56. init ();
  57. int c; scanf ("%d%d%d", &n, &m, &c);
  58. for (int i=1; i<=n; ++i) {
  59. scanf ("%d", &a[i]);
  60. vv[a[i]] = true;
  61. }
  62. for (int i=1; i<n; ++i) {
  63. if (vv[i] && vv[i+1]) {
  64. add_edge (n+i, n+i+1, c);
  65. add_edge (n+i+1, n+i, c);
  66. }
  67. }
  68. for (int i=1; i<=n; ++i) {
  69. add_edge (n+a[i], i, 0);
  70. if (a[i] > 1) add_edge (i, n+a[i]-1, c);
  71. if (a[i] < n) add_edge (i, n+a[i]+1, c);
  72. }
  73. for (int u, v, w, i=1; i<=m; ++i) {
  74. scanf ("%d%d%d", &u, &v, &w);
  75. add_edge (u, v, w); add_edge (v, u, w);
  76. }
  77. SPFA (1);
  78. printf ("Case #%d: %d\n", ++cas, d[n] == INF ? -1 : d[n]);
  79. }
  80.  
  81. return 0;
  82. }

  

SPFA(建图) HDOJ 4725 The Shortest Path in Nya Graph的更多相关文章

  1. Hdu 4725 The Shortest Path in Nya Graph (spfa)

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

  2. HDU 4725 The Shortest Path in Nya Graph [构造 + 最短路]

    HDU - 4725 The Shortest Path in Nya Graph http://acm.hdu.edu.cn/showproblem.php?pid=4725 This is a v ...

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

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

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

  6. HDU 4725 The Shortest Path in Nya Graph

    he Shortest Path in Nya Graph Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged o ...

  7. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

  8. HDU 4725 The Shortest Path in Nya Graph( 建图 + 最短路 )

    主要是建图,建好图之后跑一边dijkstra即可. 一共3N个点,1~N是原图中的点1~N,然后把每层x拆成两个点(N+x)[用于连指向x层的边]和(N+N+x)[用于连从x层指出的边]. 相邻层节点 ...

  9. HDU 4725 The Shortest Path in Nya Graph (最短路 )

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

随机推荐

  1. Spring面向切面编程(AOP)方式二

    使用注解进行实现:减少xml文件的配置. 1 建立切面类 不需要实现任何特定接口,按照需要自己定义通知. package org.guangsoft.utils; import java.util.D ...

  2. 关闭Eclipse的控制台console自动跳出

    一.背景 在eclipse中进行开发,尤其是在后台有项目运行的时候,当有log或者错误需要打印到console中时,控制台就会被自动弹出,恰好这时候你又在编写代码,就会感觉瞬间想杀人,下面我们就来分享 ...

  3. [Android] adb 命令 dumpsys activity , 用来看 task 中的activity。 (uninstall virus)

    用“adb shell dumpsys activity”命令再来查看一下系统运行的的任务,就会看到: ACTIVITY MANAGER ACTIVITIES (dumpsys activity ac ...

  4. September 23rd 2016 Week 39th Friday

    Even a small star shines in the darkness. 星星再小,也会发光. In the darkness, even a small star can shine. N ...

  5. qt_计算器的简单实现

    //阶乘不知道怎么实现不了/(ㄒoㄒ)/~~,以后慢慢调试吧......... //转换为后缀表达式,实现最主要功能 void MainWindow::toPostfix () { QString e ...

  6. 第一课 移动端&响应式

    一.调试工具介绍(Chrome Emulation) 1.Device(设备相关) 自定义尺寸.Network(网络模拟).UseAgent(浏览器信息).缩放 2.Media(媒体) 3.Netwo ...

  7. php 投票系统练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Redis笔记(七)Java实现Redis消息队列

    这里我使用Redis的发布.订阅功能实现简单的消息队列,基本的命令有publish.subscribe等. 在Jedis中,有对应的java方法,但是只能发布字符串消息.为了传输对象,需要将对象进行序 ...

  9. .net学习之母版页执行顺序、jsonp跨域请求原理、IsPostBack原理、服务器端控件按钮Button点击时的过程、缓存、IHttpModule 过滤器

    1.WebForm使用母版页后执行的顺序是先执行子页面中的Page_Load,再执行母版页中的Page_Load,请求是先生成母版页的控件树,然后将子页面生成的控件树填充到母版页中,最后输出 2.We ...

  10. Filp Game

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25573   Accepted: 11052 题目链接: ...