最短路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:有轨电车 这题就是构造一个有向无权图,然后每一个点都会有一个开关,这个开关指向他的其中一个出度.当途经这个点的时候,如 ...
随机推荐
- c# 获取毫秒值,时间戳
获取时间戳(秒) (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000 获取时间戳(毫秒) (DateTime. ...
- listView的异步加载数据
1 public class MainActivity extends Activity { 2 3 private ListView listView; 4 private ArrayList< ...
- AI关注的网址
中科院计算技术研究所博士招生:http://admission.ucas.ac.cn/info/ZhaoshengDanweiDetail/9adf9e50-424b-44c8-b2dc-900ef9 ...
- Robot Motion - poj 1573
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11269 Accepted: 5486 Description A ...
- jvm 调优参数
-server -Xms6000M -Xmx6000M -Xmn500M -XX:PermSize=500M -XX:MaxPermSize=500M -XX:SurvivorRatio=65536 ...
- phalcon builder 用法
$rawSql = $builder->columns(["aa","bb"]) ->from("TableName") -&g ...
- Django安装和启动
1.django安装 在http://www.djangoproject.com/download/这个网站上可以下载django的最新版本.在下载时,要注意django版本和本机安装的Python版 ...
- 【BZOJ3784】树上的路径 点分治序+ST表
[BZOJ3784]树上的路径 Description 给定一个N个结点的树,结点用正整数1..N编号.每条边有一个正整数权值.用d(a,b)表示从结点a到结点b路边上经过边的权值.其中要求a< ...
- output value . Sigmoid neurons are similar to perceptrons, but modified so that small changes in their weights and bias cause only a small change in their output.
http://neuralnetworksanddeeplearning.com/chap1.html . Sigmoid neurons are similar to perceptrons, bu ...
- 【python】-- 类的反射
反射 反射我们以后会经常用到,这个东西实现了动态的装配,通过字符串来反射类中的属性和方法 一.反射函数 1.hasarttr(obj,name_str) 作用:判断一个对象obj中是否有对应的name ...