题目大意:给出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. hdu_5821_Ball(贪心)

    题目链接:hdu_5821_Ball 题意: 给你序列A和序列B,依次给你m个区间,每次你可以交换区间内的任意数,问最后能不能将A变成B 题解: 比赛当时没想到,智商被压制 1001: 假设有4个红球 ...

  2. asp.net学习视频资料地址链接

    ASP.NET开发学习视频教程大全(共800集) http://felix520wj.blog.51cto.com/7129746/1548458 http://study.163.com/cours ...

  3. [SOJ] 1282. Computer games (KMP)

    坑爹题 1282. Computer Game Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Brian is an ...

  4. Lucene基础(2)

    上一篇:Lucene基础(1) 一.Lucene术语 Document, Field, Term, Query, Analyzer相信在其中大多数在之前已经理解了...对其中部分概念详细说明 Docu ...

  5. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home

    求1到N的最短路 注意有重边 跑一遍dijkstra就行 /* *********************************************** Author :Sun Yuefeng ...

  6. incallui中如何查询联系人数据

    联系人信息显示在CallCard中,提示当前正在通话的联系人号码.姓名.头像.号码类型等信息: 代码中在两个地方发起对当前联系人的查询, Init():startContactInfoSearch(c ...

  7. 简单的javasrcipt选项卡

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta char ...

  8. Smarty自定义函数

    自定义函数:<{方法名称}> 在html页面是可以直接赋值的:(没啥作用只是知道即可) <{$a = "hello"}><div><{$a ...

  9. Maven快速搭建GUI项目

    一.eclipse安装好maven插件,并将maven集成到eclipse之后,用maven的archetype,搭建好一个maven-archetype-queckstart项目的骨架. 二.可执行 ...

  10. laravel性能优化

    1. 配置信息缓存 使用以下 Artisan 自带命令,把 config 文件夹里所有配置信息合并到一个文件里,减少运行时文件的载入数量: php artisan config:cache 上面命令会 ...