给定一个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. Django学习---多人博客项目(1)

    一.创建项目和应用 ​ 在Pycharm中用Django模板创建一个工程文件 创建项目 python manage.py startproject 项目名 . 创建应用 python manage.p ...

  2. 九、Shell脚本高级编程实战第九部

    一.监控mysql主从同步是否异常,如果异常,发送短信给管理员 1)开发一个守护进程脚本每30秒实现检测一次. 2)如果错误号是:1158.1159.1008.1007.1062,请跳过 3)请使用数 ...

  3. 论文翻译——Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection

    Dynamic Pooling and Unfolding Recursive Autoencoders for Paraphrase Detection 动态池和展开递归自动编码器的意译检测 论文地 ...

  4. C/C++中开平方函数sqrt()的用法

    开平方使用sqrt()函数 使用方法: 包含于math.h头文件 sqrt(float * number),返回number的开平方数,返回值为浮点型 sqrt使用时大多需要要强制类型转化,因为sqr ...

  5. PAT甲级——1035 Password (20分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  6. TPO6-1Powering the Industrial Revolution

    The source had long been known but not exploited. Early in the eighteenth century, a pump had come i ...

  7. 爬虫笔记(十一)——认识cookie

    什么是cookie? 在爬虫的使用中,如果涉及登录等操作时,经常会使用到cookie.简单的来说,我们访问每一个互联网页面,都是通过HTTP协议进行的,而HTTP协议是一个无状态协议,所谓的无状态协议 ...

  8. http跳转http

    server {listen 80;server_name 123.com;root /var/www/web/123;index index.html index.htm index.php;rew ...

  9. tomcat更新class不生效

    替换线上lib里的class不生效,需要想想是不是前人为了图方便在classes里面扔了一份老版本class

  10. Form表单中method="post/get'的区别

    Form提供了两种数据传输的方式--get和post.虽然它们都是数据的提交方式,但是在实际传输时确有很大的不同,并且可能会对数据产生严重的影响.虽然为了方便的得到变量值,Web容器已经屏蔽了二者的一 ...