SGU 298 King Berl VI 差分约束 并使得min(dis[n]-dis[1])

题目链接:点击打开链接

题意:

给定n个点m条约束。

下面输出 u v x 表示:

dis[u] - dis[v] >= x

然后建图就是 u->v 边权为-x

输出一个解满足 -10000<= dis[i] <= 10000。

若有多解输出一个 dis[n] - dis[1] 最小的解。

思路:

正图求个最大解,反图求个最小解。

对于一个点的 最大解<最小解,则差分约束无解。

题目要求dis[n]-dis[1]最小,那就令dis[n]为其最小解,dis[1]为其最大解,再spfa一遍就好。

注意:这里所说的最大解最小解是需要确定一个变量的。则这个变量就是(解集中max(dis[i]) = inf)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <math.h>
#include <map>
#include <queue>
using namespace std;
#define N 10010
#define M 100010
#define inf 10000
struct Edge{
	int to, dis, nex;
};
int inq[N], Tim[N];
int n, m;
bool spfa(int head[], Edge edge[], int dis[]){
	queue<int> q;
	for(int i = 1; i <= n; i++) Tim[i] = 0, inq[i] = 1, q.push(i);
	while(!q.empty()){
		int u = q.front(); q.pop(); inq[u] = 0;
		for(int i = head[u]; ~i; i = edge[i].nex){
			int v = edge[i].to;
			if(dis[v] > dis[u] + edge[i].dis) {
				dis[v] = dis[u]+edge[i].dis;
				if(!inq[v]) {
					Tim[v]++;
					if(Tim[v] > n) return false;
					inq[v] = 1;
					q.push(v);
				}
			}
		}
	}
	return true;
}

Edge edge[M], Fedge[M];
int head[N], edgenum, fan[N];
void init(){memset(head, -1, sizeof head); memset(fan, -1, sizeof fan); edgenum = 0; }
void add(int u, int v, int d){
	Edge E = {v, d, head[u]};	edge[edgenum] = E;	head[u] = edgenum;
	Edge E2 = {u, d, fan[v]};	Fedge[edgenum] = E2; fan[v] = edgenum++;
}
int disg[N], disr[N];

int solve(){
	init();
	int u, v, d;
	while(m--){
		scanf("%d %d %d", &u, &v, &d);
		add(u, v, -d);
	}
	for(int i = 1; i <= n; i++) disg[i] = inf;
	if(spfa(head, edge, disg)==false) return -1;

	for(int i = 1; i <= n; i++) disr[i] = inf;
	if(spfa(fan, Fedge, disr)==false) return -1;
	for(int i = 1; i <= n; i++)
		if(disg[i] < -disr[i])
			return -1;
	disg[n] = -disr[n];
	spfa(head, edge, disg);
	for(int i = 1; i <= n; i++)printf("%d%c", disg[i], i==n?'\n':' ');
	return 0;
}
int main(){
	while(cin>>n>>m)
		if(solve()==-1)puts("-1");
	return 0;
}
/*
3 3
1 2 1
2 1 1
3 1 2
3 3
1 2 1
2 1 -1
3 1 2
3 3
1 2 1
2 1 -1
1 3 2

*/
时间: 2024-12-28 20:21:12

SGU 298 King Berl VI 差分约束 并使得min(dis[n]-dis[1])的相关文章

codevs 1183 泥泞的道路 (二分+SPFA+差分约束)

/* 二分答案(注意精度) 对于每一个答案 有(s1+s2+s3...)/(t1+t2+t3...)>=ans 时符合条件 这时ans有变大的空间 对于上述不等式如果枚举每一条路显得太暴力 化简一下变成 :s1-t1*ans+s2-t2*ans+s3-t3*ans...>=0 差分约束跑最长路 如果dis[n]>0 或者有正环 (开始这个忘掉了)ans就合法 */ #include<iostream> #include<cstdio> #include<cs

POJ 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

poj 1364 King(差分约束)(中等)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11028   Accepted: 4040 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin

POJ 1364 King(非连通图的差分约束,经典好题)

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11103   Accepted: 4068 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound kin

POJ1364 King 【差分约束】

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9977   Accepted: 3711 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound king

poj1364 King --- 差分约束

这是我见过最扯淡的题面之一. 题读了差不多一半我都觉得我这题肯定读不懂了,到最后终于看到重点了靠! 就是个差分约束大水题!毫无新意! 扯些什么皇后想生孩子!生了男孩是个弱智!父王很担心!这些有的没的有意思吗!! 题目就是给一个序列,告诉你 a b gt/lt c 表示从a起的b+1个数之和大于/小于c 就根据这个列不等式,要把> 或 <关系换成>= <= 就减一就可以了 列出不等式: S[a-1]-S[a+b]<=-c-1 S[a+b]-S[a-1]<=c-1 需要注意

POJ 1364 King 差分约束 找负环

嘛,虽然是一道水题+模板题,不过还是学到了很多东西的,记录一下. 首先题目给出的不等式是小于,但是差分约束系统只能处理小于等于的情况,所以要转化成小于等于的进行处理.对于整数处理方法非常简单= = 然后是找负环的情况,其实不需要考虑图连不连通,只要一开始就把所有的点的d置成0,然后都push进队列里面就好了. PS:这种方法同样可以用在处理多源点最短路问题上. #include <cstdio> #include <cstring> #include <cmath> #

【差分约束】POJ1364/LG UVA515 king

这道题是个差分约束 King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14901 Accepted: 5248 Description Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sou

【转载】夜深人静写算法(四)——差分约束

[转载]夜深人静写算法(四) - 差分约束  目录     一.引例       1.一类不等式组的解   二.最短路       1.Dijkstra       2.图的存储       3.链式前向星       4.Dijkstra + 优先队列       5.Bellman-Ford       6.SPFA       7.Floyd-Warshall   三.差分约束        1.数形结合        2.三角不等式        3.解的存在性        4.最大值