poj3169——Layout(差分约束+SPFA判断负环)

Description

Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate).

Some cows 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 ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows 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 cow 1 and cow N that satisfies the distance constraints.

Input

Line 1: Three space-separated integers: N, ML, and MD.

Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart.

Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.

Output

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

Sample Input

4 2 1

1 3 10

2 4 20

2 3 3

Sample Output

27

典型的差分约束,包含a-b<=d,那么b+d>=a,构造权值为d的b->a有向边。a-b>=d,那么b-a<=-d,a+(-d)>=b,构造权值为-d的a->b有向边。

输出-2的情形说明1和n之间没有不等式的约束,反映到图中就是两点之间没有·通路

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 200010
#define Mod 10001
using namespace std;
struct Edge
{
    int v,w,next;
};
Edge edge1[MAXN<<1];
int head1[MAXN],n,m,e,vis[MAXN],dis[MAXN];
int q[MAXN],p[MAXN];
void add(Edge *edge,int *head,int u,int v,int w)
{
    edge[e].v=v;
    edge[e].w=w;
    edge[e].next=head[u];
    head[u]=e;
    e++;
}
void spfa(Edge *edge,int *head,int u)
{
    memset(vis,0,sizeof(vis));
    memset(p,0,sizeof(p));
    for(int i=1; i<=n; ++i)
        dis[i]=INF;
    dis[u]=0;
    queue<int> q;
    q.push(u);
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        vis[u]=0;
        p[u]++;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(w+dis[u]<dis[v])
            {
                dis[v]=w+dis[u];
                if(!vis[v]&&p[v]<=n)
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
int main()
{
    int ml,md,a,b,d;
    while(~scanf("%d%d%d",&n,&ml,&md))
    {
        e=0;
        memset(head1,-1,sizeof(head1));
        while(ml--)
        {
            scanf("%d%d%d",&a,&b,&d);
            if(a<b)
                swap(a,b);
            add(edge1,head1,b,a,d);
            //a-b<=d
        }
        while(md--)
        {
            scanf("%d%d%d",&a,&b,&d);
            if(a<b)
                swap(a,b);
            add(edge1,head1,a,b,-d);
        }
        spfa(edge1,head1,1);
        if(p[n]>n)
            printf("-1\n");
        else if(dis[n]==INF)
            printf("-2\n");
        else
            printf("%d\n",dis[n]);
    }
    return 0;
}
时间: 2024-10-18 15:06:45

poj3169——Layout(差分约束+SPFA判断负环)的相关文章

[luoguP1993] 小 K 的农场(差分约束 + spfa 判断负环)

传送门 差分约束系统..找负环用spfa就行 ——代码 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #define N 100001 5 6 int n, m, cnt; 7 int head[N], to[N << 1], val[N << 1], next[N << 1], dis[N]; 8 bool vis[N]; 9 10 inline i

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

spfa 判断负环 (转载)

判断给定的有向图中是否存在负环. 利用 spfa 算法判断负环有两种方法: 1) spfa 的 dfs 形式,判断条件是存在一点在一条路径上出现多次. 2) spfa 的 bfs 形式,判断条件是存在一点入队次数大于总顶点数. 代码如下: 法 1 (spfa 的 dfs 形式): #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int oo = 1 &

POJ 3259 Wormholes【最短路/SPFA判断负环模板】

农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径,W(1≤W≤200)个虫洞.FJ作为一个狂热的时间旅行的爱好者,他要做到以下几点:开始在一个区域,通过一些路径和虫洞旅行,他要回到最开时出发的那个区域出发前的时间.也许他就能遇到自己了:).为了帮助FJ找出这是否是可以或不可以,他会为你提供F个农场的完整的映射到(1≤F≤5).所有的路径所花时间都

spfa判断负环

会了spfa这么长时间竟然不会判断负环,今天刚回.. [例题]poj3259 题目大意:当农场主 John 在开垦他的农场时,他发现了许多奇怪的昆虫洞.这些昆虫洞是单向的,并且可以把你从入口送到出口,并且使得时间倒退一段时间. John 的每个农场包含 N(1≤N≤500)块地,编号从 1-N,这 N 块地之间有 M(1≤M≤2500)条路. W(1≤W≤200)个昆虫洞.因为 John 是一个狂热的时间旅行迷,他想尝试着做这样一件事:从某块地出发,通过一些路径和昆虫洞,返回到出发点,并且时间早

Wormholes POJ - 3259 spfa判断负环

//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int N=1e5,INF=0x3f3f3f3f; int dist[N]; int h[N],e[N],ne[N],w[N],idx; int n,m,z; void add(int a,int b,in

POJ3259 Wormholes(SPFA判断负环)

Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac

POJ3259 Wormholes 【SPFA判断负环】

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 32111   Accepted: 11662 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

POJ3169:Layout(差分约束)

http://poj.org/problem?id=3169 题意: 一堆牛在一条直线上按编号站队,在同一位置可以有多头牛并列站在一起,但编号小的牛所占的位置不能超过编号大的牛所占的位置,这里用d[i]表示编 号为i的牛所处的位置,即要满足d[i]-d[i+1]<=0,同时每两头牛之间有以下两种关系(对于输入的a b d来说):             1>如果是喜欢关系:即需要满足d[b]-d[a]<=d             2>如果是讨厌关系:即需要满足d[b]-a[a]&