spfa 复杂度最好的情况是o(m),最差的时候是o(nm).
而且spfa对于图论问题无论正权还是负权都可以用,很全能(大多数情况,一般不会超复杂度的),形似bfs。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int n, m;
int h[N], w[N], ne[N], idx, e[N];
int dist[N];
int 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);
//自己距离自己是1
dist[1] = 0;
//定义
queue<int> q;
//将序号1放入
q.push(1);
//标记序号1已经入队
st[1] = true;
//循环队列
while(q.size())
{
//取出队头
auto t = q.front();
//弹出对头
q.pop();
st[t] = false;
//更新出边
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[j])
{
q.push(j);
st[j] = true;
}
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}
int main()
{
cin >> n >> m;
//构建邻接表
memset(h, -1, sizeof h);
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
int t = spfa();
if(t == -1) puts("impossible");
else
cout << t << endl;
return 0;
}