题目大意:给出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 IO 四大附加接口、try-with-resource

    Java IO 四大附加接口.try-with-resource @author ixenos 四大附加接口 Closeable.Flushable.Readable.Appendable Close ...

  2. webapi中常用attribute标签

    HTTP Methods Instead of using the naming convention for HTTP methods, you can explicitly specify the ...

  3. A - 小彭玉的扫荡食堂计划

    A - 小彭玉的扫荡食堂计划 Time Limit: 20000/10000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others) ...

  4. Python字符串连接方式

    python中有很多字符串连接方式,总结一下: 1 最原始的字符串连接方式:str1 + str22 python 新字符串连接语法:str1, str23 奇怪的字符串方式:str1 str24 % ...

  5. 实现ie6下的居中

    代码如下所示,转自 http://w3help.org/zh-cn/causes/RT8003 对于 text-align 的讨论. <div style="width:200px; ...

  6. C/C++宏定义中#与##区别 .

    // #表示:对应变量字符串化// ##表示:把宏参数名与宏定义代码序列中的标识符连接在一起,形成一个新的标识符 #define U_BOOT_CMD_MKENT_COMPLETE(name,maxa ...

  7. Ant 删除目录 vs 清空目录

    Apache Ant 可以用下面的命令来删除目录 <delete dir="${lucene.dir}"/> 但是这会删除整个目录,而我现在只想清空目录内的所有文件和子 ...

  8. ThinkPad 禁用 触摸板

    执行 xinput wowk@wowk:~$ xinput ⎡ Virtual core pointer id=2 [master pointer (3)] ⎜ ↳ Virtual core XTES ...

  9. 《Windows驱动开发技术详解》之分层驱动程序

    分层驱动程序概念 分层的目的是将功能复杂的驱动程序分解成多个简单的驱动程序.一般来说,他们是指两个或两个 以上的驱动程序,它们分别创建设备对象,并且形成一个由高到低的设备对象栈.IRP请求一般会被传送 ...

  10. 《Windows驱动开发技术详解》之读写操作

    缓冲区方式读写操作 设置缓冲区读写方式: