题目大意:给出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. Intellij Idea web项目的部署配置[转]

    原文地址:http://blog.csdn.net/z69183787/article/details/41416189 1.前言 2.项目配置(Project Structure) 2.1 Proj ...

  2. MVC5 + EF6酒店项目笔记

    最近项目组准备用MVC5开发酒店模板包括后台.在此第一次学MVC5一个礼拜,看着组长给我的MVC5模板从一脸懵逼到懵懂. 慢慢学习,成长. 未完待续........

  3. Java Object 构造方法的执行顺序

    Java Object 构造方法的执行顺序 @author ixenos 为了使用类而做的准备工作包括三个步骤 1)加载:类加载器查找字节码(一般在classpath中找),从字节码创建一个Class ...

  4. F#中的自定义隐式转换

    我们知道隐式变换在可控情况下会使代码变得简洁.熟悉C#的都知道C#中可以自定义隐式变换,例如 public class A { private int data; public static impl ...

  5. Arch最小化安装X

    Xorg xorg-server 安装xorg-server pacman -S xorg-server 可选:xorg-server-utils pacman -S xorg-server-util ...

  6. Linux下搭建ntp时间同步服务器

    1.ntpd软件安装(略过) 2.修改ntp.conf配置文件 vi /etc/ntp.conf 第一种配置:允许任何IP的客户机都可以进行时间同步将“restrict default kod nom ...

  7. php字符操作

    //一:定义字符串的方法 //1.双引号 //2.单引号 //3.heredoc语法结构 //heredoc语法定义字符串 $str=<<<TAG 我的武功终成武林盟主TAG;//注 ...

  8. SVG中image tag的高亮

    今天下午和晚上全部在实验室调试scs可视化,下午主要时间在调试字符云图的东西,不过感觉没有提升,还是不好看,囧~~ 把力道图中的线条给调整了,调细之后,好看了不少. 因为需要多个区域交互,也就是需要高 ...

  9. C# List 扩展排序

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comm ...

  10. 终端中出现While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/pod错误的修改方法

    把输入终端的sudo gem install cocoapods改为 sudo gem install -n /usr/local/bin/ cocoapods即可