题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1。问1到各个点之间的最短距离。

题目思路:SPFA求最短路

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<vector>
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<queue>
  8. #include<math.h>
  9. #include<map>
  10. #define INF 0x3f3f3f3f
  11. #define MAX 1000005
  12. #define Temp 1000000000
  13. #define MOD 1000000007
  14.  
  15. using namespace std;
  16.  
  17. int a[MAX],vis[MAX],dist[MAX],n,k;
  18.  
  19. struct node
  20. {
  21. int u,v,w,next;
  22. }G[MAX];
  23.  
  24. void Add(int u,int v,int w)
  25. {
  26. G[k].u=u;
  27. G[k].v=v;
  28. G[k].w=w;
  29. G[k].next=a[u];
  30. a[u]=k++;
  31. }
  32.  
  33. void SPFA()
  34. {
  35. queue<int>Q;
  36. int st=;
  37. vis[]=;
  38. dist[]=;
  39. Q.push(st);
  40. while(!Q.empty())
  41. {
  42. st=Q.front();
  43. Q.pop();
  44. vis[st]=;
  45. for(int i=a[st];i!=-;i=G[i].next)
  46. {
  47. int v=G[i].v;
  48. if(dist[v] > dist[st]+G[i].w)
  49. {
  50. dist[v]=dist[st]+G[i].w;
  51. if(!vis[v])
  52. {
  53. vis[v]=;
  54. Q.push(v);
  55. }
  56. }
  57. }
  58. }
  59. }
  60.  
  61. int main()
  62. {
  63. int q;
  64. while(scanf("%d",&n)!=EOF)
  65. {
  66. k=;
  67. memset(a,-,sizeof(a));
  68. memset(vis,,sizeof(vis));
  69. memset(dist,INF,sizeof(dist));
  70. for(int i=;i<=n;i++)
  71. {
  72. Add(i,i+,);
  73. Add(i+,i,);
  74. }
  75. for(int i=;i<=n;i++)
  76. {
  77. scanf("%d",&q);
  78. Add(i,q,);
  79. }
  80. SPFA();
  81. for(int i=;i<=n;i++)
  82. printf("%d%c",dist[i],i==n?'\n':' ');
  83. }
  84. return ;
  85. }

codeforces 689B Mike and Shortcuts 最短路的更多相关文章

  1. codeforces 689 Mike and Shortcuts(最短路)

    codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...

  2. CodeForces 689B Mike and Shortcuts (bfs or 最短路)

    Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...

  3. Codeforces 689B. Mike and Shortcuts SPFA/搜索

    B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...

  4. CodeForces 689B Mike and Shortcuts (BFS or 最短路)

    题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~

  5. codeforces 689B B. Mike and Shortcuts(bfs)

    题目链接: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input ...

  6. Codeforces Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  7. Codeforces Round #361 (Div. 2)——B. Mike and Shortcuts(BFS+小坑)

    B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  8. hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)

    hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...

  9. codeforces 547E Mike and Friends

    codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...

随机推荐

  1. aps.net 页面事件执行顺序

  2. ggplot2 theme相关设置—文本调整

    在geom设置和scale设置之后,要想把图画的漂亮,theme设置是比不可少的 在theme 设置中element_text()是一项很重要的内容 element_text(family = NUL ...

  3. hdu_5873_Football Games(xjb搞)

    题目链接:hdu_5873_Football Games 题意: 有n个队,每个队都会给其他队打一场,赢一场得2分,平局得一分,输了不得分,然后给你全部比赛结束后的得分,问你是否有假分 题解: 可以知 ...

  4. file_zilla 通过key连接远程服务器

    file_zilla 通过key连接 01 在putty中 ifconfig -a 查看当前网站ip02 文件-站点管理器--新建站点---主机ip 端口2203协议 SFTP 就是SSH协议04登录 ...

  5. php的实参和形参

    1.实参是调用函数时候的参数; 2.形参是声明函数时侯的参数, 例如 public function demo($a,$b) {         return ; } 如果声明的函数如上,调用时dem ...

  6. infix to postfix 完整版

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  7. ural 1356. Something Easier(数论,哥德巴赫猜想)

    1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...

  8. <meta http-equiv="X-UA-Compatible" content="IE=Edge">

    1.X-UA-Compatible X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中使用 ...

  9. Linux RCU机制详解

    关于rcu的几点声明: 1:RCU使用在读者多而写者少的情况.RCU和读写锁相似.但RCU的读者占锁没有任何的系统开销.写者与写写者之间必须要保持同步,且写者必须要等它之前的读者全部都退出之后才能释放 ...

  10. Hash算法冲突解决方法分析

    采用开放定址法处理散列表的冲突时,其平均查找长度?  高于链接法处理冲突 低于二分查找 开放定址法:一旦发生冲突,就去寻找下一个空的散列地址,只要散列地址够大,空的地址总会找到 链地址法: 一旦发生冲 ...