我的spfa为啥过不了dijkstra.........ctmd
作者:
VALUE_5
,
2021-11-15 12:44:44
,
所有人可见
,
阅读 255
#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
int h[N], e[N],ne[N],w[N],idx;
int dist[N];
int q[N];
bool st[N];
int n,m;
void add(int a, int b, int c){
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx++;
}
int spfa(){
memset(dist, 0x3f3f3f3f, sizeof dist);
dist[1] = 0;
int hh = 0, tt = -1;
q[++tt] = 1;
st[1] = true;
while(hh <= tt){
int t = q[hh++];
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[++tt] = j;
st[j] = true;
}
}
}
}
if(dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}
int main(){
cin >> n >> m;
memset(h, -1, sizeof h);
for(int i = 0; i < m; i++){
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
int t = spfa();
if(t == -1){
cout << "impossible" << endl;
}else{
cout << t << endl;
}
return 0;
}
spfa不是已经死了么
但是y总的视频里面可以ac啊,这是不常用了吗
哪一题啊, 也可能是数据加强了。
dijkstra堆优化那个题,好像是我的队列开的小了,因为我是数组 模拟的
因该是数据加强了,我改成stl的队列也不行,大佬方便的话跑一下我的代码,看看是不是我那写错了