题目大意:给出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. bug记录_signalr执行$.connnection.testhub结果为空

    最后发现配置文件<appSettings>中多了一句<add key="owin:AutomaticAppStartup" value="false&q ...

  2. MVC中发生System.Data.Entity.Validation.DbEntityValidationException验证异常的解决方法

    发生System.Data.Entity.Validation.DbEntityValidationException这个异常的时候,如果没有用特定的异常类去捕捉,是看不到具体信息的. 通常都是用Sy ...

  3. Payoneer官网注册教程,免费申请美国银行账号

    在我搞网赚项目的过程中,碰到境外收款付款的问题,起初我用Paypal贝宝,手续费高得惊人!相信做电商外贸的朋友深有体会.幸而发现了Paypal替代产品Payoneer,注册简单,手续费低,还有中文网站 ...

  4. vultr vps发布多用户管理功能

    中国用户购买海外vps,有一个麻烦之处是付款环节,可能你没有visa信用卡,vultr和digitalocean和linode这类vps不支持支付宝,给一些朋友带来不便.由此产生的vps代购行业,其实 ...

  5. shell笔记-local、export用法

    local一般用于局部变量声明,多在在函数内部使用.    1.    Shell脚本中定义的变量是global的,其作用域从被定义的地方开始,到shell结束或被显示删除的地方为止.    2.   ...

  6. manifest中的largeHeap是干什么用的?

    转 http://blog.csdn.net/jiaoyang623/article/details/8773445 今天群里有人讨论怎么给app分配超过100M的内存,有人亮出了largeHeap参 ...

  7. MySQL复制表结构,表数据。

    1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable; ...

  8. 第三十七节,hashlib加密模块

    在使用hashlib模块时需要先 import hashlib 引入模块 用于加密相关的操作,代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA ...

  9. spark java 代码example

    https://github.com/apache/spark/tree/master/examples/src/main/java/org/apache/spark/examples

  10. vb脚本自动更新版本信息

    使用的串口显示软件为secureCrt,支持脚本功能,今天写了一个简单的软件升级脚本(VB脚本). 如下: # $language = "VBScript" # $interfac ...