codeforces 689B Mike and Shortcuts 最短路
题目大意:给出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 最短路的更多相关文章
- codeforces 689 Mike and Shortcuts(最短路)
codeforces 689 Mike and Shortcuts(最短路) 原题 任意两点的距离是序号差,那么相邻点之间建边即可,同时加上题目提供的边 跑一遍dijkstra可得1点到每个点的最短路 ...
- 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 SPFA/搜索
B. Mike and Shortcuts time limit per test: 3 seconds memory limit per test: 256 megabytes input: sta ...
- CodeForces 689B Mike and Shortcuts (BFS or 最短路)
题目链接:http://codeforces.com/problemset/problem/689/B 题目大意: 留坑 明天中秋~
- codeforces 689B B. Mike and Shortcuts(bfs)
题目链接: B. Mike and Shortcuts time limit per test 3 seconds memory limit per test 256 megabytes input ...
- 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 ...
- 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 ...
- hdu4135-Co-prime & Codeforces 547C Mike and Foam (容斥原理)
hdu4135 求[L,R]范围内与N互质的数的个数. 分别求[1,L]和[1,R]和n互质的个数,求差. 利用容斥原理求解. 二进制枚举每一种质数的组合,奇加偶减. #include <bit ...
- codeforces 547E Mike and Friends
codeforces 547E Mike and Friends 题意 题解 代码 #include<bits/stdc++.h> using namespace std; #define ...
随机推荐
- 网页弹出窗口工具推荐之jqmodal
各种在jquery基础上实现的弹出窗口,有详细的开发说明文档.在项目中快速实现网页中弹出窗口的需求.基本能满足各种弹出窗口的需求 其官方网址如下http://jquery.iceburg.net/jq ...
- POJ 2240 Arbitrage(SPFA+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...
- jxls导出EXCEL模板
http://jxls.sourceforge.net/ InputStream templateInput = null; InputStream in = null; OutputStream o ...
- 【VMware】VMware Workstation中虚拟机网络配置
一直用的vmware的虚拟机以及UVP还有cirtix的虚拟机,然后对vmware workstation却一窍不通,这个怎么了得,下面介绍一下我学习中遇到的问题及解决办法. 一.准备工作: vmwa ...
- 字符串长度截取换行/n
/// <summary> /// 格式化字符串长度,超出部分显示省略号,区分汉字跟字母.汉字2个字节,字母数字一个字节 /// </summary> ...
- erlang nif小结
一.nif获取string参数的两种方式 1.eif_get_string 实例如下: static ERL_NIF_TERM erl_sm4_encrypt(ErlNifEnv* env, int ...
- G - 小晴天老师系列——可恶的墨水瓶
G - 小晴天老师系列——可恶的墨水瓶 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Othe ...
- AccessToMySql数据库的导入以及java生成.exe文件
一.AccessToMySql 最近做了一个Access数据库导入MySql的小工具,期间遇到诸多问题,这里小计一下. 表名为cur_rec,共有5个字段 比较奇葩的是这个表居然是四个字段的联合主键, ...
- MS13-069(CVE-2013-3205) CCaret use-after-free Vulnerability Analysis (2014.9)
MS13-069(CVE-2013-3205) CCaret use-after-free Vulnerability Analysis 1. Introduction In IE's standar ...
- redis3--key的操作
我们之前使用Redis简单存储了三个参数:在语句set name jack中,其中name就是一个key.我们Java中的变量名是有一定规则的,比如组成内容可以是"数字",&quo ...