为什么这题我SPFA加了容错SLF和MCFX反而第六个点错了(看了下大概是误判负环了) 感谢大佬解答
代码:
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxN = 1011,maxM = 30011,inf = 0x3f3f3f3f;
struct conP{
int p,next,v;
};
conP cfs[maxM];
int N,last[maxN],dis[maxN],tot,Ml,Md,R = 200,val = 15;
int A,B,D;
bool SPFA(int );
void add_edge(int ,int ,int );
int read();
int main() {
N = read();
Ml = read();
Md = read();
for(register int i(1);i <= Ml;++i) {
A = read();
B = read();
D = read();
if(A > B) swap(A,B);
add_edge(A,B,D);
}
for(register int i(1);i <= Md;++i) {
A = read();
B = read();
D = read();
if(A > B) swap(A,B);
add_edge(B,A,-D);
}
for(register int i = 2;i <= N;++i) add_edge(i,i - 1,0);
if(SPFA(1)) {
if(dis[N] != 0x3f3f3f3f) printf("%d",dis[N]);
else {
for(register int i = 1;i <= N;++i) add_edge(0,i,0);
if(!SPFA(0)) {
printf("-1");
}
else printf("-2");
}
}
else {
printf("-1");
}
return 0;
}
bool SPFA(int s) {
int p,in_num[maxN] = {};
bool b[maxN] = {};
deque<int > queue_;
memset(dis,0x3f3f3f3f,sizeof(dis));
queue_.push_back(s);
dis[s] = 0;
in_num[s] = 1;
while(!queue_.empty()) {
p = queue_.front();
queue_.pop_front();
b[p] = false;
for(register int i = last[p];i;i = cfs[i].next) {
int to = cfs[i].p,v = cfs[i].v;
if(dis[to] > dis[p] + v) {
dis[to] = dis[p] + v;
if(!b[to]) {
b[to] = true;
if(in_num[to] < R and in_num[to]) {
queue_.push_front(to);
}
else {
if(!queue_.size()) queue_.push_back(to);
else if(dis[queue_.front()] + val >= dis[to]) queue_.push_front(to);
else queue_.push_back(to);
}
in_num[to] += 1;
if(in_num[to] == N + 1) return false;
}
}
}
}
return true;
}
void add_edge(int x,int y,int v) {
cfs[++tot].p = y;
cfs[tot].v = v;
cfs[tot].next = last[x];
last[x] = tot;
}
int read() {
int x = 0;
char ch = getchar();
while(ch > '9' or ch < '0') ch = getchar();
while(ch >= '0' and ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x;
}