codeforces 689 Mike and Shortcuts(最短路)

原题

  1. 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边
  2. 跑一遍dijkstra可得1点到每个点的最短路,时间复杂度是O(mlogm)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int Max=200000+10;
int abs(int x) {return x>=0?x:-x;}
int n,m;
struct Edge{
int from,to,dist;
Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
vector <Edge> edges;
vector <int> G[Max];
int d[Max];
void Init()
{
for(int i=0;i<=n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back(Edge(from,to,dist));
m=edges.size();
G[from].push_back(m-1);
}
struct node
{
int d,u;
node(int dd,int uu):d(dd),u(uu){};
bool operator < (const node & rhs) const {
return d>rhs.d;
}
};
const int INF=0x3f3f3f3f;
bool done[Max];
void Dijkstra(int s)
{
priority_queue<node>Q;
for(int i=1;i<=n;i++) d[i]=INF;
d[s]=0;
memset(done,0,sizeof(done));
Q.push(node(0,s));
while(!Q.empty())
{
node x=Q.top();Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<G[u].size();i++)
{
Edge &e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
Q.push(node(d[e.to],e.to));
}
}
}
}
int main()
{
while(~scanf("%d",&n))
{
int x;
Init();
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
AddEdge(i-1,i,1);
AddEdge(i+1,i,1);
AddEdge(i,x,1);
}
Dijkstra(1);
for(int i=1;i<=n;i++)
{
if(i!=1) printf(" ");
printf("%d",d[i]);
}
puts("");
}
return 0;
}

codeforces 689 Mike and Shortcuts(最短路)的更多相关文章

  1. codeforces 689B Mike and Shortcuts 最短路

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

  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 Round #361 (Div. 2) B. Mike and Shortcuts bfs

    B. Mike and Shortcuts 题目连接: http://www.codeforces.com/contest/689/problem/B Description Recently, Mi ...

  6. 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 ...

  7. codeforces 689B B. Mike and Shortcuts(bfs)

    题目链接: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input ...

  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. setsockopt 设置socket 详细用法(转载)

    转自:http://www.cppblog.com/killsound/archive/2009/01/16/72138.html 1.closesocket(一般不会立即关闭而经历TIME_WAIT ...

  2. Objective-C NSObject与运行时系统

    创建: 2018/01/26 完成: 2018/02/02 遗留: TODO  NSObject  运行时系统 程序运行时候起作用的系统? NSObject是运行时系统的接口(API)  属性 只有一 ...

  3. 【STM32H7教程】第22章 STM32H7的SysTick实现多组软件定时器

    完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第22章       STM32H7的SysTick实现 ...

  4. 洛谷2019 3月月赛 T2

    题干 洛谷同款 T2?(看似比T1简单些) 二维前缀和嘛?[多简单 我天天拿二维前缀和水DP] 这是前缀和的预处理 2333 处理出来所有的情况 某个地方要加上mod再%mod 如果没有这一步 那么 ...

  5. [C陷阱和缺陷] 第3章 语义“陷阱”

    第3章 语义"陷阱"     一个句子哪怕其中的每个单词都拼写正确,而且语法也无懈可击,仍然可能有歧义或者并非书写者希望表达的意思.程序也有可能表面上是一个意思,而实际上的意思却相 ...

  6. 2017杭电多校第五场Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. SQL优化器简介

    文章导读: 什么是RBO? 什么是CBO? 我们在工作中经常会听到这样的声音:"SQL查询慢?你给数据库加个索引啊".虽然加索引并不一定能解决问题,但是这初步的体现了SQL优化的思 ...

  8. java用匿名内部类实现多线程堆内存变量共享

    匿名内部类介绍:http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html 用Runnable模拟实现共享堆内存变量 import ...

  9. LN : leetcode 513 Find Bottom Left Tree Value

    lc 513 Find Bottom Left Tree Value 513 Find Bottom Left Tree Value Given a binary tree, find the lef ...

  10. php-5.3源码编译autoconf版本不符合解法

    1. 网上下载符合版本的autoconf 2. 卸载本地原本的autoconf 3. 解压autoconf 后进入目录 ./configure && make && s ...