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 ...
随机推荐
- aps.net 页面事件执行顺序
- ggplot2 theme相关设置—文本调整
在geom设置和scale设置之后,要想把图画的漂亮,theme设置是比不可少的 在theme 设置中element_text()是一项很重要的内容 element_text(family = NUL ...
- hdu_5873_Football Games(xjb搞)
题目链接:hdu_5873_Football Games 题意: 有n个队,每个队都会给其他队打一场,赢一场得2分,平局得一分,输了不得分,然后给你全部比赛结束后的得分,问你是否有假分 题解: 可以知 ...
- file_zilla 通过key连接远程服务器
file_zilla 通过key连接 01 在putty中 ifconfig -a 查看当前网站ip02 文件-站点管理器--新建站点---主机ip 端口2203协议 SFTP 就是SSH协议04登录 ...
- php的实参和形参
1.实参是调用函数时候的参数; 2.形参是声明函数时侯的参数, 例如 public function demo($a,$b) { return ; } 如果声明的函数如上,调用时dem ...
- infix to postfix 完整版
#include<iostream> #include<stack> #include<string> #include<deque> using na ...
- ural 1356. Something Easier(数论,哥德巴赫猜想)
1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...
- <meta http-equiv="X-UA-Compatible" content="IE=Edge">
1.X-UA-Compatible X-UA-Compatible是IE8的一个专有<meta>属性,它告诉IE8采用何种IE版本去渲染网页,在html的<head>标签中使用 ...
- Linux RCU机制详解
关于rcu的几点声明: 1:RCU使用在读者多而写者少的情况.RCU和读写锁相似.但RCU的读者占锁没有任何的系统开销.写者与写写者之间必须要保持同步,且写者必须要等它之前的读者全部都退出之后才能释放 ...
- Hash算法冲突解决方法分析
采用开放定址法处理散列表的冲突时,其平均查找长度? 高于链接法处理冲突 低于二分查找 开放定址法:一旦发生冲突,就去寻找下一个空的散列地址,只要散列地址够大,空的地址总会找到 链地址法: 一旦发生冲 ...