#include <algorithm>
#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的更多相关文章

  1. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  2. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  3. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  4. hdu-3790最短路刷题

    title: hdu-3790最短路刷题 date: 2018-10-20 14:50:31 tags: acm 刷题 categories: ACM-最短路 概述 一道最短路的水题,,,尽量不看以前 ...

  5. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

  6. POJ 4046 Sightseeing 枚举+最短路 好题

    有n个节点的m条无向边的图,节点编号为1~n 然后有点权和边权,给出q个询问,每一个询问给出2点u,v 输出u,v的最短距离 这里的最短距离规定为: u到v的路径的所有边权+u到v路径上最大的一个点权 ...

  7. hdoj2544 最短路(Dijkstra || Floyd || SPFA)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路 最短路算法模板题,求解使用的Dijkstra算法.Floyd算法.SPFA算法可以当做求解 ...

  8. 【bzoj2118&洛谷P2371】墨墨的等式(最短路神仙题)

    题目传送门:bzoj2118 洛谷P2371 这道题看了题解后才会的..果然是国家集训队的神仙题,思维独特. 首先若方程$ \sum_{i=1}^{n}a_ix_i=k $有非负整数解,那么显然对于每 ...

  9. POJ1847 Tram SPFA算法变形

    原题地址:http://poj.org/problem?id=1847 Tram:有轨电车 这题就是构造一个有向无权图,然后每一个点都会有一个开关,这个开关指向他的其中一个出度.当途经这个点的时候,如 ...

随机推荐

  1. day14—JQuery编程基础

    JQuery 1.什么是jQuery jQuery是一个优秀的JavaScript框架.一个轻量级的JavaScript类库.jQuery的核心理念是Write less.Do more. 使用jQu ...

  2. JavaScript的toString()

    JavaScript toString() 方法 JavaScript Boolean 对象 定义和用法 toString() 方法可把一个逻辑值转换为字符串,并返回结果. 语法 booleanObj ...

  3. 跟我一起写 Makefile(二)[转]

    原文链接 http://bbs.chinaunix.net/thread-408225-1-1.html(出处: http://bbs.chinaunix.net/) 一.Makefile里有什么? ...

  4. 安卓手机的后门控制工具SPADE

    SPADE,一款安卓手机的后门控制工具,安全研究人员可以以此了解和研究安卓后门原理. 首先,我们从网站www.apk4fun.com下载apk文件,如ccleaner.然后,我们安装spade git ...

  5. Android Activity去除标题栏和状态栏

    一.在代码中设置public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //去除ti ...

  6. 机器学习10—K-均值聚类学习笔记

    机器学习实战之K-Means算法 test10.py #-*- coding:utf-8 import sys sys.path.append("kMeans.py") impor ...

  7. busybox下inittab中runlevel解析

    Order of scripts run in /etc/rc?.d ================================== 0. Overview. All scripts execu ...

  8. HTTP响应头缓存控制

    在一般Web开发中,有时需要设置特殊的HTTP响应头阻止客户端(一般是浏览器)缓存(使用)该次请求的响应. 这时候大部分开发人员都是百度或谷歌几段代码复制粘贴即了事. 以下简述一下关于缓存控制的几种H ...

  9. Spring读书笔记-----Spring核心机制:依赖注入

    spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.今天就和大家一起来学习一下 依赖注入的基本概念 依赖注入(Dependecy Injection),也称为IoC(I ...

  10. ImportError: No module named '_sqlite3'

    问题: Python 3.5.1 报错如下 Traceback (most recent call last): File "manage.py", line 16, in < ...