当前位置:网站首页>Lesson 3: SPFA seeking the shortest path
Lesson 3: SPFA seeking the shortest path
2022-07-27 14:46:00 【Fight! Sao Nian!】
subject :AcWing 851. spfa Find the shortest path
Given a n A little bit m Directed graph of strip edge , There may be double edges and self rings in the graph , The edge weight may be negative .
Please find out 1 It's on the n The shortest distance of point No , If you can't get from 1 Go to No n Number point , The output impossible.
The data guarantees that there is no negative weight loop .
Input format
The first line contains integers n and m.
Next m Each row contains three integers x,y,z, Indicates that there is a from point x point-to-point y The directed side of , Side length is z.
Output format
Output an integer , Express 1 It's on the n The shortest distance of point No .
If the path doesn't exist , The output impossible.
Data range
1≤n,m≤105,
The absolute value of side length involved in the figure shall not exceed 10000.
sample input :
3 3
1 2 5
2 3 -3
1 3 4
sample output :
2
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
map<int,vector<PII>> list;
const int N = 100010;
int n,m;
int dist[N],st[N];
void spfa()
{
memset(dist,0x3f,sizeof(dist));
dist[1]=0;
queue<int> q;
q.push(1);
st[1]=true;
while(q.size())
{
int t=q.front();
q.pop();
st[t]=false;
for(auto it:list[t])
{
if(dist[it.first]>dist[t]+it.second)
{
dist[it.first]=dist[t]+it.second;
if(!st[it.first])
{
q.push(it.first);
st[it.first]=true;
}
}
}
}
}
int main()
{
cin>>n>>m;
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
list[a].push_back({
b,c});
}
spfa();
if(dist[n]==0x3f3f3f3f)cout<<"impossible"<<endl;
else cout<<dist[n]<<endl;
return 0;
}
边栏推荐
- Research on Chinese idiom metaphorical knowledge recognition and relevance based on transfer learning and text enhancement
- 【医疗行业】DICOM converter Tools
- @What happens when bean and @component are used on the same class?
- Differences among CPU, GPU and NPU
- Simple encapsulation steps of request data request of uniapp
- C language layered understanding (C language array)
- np. Usage and difference of range() and range()
- Annual comprehensive analysis of China's online video market in 2022
- aac 和 h264等的时间戳
- Why does script file 'd:\anaconda3\envs\pad appear_ env\Scripts\pip-script. py‘ is not present.
猜你喜欢

Navicate reports an error access violation at address 00000000

Simple encapsulation steps of request data request of uniapp

Understand JS execution context in an article

Chinese character style transfer --- antagonistic discriminative domain adaptation (L1)

2022年中国网络视频市场年度综合分析

FPGA timing constraint sharing 04_ Output delay constraint

JS什么是声明提前?函数与变量声明提前的先后顺序(执行上下文铺垫篇)

在Oracle VirtualBox中导入Kali Linux官方制作的虚拟机

Thread knowledge summary

PROFINET 模拟器使用教程
随机推荐
万字详解 Google Play 上架应用标准包格式 AAB
Airport cloud business sign analysis
【STM32】EXTI
Toward fast, flexible, and robust low light image enhancement cvpr2022
这年头谁还不会抓包,WireShark 抓包及常用协议分析送给你!
poj3461 Oulipo【KMP】
Win11壁纸变黑怎么办?Win11壁纸变黑了的解决方法
PROFINET 模拟器使用教程
Thread knowledge summary
UnityUI方面处理(归纳与积累)
Is there a regular and safe account opening platform for gold speculation
Navicate报错access violation at address 00000000
STM32 - capacitive touch button experiment
Annual comprehensive analysis of China's online video market in 2022
2022年中国网络视频市场年度综合分析
一文搞懂 Redis 架构演化之路
va_list 使用总结
Rtl8762dk environment construction (I)
log4j2 jdbc appender
Understand JS execution context in an article