优化bellman_ford的方法
bellman_ford(== 边数限制时用 ==):
每次更新每条边,存在负权边的时候dist[n] !=0x3f3f3f3f, 要用 dist[n] > 0x3f3f3f3f / 2来判断
psfa:
优点:万金油,正权边,负权边都可以用,速度快–O mn
缺点:正权边被出题人卡的话,只能用堆优化的dijksktra搞–O mlogn
每次只更新dist[j] > dist[t] + w[i](距离变短)点所指向的点,若最后dist[n]未被更新则一定为0x3f3f3f3f, 故可以:if(dist[n] == 0x3f3f3f3f) return -1;
C++ 代码
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 1e5 + 10;
int h[N], ne[N], e[N], w[N], idx;
int dist[N];
bool st[N];
int m, n, a, b, c;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
int spfa()
{
queue<int> q;
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
q.push(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[j])
{
q.push(j);
st[j] = true;
}
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
else return dist[n];
}
int main()
{
memset(h, -1, sizeof(h));
cin.tie(0);
cin >> n >> m;
while(m--)
{
cin >> a >> b >> c;
add(a, b, c);
}
int t = spfa();
if(t == -1) puts("impossible");
else cout<< t;
}
飞哥66