CodeForces 689B【最短路】
题意:
给你一副图,给出的点两两之间的距离是abs(pos1-pos2),然后给你n个数是表示该pos到x的距离是1.
思路:
直接建边,跑spfa就好了。虽然说似乎题意说边很多,其实只要建一下相邻的点的边就好了,这样的图的性质还是得到了;
// d*##$.
// zP"""""$e. $" $o
//4$ '$ $" $
//'$ '$ J$ $F
// 'b $k $> $
// $k $r J$ d$
// '$ $ $" $~
// '$ "$ '$E $
// $ $L $" $F ...
// $. 4B $ $$$*"""*b
// '$ $. $$ $$ $F
// "$ R$ $F $" $
// $k ?$ u* dF .$
// ^$. $$" z$ u$$$$e
// #$b $E.dW@e$" ?$
// #$ .o$$# d$$$$c ?F
// $ .d$$#" . zo$> #$r .uF
// $L .u$*" $&$$$k .$$d$$F
// $$" ""^"$$$P"$P9$
// JP .o$$$$u:$P $$
// $ ..ue$" "" $"
// d$ $F $
// $$ ....udE 4B
// #$ """"` $r @$
// ^$L '$ $F
// RN 4N $
// *$b d$
// $$k $F
// $$b $F
// $"" $F
// '$ $
// $L $
// '$ $
// $ $
#include <bits/stdc++.h>
using namespace std;
typedef __int64 LL;
const int N=2e5+10;
struct asd{
int to;
LL w;
int next;
};
asd q[N*8];
int head[N*8],tol;
int n;
LL dis[N];
bool vis[N];
int num[N];
queue<int>que;
void spfa()
{
while(!que.empty())
que.pop();
for(int i=1;i<=n;i++)
{
vis[i]=num[i]=0;
dis[i]=1e15;
}
vis[1]=true;
num[1]++;
dis[1]=0;
que.push(1);
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=0;
for(int v=head[u];v!=-1;v=q[v].next)
{
int i=q[v].to;
if(dis[i]>dis[u]+q[v].w)
{
dis[i]=dis[u]+q[v].w;
if(!vis[i])
{
vis[i]=1;
que.push(i);
}
}
}
}
}
void add(int a,int b,LL c)
{
q[tol].to=b;
q[tol].w=c;
q[tol].next=head[a];
head[a]=tol++;
}
int main()
{
scanf("%d",&n);
int x;
memset(head,-1,sizeof(head));
tol=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
//add(x,i,1);
add(i,x,1);
}
for(int i=2;i<=n;i++)
{
add(i,i-1,1);
add(i-1,i,1);
}
spfa();
for(int i=1;i<=n;i++)
printf("%I64d ",dis[i]);
return 0;
}
CodeForces 689B【最短路】的更多相关文章
- CodeForces 689B Mike and Shortcuts (BFS or 最短路)
题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~
- CodeForces 689B Mike and Shortcuts (bfs or 最短路)
Mike and Shortcuts 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/F Description Recently ...
- codeforces 689B Mike and Shortcuts 最短路
题目大意:给出n个点,两点间的常规路为双向路,路长为两点之间的差的绝对值,第二行为捷径,捷径为单向路(第i个点到ai点),距离为1.问1到各个点之间的最短距离. 题目思路:SPFA求最短路 #incl ...
- CodeForces 300C 最短路
A Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Submit Status Pr ...
- Codeforces Gym100783H 最短路 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/CF-Gym100783H.html 题目传送门 - CF-Gym100783H 题意 给定一个 $n$ 个节点 ...
- President's Path CodeForces - 416E (最短路,计数)
大意: 给定无向图, 求任意两点间所有最短路经过的边数 刚开始想先用floyd跑出最短路, 然后在DAG上DP, 交了1发, 发现会算重复 贴一下题解的做法 #include <iostream ...
- Codeforces 689B. Mike and Shortcuts SPFA/搜索
B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...
- codeforces 757F - 最短路DAG+灭绝树
Description 给定一个n个点,m条边的带权无向图,和起点S.请你选择一个点u(u!=S),使得在图中删掉点u 后,有尽可能多的点到S的最短距离改变. Solution 先建出最短路DAG,在 ...
- Codeforces 1076D——最短路算法
题目 给你一个有n个顶点.m条边的无向带权图.需要擦除一些边使得剩余的边数不超过k,如果一个点在原始图到顶点1的最短距离为d,在删边后的图中到顶点的最短距离仍是d,则称这种点是 good.问如何删边, ...
随机推荐
- android lanchmode
http://www.cnblogs.com/xiaoQLu/archive/2012/07/17/2595294.html http://www.cnblogs.com/lwbqqyumidi/p/ ...
- 基于EasyDarwin云视频平台的幼儿园视频直播(手机直播/微信直播)解决方案
一.方案介绍 1.1.方案背景 在2016年10月25日至28日的安博会上,我们看到了不少的幼教平台厂商,我们注意到大部分的幼教平台,为了追求极佳的用户体验,在微信或者APP端能够做到极快的打开速度, ...
- the algebra of modulo-2 sums disk failure recovery
x=y x_+_y=0 The bit in any position is the modulo-2 sum of all the bits in the corresponding positio ...
- Python pandas 获取Excel重复记录
pip install pandas pip install xlrd 大量记录的时候,用EXCEL排序处理比较费劲,EXCEL程序动不动就无响应了,用pands完美解决. # We will use ...
- Kafka理论学习
Kafka Consumer设计解析 http://www.jasongj.com/2015/08/09/KafkaColumn4/
- LightOJ1138 —— 阶乘末尾0、质因子分解
题目链接:https://vjudge.net/problem/LightOJ-1138 1138 - Trailing Zeroes (III) PDF (English) Statistic ...
- 让ansbile和docker愉快的在一起
引自: http://cloud.51cto.com/art/201510/494328.htm
- RTree算法Java实现 JSI RTree Library的调用实例 标签:jsi-rtree-library
1. [代码]jsi-rtree-library /** * */package com.mycompany.project; //package net.sourceforge.jsi.examp ...
- Idea中的插件-列出Java Bean的所有set方法
插件的git 地址: https://github.com/yoke233/genSets 将插件jar导入idea中,使用方式是对象后加.allset,然后回车.
- hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(Bash Game)
题意:从0开始捐款,每次不超过m元,首先达到n元的获胜 思路:等同于从n开始,每次取不超过m,首先达到0的获胜.(Bash Game) #include<iostream> #includ ...