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. 第十三周 Leetcode 363. Max Sum of Rectangle No Larger Than K(HARD)

    Leetcode363 思路: 一种naive的算法就是枚举每个矩形块, 时间复杂度为O((mn)^2), 可以做少许优化时间复杂度可以降低到O(mnnlogm), 其中m为行数, n为列数. 先求出 ...

  2. JeePlus:代码生成器

    ylbtech-JeePlus:代码生成器 1.返回顶部 1. 代码生成器Jeeplus代码生成器可以快速提高你的开发效率代码生成器可以0编码快速开发,通过配置生成数据库,mapper,service ...

  3. IOS各种调试

    IOS各种调试技巧豪华套餐   目录 前言逼优鸡知己知彼 百战不殆抽刀断Bug 普通操作 全局断点(Global BreakPoint) 条件断点(Condational Breakpoints)打印 ...

  4. PCB 批量Word转PDF实现方法

    自上次公司电脑中毒带来的影响,导致系统自动生成的Word档PCB出货报告,通过公司邮件服务器以附件的方式发送给客户后,客户是无法打开或打开缓慢的现象,如果将Word档转为PDF后在客户端是可以正常打开 ...

  5. bzoj 1577: [Usaco2009 Feb]庙会捷运Fair Shuttle【贪心+线段树】

    按结束时间排序,然后开个线段树,按照排序后的牛群贪心的选 贪心的依据是选哪头牛都是选,不如给后面的多省一点空间 #include<iostream> #include<cstdio& ...

  6. robotframework - 框架做接口自动化post请求

    1.做get请求之前先安装 Request库,参考github上链接 :https://github.com/bulkan/robotframework-requests/#readme 2.请求&a ...

  7. spring Cache /Redis 缓存 + Spring 的集成示例

    spring Cache https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ spring+redis 缓存 ht ...

  8. Ajax 知识点总结

    1.AJAX的优缺点都有什么? 最大的一点是页面无刷新,用户的体验非常好.使用异步方式与服务器通信,具有更加迅速的响应能力.可以把以前一些服务器负担的工作转嫁到客户端,利用客户端闲置的能力来处理,减轻 ...

  9. 关于Android皮肤更换分享

    http://www.eoeandroid.com/forum.php?mod=viewthread&tid=264902&highlight=%E6%8D%A2%E8%82%A4&a ...

  10. 题解报告:hdu 5695 Gym Class(拓扑排序)

    题目链接:acm.hdu.edu.cn/showproblem.php?pid=5695 Problem Description 众所周知,度度熊喜欢各类体育活动.今天,它终于当上了梦寐以求的体育课老 ...