最短路N题Tram SPFA
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <queue>
#include <vector>
#define N 1010
#define INF 0x3f3f3f3f
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
int head[N];
int dis[N];
int n;
bool f[N];
struct node
{
int s, e, w, next;
}ans[N];
void Init()
{
for(int i = 0; i <= n; i++) {
dis[i] = INF;
f[i] = 0;
head[i] = -1;
}
}
void Add(int s, int e, int w, int index)
{
ans[index].s = s;
ans[index].e = e;
ans[index].w = w;
ans[index].next = head[s];
head[s] = index;
}
void Spfa(int a, int b)
{
queue<int>q;
q.push(a);
f[a] = 1;
dis[a] = 0;
while(q.size()) {
int p = q.front(); q.pop();
f[p] = 0;
for(int i = head[p]; i != -1; i = ans[i].next) {
if(dis[p] + ans[i].w < dis[ans[i].e]) {
dis[ans[i].e] = dis[p] + ans[i].w;
if(!f[ans[i].e]) {
f[ans[i].e] = 1;
q.push(ans[i].e);
}
}
}
}
if(dis[b] == INF) printf("-1\n");
else
printf("%d\n", dis[b]);
}
int main()
{
int a, b;
while(~scanf("%d %d %d", &n, &a, &b)) {
int m, g, index = 0;
Init();
for(int i = 1; i <= n; i++) {
scanf("%d %d", &m, &g);
Add(i, g, 0, index++);
for(int j = 1; j < m; j++) {
scanf("%d", &g);
Add(i, g, 1, index++);
}
}
Spfa(a, b);
}
}
最短路N题Tram SPFA的更多相关文章
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- hdu-3790最短路刷题
title: hdu-3790最短路刷题 date: 2018-10-20 14:50:31 tags: acm 刷题 categories: ACM-最短路 概述 一道最短路的水题,,,尽量不看以前 ...
- 牛客小白月赛6 I 公交线路 最短路 模板题
链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...
- POJ 4046 Sightseeing 枚举+最短路 好题
有n个节点的m条无向边的图,节点编号为1~n 然后有点权和边权,给出q个询问,每一个询问给出2点u,v 输出u,v的最短距离 这里的最短距离规定为: u到v的路径的所有边权+u到v路径上最大的一个点权 ...
- hdoj2544 最短路(Dijkstra || Floyd || SPFA)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路 最短路算法模板题,求解使用的Dijkstra算法.Floyd算法.SPFA算法可以当做求解 ...
- 【bzoj2118&洛谷P2371】墨墨的等式(最短路神仙题)
题目传送门:bzoj2118 洛谷P2371 这道题看了题解后才会的..果然是国家集训队的神仙题,思维独特. 首先若方程$ \sum_{i=1}^{n}a_ix_i=k $有非负整数解,那么显然对于每 ...
- POJ1847 Tram SPFA算法变形
原题地址:http://poj.org/problem?id=1847 Tram:有轨电车 这题就是构造一个有向无权图,然后每一个点都会有一个开关,这个开关指向他的其中一个出度.当途经这个点的时候,如 ...
随机推荐
- 高抛低吸T+0操作要领(目前行情短线炒作的必备技能)
最近的行情只能用操蛋来形容,但是危机中不乏机会.现在已经不是之前行情的思路,那着一个股票长线抱着,即使是好的牛股,也经不起目前行情的这 么折腾.所以,现在最适合的操作方式就是高抛低吸.今天低吸保不准明 ...
- source sh运行脚本的差别
主要有两种方式运行shell脚本 1)source test.bsh 2)sh test.bsh 1)souce运行脚本文件会在父程序中运行.各项动作都会在原本的bash内生效.运行过程不另开进程.脚 ...
- eureka集群高可用配置,亲测成功配置(转)
转自大神的文章:https://blog.csdn.net/tianyaleixiaowu/article/details/78184793 网上讲这个东西的很多,抄来抄去的,大部分类似,多数没讲明白 ...
- nodeJS 中关于 promise 的使用
var readInfo = function (fileName) { var defer = q.defer(); fs.readFile(fileName, {encoding:'utf-8'} ...
- jquery简洁遮罩插件
/************************** *Desc:提交操作时遮罩 *Argument:type=0 全屏遮 1局部遮 *Author:Zery-Zhang *Date:2014-09 ...
- android 布局特点
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_cont ...
- GIS开发站点收藏
Arcgis API for Silverlight ArcGIS API for Silverlight开发入门 C#开发ArcGIS
- Nginx (简体中文)
博文地址:https://wiki.archlinux.org/index.php/Nginx_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87)#.E5.AE.89.E8. ...
- window下php5安装redis扩展 设置自启动服务
最近想在5.6版本的开发环境装一下redis的扩展,结果找了半天都是失效链接,特此做下备份 5.3-5.6 https://pecl.php.net/package/redis/2.2.7/windo ...
- Mark指针的指针(**)和链表使用(*&)
利用二级指针删除单向链表 彻底理解链表中为何使用指针的指针或者指针的引用 详解C++指针的指针和指针的引用