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

题目思路:SPFA求最短路

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0x3f3f3f3f
#define MAX 1000005
#define Temp 1000000000
#define MOD 1000000007 using namespace std; int a[MAX],vis[MAX],dist[MAX],n,k; struct node
{
int u,v,w,next;
}G[MAX]; void Add(int u,int v,int w)
{
G[k].u=u;
G[k].v=v;
G[k].w=w;
G[k].next=a[u];
a[u]=k++;
} void SPFA()
{
queue<int>Q;
int st=;
vis[]=;
dist[]=;
Q.push(st);
while(!Q.empty())
{
st=Q.front();
Q.pop();
vis[st]=;
for(int i=a[st];i!=-;i=G[i].next)
{
int v=G[i].v;
if(dist[v] > dist[st]+G[i].w)
{
dist[v]=dist[st]+G[i].w;
if(!vis[v])
{
vis[v]=;
Q.push(v);
}
}
}
}
} int main()
{
int q;
while(scanf("%d",&n)!=EOF)
{
k=;
memset(a,-,sizeof(a));
memset(vis,,sizeof(vis));
memset(dist,INF,sizeof(dist));
for(int i=;i<=n;i++)
{
Add(i,i+,);
Add(i+,i,);
}
for(int i=;i<=n;i++)
{
scanf("%d",&q);
Add(i,q,);
}
SPFA();
for(int i=;i<=n;i++)
printf("%d%c",dist[i],i==n?'\n':' ');
}
return ;
}

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. Java Lambda表达式入门[转]

    原文链接: Start Using Java Lambda Expressions http://blog.csdn.net/renfufei/article/details/24600507 下载示 ...

  2. javascript apply()和call()

    原文链接 http://www.jb51.net/article/30883.htm 想要理解透彻apply()和call() ,还要需要理解this  作用域 局部变量  全局变量 js apply ...

  3. Bullet_Point_Py

    1. if __name__ == "__main__" Term: 模块最高级的代码:模块中没有缩进的代码 Principal 1: 第一次导入模块会执行模块最高级的代码.不管你 ...

  4. Understanding continuations

    原文地址http://fsharpforfunandprofit.com/posts/computation-expressions-continuations/ 上一篇中我们看到复杂代码是如何通过使 ...

  5. 4、Web应用程序中的安全向量 -- over-posting(重复提交)

    模型绑定是ASP.NET MVC提供的强大功能,可遵照命名约定将输入元素映射到模型属性,从而极大地简化了处理用户输入的过程,然而,这也成为了攻击的另一种没接,给攻击者提供了一个填充模型属性的机会,右下 ...

  6. [SOJ]1753 解码

    1753. 解码 Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description ZX是另一头04级的牛,他现在在UPen.他跟LLK经 ...

  7. hdu2063 匈牙利算法 二分最大匹配模版题

    过山车 Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class na ...

  8. C++著名程序库的比较和学习经验(STL.Boost.GUI.XML.网络等等)

    1.C++各大有名库的介绍--C++标准库 2.C++各大有名库的介绍--准标准库Boost 3.C++各大有名库的介绍--GUI 4.C++各大有名库的介绍--网络通信 5.C++各大有名库的介绍- ...

  9. ADO.NET 数据访问类查询、属性扩展

    今天,我首先在之前所做的人员管理系统的基础上,利用数据访问类进行了所有人员信息的查询. 主程序代码: List<Users> Ulist = new UsersData().Select( ...

  10. linux 内核协议栈收报流程(二)Netfilter全貌

    ip层分片整理 int ip_local_deliver(struct sk_buff *skb){ /* * Reassemble IP fragments. */ if (ip_is_fragme ...