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 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

题意:

n个人站成一行,有两个人之间的距离最少是多少,有两个人之间的距离最大是多少,问第一个人到第 n个人之间的距离最大是多少;

思路:

由题意可以得到这样的不等式:

X行:

dis[B]-dis[A]<=C;

dis[A]-dis[B]<=0;

Y行:

dis[A]-dis[B]<=-C;

这是转化成最短路径的写法,当然也可以转化成求最长路径;

当无法满足的时候就是-1,当1和n不连通的时候就是-2;否则就是dis[n]了;

用spfa算法好快;

AC代码:

//#include <bits/stdc++.h>
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e4+5;
int cnt,n,x,y,vis[N],head[N],num[N];
int dis[N];
struct Edge
{
    int to,next,val;
}edge[4*N];
void add_edge(int s,int e,int val)
{
    edge[cnt].to=e;
    edge[cnt].next=head[s];
    edge[cnt].val=val;
    head[s]=cnt++;
}
queue<int>qu;
void spfa()
{
    while(!qu.empty())qu.pop();
    mst(vis,0);
    mst(dis,inf);
    mst(num,0);
    qu.push(1);
    dis[1]=0;
    vis[1]=1;
    while(!qu.empty())
    {
        int fr=qu.front();
        qu.pop();

        num[fr]++;
        if(num[fr]>n)
        {
            printf("-1\n");
            return ;
        }
        for(int i=head[fr];i!=-1;i=edge[i].next)
        {
            int x=edge[i].to;
            if(dis[x]>dis[fr]+edge[i].val)
            {
                dis[x]=dis[fr]+edge[i].val;
                if(!vis[x])
                {
                    qu.push(x);
                    vis[x]=1;
                }
            }
        }
        vis[fr]=0;
    }
    if(dis[n]==inf)printf("-2\n");
    else printf("%d\n",dis[n]);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cnt=0;
        mst(head,-1);
        int u,v,w;
        scanf("%d%d%d",&n,&x,&y);
        Riep(x)
        {
            scanf("%d%d%d",&u,&v,&w);
            add_edge(u,v,w);
            add_edge(v,u,0);
        }
        Riep(y)
        {
            scanf("%d%d%d",&u,&v,&w);
            add_edge(v,u,-w);
        }
        spfa();
    }

    return 0;
}
时间: 2024-07-31 03:16:45

hdu-3592 World Exhibition(差分约束)的相关文章

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 Exhibiti

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

HDU 1384 Intervals【差分约束-SPFA】

类型:给出一些形如a−b<=k的不等式(或a−b>=k或a−b<k或a−b>k等),问是否有解[是否有负环]或求差的极值[最短/长路径].例子:b−a<=k1,c−b<=k2,c−a<=k3.将a,b,c转换为节点:k1,k2,k3转换为边权:减数指向被减数,形成一个有向图: 由题可得(b−a) + (c−b) <= k1+k2,c−a<=k1+k2.比较k1+k2与k3,其中较小者就是c−a的最大值.由此我们可以得知求差的最大值,即上限被约束,此时我

HDU 3592 World Exhibition (差分约束,spfa,水)

题意: 有n个人在排队,按照前后顺序编号为1~n,现在对其中某两人的距离进行约束,有上限和下限,表示dis[a,b]<=c或者dis[a,b]>=c,问第1个人与第n个人的距离最多可能为多少?(若INF则输出-2,若冲突则输出-1,否则输出距离) 思路: 建图时都将约束转成a-b<=c的标准形式,然后建一条b->a的边,权为c.然后求最短路,注意最短路跑出来的结果却是最远的合法距离,而不是最短距离.本题无需添加辅助边,只要到达不了n,则距离为INF,输出-2,若有负环,那肯定是冲突

HDU.1529.Cashier Employment(差分约束 最长路SPFA)

题目链接 \(Description\) 给定一天24h 每小时需要的员工数量Ri,有n个员工,已知每个员工开始工作的时间ti(ti∈[0,23]),每个员工会连续工作8h. 问能否满足一天的需求.若能,输出最少需要多少员工. \(Solution\) 参考. 既然给的是开始工作时间,那么就先根据开始时间做 设Ai表示在i时开始工作的人数(未知),Bi表示i时可工作人数的上限(已知) 那么有:(注意可以跨天) A[i-7]+A[i-6]+...+A[i-1]+A[i] >= R[i] (7 <

HDU 1384 Intervals (差分约束)

Sample Input 5 3 7 3 8 10 3 6 8 1 1 3 1 10 11 1 Sample Output 6 题意:给你n个数u,v,w:要求在[u,v]区间至少取w个数(整数),求最少要取多少个数. S[v+1] - S[u] >= w, S[i+1] - S[i] >=0&&<=1,S[i] - S[i+1] <=-1. 在u,v+1之间建一条边,跑一遍SPFA即可. #include <iostream> #include <

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

Hdu 3666 THE MATRIX PROBLEM(差分约束)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3666 思路:差分约束. 取对数将乘除转化为加减. L<=m[i][j]*a[i]/b[j]<=U log(L/m[i][j])<=log(a[i])-log(b[j])<=log(U/m[i][j]) 则 : log(a[i])<=log(b[j])+log(U/m[i][j]) log(b[j])<=log(a[i])+log(m[i][j]/L) SPFA判

Hdu 4594 Difference(奇圈判断+差分约束)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4598 思路:由题意可知两相连点ai符号一定相反,所以若存在奇圈则一定无解.染色,colour[i]==1表示为正,colour[i]==2表示为负.由于(b)条件为充要条件,所以对于图中的点| a[i]-a[j] | >= T,对于非图中点| a[i]-a[j] | < T,即| a[i]-a[j] | <= T-1 .所以图中点,若colour[i]==1,a[i]-a[j] >=