算法提高差分约束和负环(spfa)

负环:

在这里插入图片描述

思路:图论最重要以及最难的其实就是建图,需要耐心读题

AC代码:

#include<bits/stdc++.h>

using namespace std;

const int N = 3e5 + 10;
typedef long long ll;
typedef pair<int, int>PII;
//spfa判断负环
vector<PII>ed[N];
int dis[N];
int st[N];
int n, m, w;
int cnt[N];
bool spfa()
{
	memset(dis, 0x3f, sizeof dis);
	memset(cnt, 0, sizeof cnt);
	memset(st, 0, sizeof st);
	dis[1] = 0;
	queue<int>q;

	for (int i = 1; i <= n; i ++ )
	{
		q.push(i);//因为起点不确定,因此需要都压入
	}

	cnt[1] ++;
	while(q.size()){
		int x = q.front();
		q.pop();
		st[x] = 0;
		for(auto it : ed[x])
		{
			if(dis[it.first] <= dis[x] + it.second) continue;
			dis[it.first] = dis[x] + it.second;
			if(!st[it.first])
			{
				cnt[it.first] ++;
				st[it.first] = 1;
				q.push(it.first);
				if(cnt[it.first] >= n) return true;
			}
		}
	}
	return false;
}
int main()
{
    int t;
	cin >> t;
	while(t --){
		cin >> n >> m >> w;
	
	    for(int i = 0; i < m; i ++)
		{
			int a, b, c;
			cin >> a >> b >> c;
			ed[a].push_back({b, c});
			ed[b].push_back({a, c});
		}
		for(int i = 0; i < w; i ++)
		{
			int a, b, c;
			cin >> a >> b >> c;
			ed[a].push_back({b, -c});
		}//建图
		if(spfa()) puts("YES");
		else puts("NO");
		for(int i = 0; i <= n * 2; i ++) ed[i].clear();
	}
	return 0;
}

差分约束:

在这里插入图片描述

思路:

在这里插入图片描述

AC代码:

#include <iostream>
#include <cstring>

typedef long long LL;
const int N = 1e5 + 10, M = 3 * N;
int n, m;
int h[N], e[M], w[M], ne[M], idx;
int dist[N], cnt[N], q[N];
bool st[N];

void add(int a, int b, int c) {
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa() {
    //因为是求所有x_i的最小值,因此就是求不等式的下界的最大值
    //转而就是求图论的最长路
    int hh = 0, tt = 1;
    memset(dist, -0x3f, sizeof dist);
    dist[0] = 0;
    q[0] = 0;

    //建立一个能够到所有点的虚拟源点0
    for (int i = 1; i <= n; ++i) add(0, i, 1);  //x_0 <= x_i + 1

    //这题在判负环的时候会TLE
    //上一次的“负环”题目中的trick方法还是太玄学了
    //这里用一个不会TLE的判负环方法,那就是把SPFA算法中的循环队列改为栈
    //这样对于遇到的负环,就不会加入队尾,知道再次遍历完整个队列才去算他
    //遇到负环会直接在栈顶连续入栈出栈,直到判断他的cnt[i] >= n+1,即发现负环
    while (hh != tt) {
        int t = q[--tt];
        st[t] = false;

        for(int i = h[t]; ~i; i = ne[i]) {
            int j = e[i];
            if (dist[j] < dist[t] + w[i]) {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;

                if (cnt[j] >= n + 1) return true;

                if (!st[j]) {
                    st[j] = true;
                    q[tt++] = j;
                }
            }
        }
    }
    return false;
}
int main() {
    memset(h, -1, sizeof h);
    scanf("%d%d", &n, &m);
    while (m--) {
        int x, a, b;
        scanf("%d%d%d", &x, &a, &b);
        if (x == 1) add(a, b, 0), add(b, a, 0);
        else if (x == 2) add(a, b, 1);
        else if (x == 3) add(b, a, 0);
        else if (x == 4) add(b, a, 1);
        else if (x == 5) add(a, b, 0);
    }
    if (spfa()) puts("-1");
    else {
        LL res = 0;
        for (int i = 1; i <= n; ++i) res += dist[i];
        printf("%lld\n", res);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值