HDOJ 3592 World Exhibition 差分约束

World Exhibition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1373    Accepted Submission(s): 673

Problem Description

Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the
same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.

There is something interesting. Some like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of X (1 <= X <= 10,000) constraints describes
which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person dislike each other and the minimum distance by which they must be separated.

Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the distance constraints.

Input

First line: An integer T represents the case of test.

The next line: Three space-separated integers: N, X, and Y.

The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.

The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.

Output

For each line: A single integer. If no line-up is possible, output -1. If person 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between person 1 and N.

Sample Input

1
4 2 1
1 3 8
2 4 15
2 3 4

Sample Output

19

Author

alpc20

Source

2010 ACM-ICPC Multi-University
Training Contest(15)——Host by NUDT

/* ***********************************************
Author        :CKboss
Created Time  :2015年07月30日 星期四 08时16分29秒
File Name     :HDOJ3592.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=1111;
const int INF=0x3f3f3f3f;

struct Edge
{
	int to,next,cost;
}edge[30300];

int Adj[maxn],Size;

void init()
{
	memset(Adj,-1,sizeof(Adj)); Size=0;
}

void Add_Edge(int u,int v,int c)
{
	edge[Size].to=v;
	edge[Size].cost=c;
	edge[Size].next=Adj[u];
	Adj[u]=Size++;
}

int n,m1,m2;

int dist[maxn],cq[maxn];
bool inq[maxn];

bool spfa(int rt)
{
	memset(dist,63,sizeof(dist));
	memset(cq,0,sizeof(cq));
	memset(inq,false,sizeof(inq));

	dist[rt]=0;
	queue<int> q;
	inq[rt]=true; q.push(rt); cq[rt]=1;

	while(!q.empty())
	{
		int u=q.front(); q.pop();

		for(int i=Adj[u];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(dist[v]>dist[u]+edge[i].cost)
			{
				dist[v]=dist[u]+edge[i].cost;
				if(!inq[v])
				{
					inq[v]=true;
					cq[v]++;
					if(cq[v]>=n) return false;
					q.push(v);
				}
			}
		}
		inq[u]=false;
	}
	return true;
}

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int T_T;
	scanf("%d",&T_T);
	while(T_T--)
	{
		scanf("%d%d%d",&n,&m1,&m2);
		init();
		int u,v,c;
		for(int i=0;i<m1;i++)
		{
			scanf("%d%d%d",&u,&v,&c);
			Add_Edge(u,v,c);
		}
		for(int i=0;i<m2;i++)
		{
			scanf("%d%d%d",&u,&v,&c);
			Add_Edge(v,u,-c);
		}

		bool fg=spfa(1);
		if(fg==false) puts("-1");
		else if(dist[n]==INF) puts("-2");
		else printf("%d\n",dist[n]);
	}

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 07:20:46

HDOJ 3592 World Exhibition 差分约束的相关文章

hdu3592 World Exhibition --- 差分约束

这题建图没什么特别 x个条件:Sb-Sa<=c y个条件:Sa-Sb<=-c 题目问的是,1和n之间的关系. 有负环的话,整个就不可能成立,输出-1 如果图是连通的(1到n是连通的),就输出d[n] 不连通就是题目中说-2的情况. 原来我们建图一般添加一个附加结点,或者开始就把所有点入队,就是考虑到不连通的问题,所以添加一个没有意义的条件. #include <iostream> #include <cstring> #include <string> #i

HDOJ 1534 Schedule Problem 差分约束

差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1503    Accepted Submission(s): 647 Special Judge Problem Descr

hdu-3592 World Exhibition(差分约束)

题目链接: World Exhibition Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are st

hdu 差分约束题集

[HDU]1384 Intervals 基础差分约束★1529 Cashier Employment 神级差分约束★★★★ 1531 King 差分约束★1534 Schedule Problem 差分约束输出一组解★3440 House Man 比较好的差分约束★★3592 World Exhibition 简单★3666 THE MATRIX PROBLEM 中等★★4274 Spy's Work [先处理出欧拉序列,然后就是差分约束了...] [POJ]1201 Intervals1275

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

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

差分约束算法总结

来自 https://blog.csdn.net/my_sunshine26/article/details/72849441 差分约束系统 一.概念 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统. 二.引例 给定n个变量和m个不等式,每个不等式的形式为 x[i] - x[j] <= a[k] (0 <= i, j < n, 0 <= k < m, a[k]已知),求 x[i] - x[j]

差分约束

1.bzoj3436 思路: 差分约束根据限制条件建图,注意要有一个超级源点向所有点连一条边权为0的边建图看代码. 然后spfa判负环,写bfs会超时的......实测n遍. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define inf 0x7fffffff #define ll long long #define N 100007 using na

bzoj2788 festival 差分约束

填坑中--链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2788 题意: 有$n$个正整数$X1,X2,...,Xn$,再给出$m1+m2$个限制条件,限制分为两类:1. 给出$a,b(1<=a,b<=n)$,要求满足$Xa + 1 = Xb$2. 给出$c,d (1<=c,d<=n)$,要求满足$Xc <= Xd$在满足所有限制的条件下,求集合${Xi}$大小的最大值. 首先看情况我们也知道是差分约束-- 但是这个差分

POJ 1201 Intervals 差分约束

http://poj.org/problem?id=1201 TLE了很久,因为用了cin..... 思路和其他差分约束差不多,http://www.cppblog.com/menjitianya/archive/2015/11/19/212292.html 如果区间[a, b]中至少有c个元素,如果用上面的博客,那么说明xa - xb >= c,但是注意这里是闭区间,xa - xb是不包括b这个点的, 就比如用了[a, b]有c个元素,[b, d]有x个,那么ans = c + x - 1个,