首页 > 代码库 > UVA 11478 - Halum(差分约束+最短路)

UVA 11478 - Halum(差分约束+最短路)

UVA 11478 - Halum

题目链接

题意:给定一个有向图,每次操作可以选择一个结点,把以这个点为起点的边权值+d,以这个边为终点的-d,问经过操作后,能得到的边权最小的最大值是多少,并且要判但是否无穷大或无解

思路:转化为差分约束,设一条边,他增加的权值为sum(u)减少了sum(v),那么二分答案x,得到一个不等式sum(u) - sum(v) + w(u, v) >= x,变形后得到sum(v) - sum(u) <= w(u, v) - x,这样就转化为了差分约束,直接bellmenford判负环即可

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;

typedef int Type;

const int MAXNODE = 505;
const int MAXEDGE = 2777;

struct Edge {
	int u, v;
	Type dist;
	Edge() {}
	Edge(int u, int v, Type dist) {
		this->u = u;
		this->v = v;
		this->dist = dist;
	}
};

struct BellmanFrod {
	int n, m;
	Edge edges[MAXEDGE];
	int first[MAXNODE];
	int next[MAXEDGE];
	bool inq[MAXNODE];
	Type d[MAXNODE];
	int p[MAXNODE];
	int cnt[MAXNODE];

	void init(int n) {
		this->n = n;
		memset(first, -1, sizeof(first));
		m = 0;
	}

	void add_Edge(int u, int v, Type dist) {
		edges[m] = Edge(u, v, dist);
		next[m] = first[u];
		first[u] = m++;
	}

	bool negativeCycle() {
		queue<int> Q;
		memset(inq, 0, sizeof(inq));
		memset(cnt, 0, sizeof(cnt));
		for (int i = 0; i < n; i++) {
			d[i] = 0; inq[i] = true; Q.push(i);
		}

		while (!Q.empty()) {
			int u = Q.front();
			Q.pop();
			inq[u] = false;
			for (int i = first[u]; i != -1; i = next[i]) {
				Edge& e = edges[i];
				if (d[e.v] > d[u] + e.dist) {
					d[e.v] = d[u] + e.dist;
					p[e.v] = i;
					if (!inq[e.v]) {
						Q.push(e.v);
						inq[e.v] = true;
						if (++cnt[e.v] > n) return true;
					}
				}
			}
		}
		return false;
	}
} gao;

int n, m;

bool judge(int x) {
	for (int i = 0; i < gao.m; i++)
		gao.edges[i].dist -= x;
	bool tmp = gao.negativeCycle();
	for (int i = 0; i < gao.m; i++)
		gao.edges[i].dist += x;
	return tmp;
}

int main() {
	while (~scanf("%d%d", &n, &m)) {
		gao.init(n);
		int u, v, dist;
		int l = 1, r = 0;
		while (m--) {
			scanf("%d%d%d", &u, &v, &dist);
			u--, v--; r = max(r, dist);
			gao.add_Edge(u, v, dist);
		}
		r++;
		if (judge(l)) printf("No Solution\n");
		else if (!judge(r)) printf("Infinite\n");
		else {
			while (l < r) {
				int mid = (l + r) / 2;
				if (judge(mid)) r = mid;
				else l = mid + 1;
			}
			printf("%d\n", l - 1);
		}
	}
	return 0;
}


UVA 11478 - Halum(差分约束+最短路)