给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数。

请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出impossible。

数据保证不存在负权回路。

输入格式

第一行包含整数n和m。

接下来m行每行包含三个整数x,y,z,表示存在一条从点x到点y的有向边,边长为z。

输出格式

输出一个整数,表示1号点到n号点的最短距离。

如果路径不存在,则输出”impossible”。

数据范围

1≤n,m≤1051≤n,m≤105,
图中涉及边长绝对值均不超过10000。

输入样例:

3 3
1 2 5
2 3 -3
1 3 4

输出样例:

2

对bellman_ford优化
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; typedef pair<int, int> PII; const int N = 1e5 + 10; int n, m;
int h[N], w[N], e[N], ne[N], idx;
int dist[N];
bool st[N]; void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
} int spfa()
{
memset(dist,0x3f,sizeof dist);
dist[1] = 0; //定义队列存储所有待更新的点
queue<int> q;
q.push(1);//1号点放入队列
st[1] = true;//表示当前这个点是不是在队列当中,防止队列当中存储重复的点 while(q.size()){//队列不空
int t = q.front();
q.pop(); st[t] = false; //更新t的所有的邻边
for(int i = h[t];i != -1;i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if(!st[t])
{
q.push(j);
st[j] = true;
}
}
} } if(dist[n] == 0x3f3f3f3f) return -1;
else return dist[n]; } int main()
{
scanf("%d%d", &n, &m); memset(h, -1, sizeof h);
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
} int t = spfa();
if(t == -1) cout << "impossible";
else
cout << spfa() << endl; return 0;
}

  

851. spfa求最短路的更多相关文章

  1. ACM - 最短路 - AcWing 851 spfa求最短路

    AcWing 851 spfa求最短路 题解 以此题为例介绍一下图论中的最短路算法 \(Bellman\)-\(Ford\) 算法.算法的步骤和正确性证明参考文章最短路径(Bellman-Ford算法 ...

  2. acwing 851. spfa求最短路 模板

    地址 https://www.acwing.com/problem/content/description/853/ 给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你求出 ...

  3. 851. spfa求最短路(spfa算法模板)

    给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出impossible. 数据保证不存在负权回路. 输入格式 ...

  4. AcWing 851. spfa求最短路 边权可能为负数。 链表 队列

    #include <cstring> #include <iostream> #include <algorithm> #include <queue> ...

  5. 基于bellman-ford算法使用队列优化的spfa求最短路O(m),最坏O(n*m)

    acwing851-spfa求最短路 #include<iostream> #include<cstring> #include<algorithm> #inclu ...

  6. spfa求次短路

    思路:先算出每个点到1的最短路d1[i],记录下路径,然后枚举最短路上的边 删掉之后再求一遍最短路,那么这时的最短路就可能是答案. 但是这个做法是错误的,可以被卡掉. 比如根据下面的例题生成的一个数据 ...

  7. SPFA求最短路——Bellman-Ford算法的优化

    SPFA 算法是 Bellman-Ford算法 的队列优化算法的别称,通常用于求含负权边的单源最短路径,以及判负权环.SPFA 最坏情况下复杂度和朴素 Bellman-Ford 相同,为 O(VE), ...

  8. Holy Grail【spfa求最短路】

    题目链接:https://www.jisuanke.com/contest/3004?view=challenges 题目大意: 1.一个无向图,给出六个顶点,添六条边,但是添边是有限制的.每次添边的 ...

  9. poj2387 spfa求最短路

    //Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...

随机推荐

  1. 利用FastJson,拼接复杂嵌套json数据&&直接从json字符串中(不依赖实体类)解析出键值对

    1.拼接复杂嵌套json FastJson工具包中有两主要的类: JSONObject和JSONArray ,前者表示json对象,后者表示json数组.他们两者都能添加Object类型的对象,但是J ...

  2. Python笔记_第二篇_面向过程_第二部分_3.模块的概述

    这部分内容是非常重要的,分模块的基本概念和一些常用模块的使用,其实常用模块使用这部分也不是太全面,后续或者有机会再通过其他材料进行讲解. 1. 模块的概述: 目前代码比较少,写在一个文件中还体现不出什 ...

  3. Microsoft .NET Framework 4.5.2 (Offline Installer)

    Microsoft .NET Framework 4.5.2 (Offline Installer) for Windows Vista SP2, Windows 7 SP1, Windows 8, ...

  4. python画图例子代码

    matplotlib包,使得python可以使用类似matlab的命令 双坐标,子图例子 fig, axes = plt.subplots( 2,1, figsize=(14, 14) ) ax = ...

  5. mac 编程环境

    新mac (EI Capitan),需要在python中使用xgboost,通过pip安装未成功. 配置pip cat $HOME/Library/Application\ Support/pip/p ...

  6. windows系统下的渗透测试神器 -pentestbox

    Pentestbox介绍 PentestBox官网:https://pentestbox.org/zh/ 这是一个运行在windows环境下的终端,集成了绝大部分渗透测试所需要的环境 如python2 ...

  7. 正则表达式awk学习(三)

    awk:格式化文本输出 gawk - pattern scanning and processing language awk:gawk的符号链接 基本用法:gawk [options] 'progr ...

  8. K-th K

    题目描述 You are given an integer sequence x of length N. Determine if there exists an integer sequence ...

  9. MyEclipse10安装properties文件插件

    安装步骤 1.下载PropertiesEditor插件 http://propedit.sourceforge.jp/index_en.html 2.解压出features.plugins文件 3.在 ...

  10. hibernate中lazy的使用

    lazy,延迟加载 Lazy的有效期:只有在session打开的时候才有效:session关闭后lazy就没效了. lazy策略可以用在: * <class>标签上:可以取值true/fa ...