为什么用了优化暴毙了
  • 板块学术版
  • 楼主whale142857
  • 当前回复15
  • 已保存回复15
  • 发布时间2020/7/30 19:32
  • 上次更新2023/11/6 21:44:37
查看原帖
为什么用了优化暴毙了
245821
whale142857楼主2020/7/30 19:32

这是题目

这道题我白板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 += 1) {
		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 += 1) {
		A = read();
		B = read();
		D = read();
		if(A > B) swap(A,B);
		add_edge(B,A,-D);
	}
	for(int i = 2;i <= N;i += 1) 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(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;
}

代码里的zz处理不要管

舅舅蒟蒻

2020/7/30 19:32
加载中...