POJ 3169 Layout(差分约束_ Bellman-ford)

题意:

给你n 只牛。

告诉你一些牛的最远距离是多少, 告诉你一些牛的最近距离是多少, 并且 第i 只牛一定在第i-1只牛的右边,求1到n的最远距离, 不存在输出-1, 无限远输出-2;

思路:

差分约束。

我们知道最短路的判断:

如果d[e.to] >= d[e.from] + e.dist;

那么d[e.to] = d[e.from] + e.dist

所以 d[e.to] <= d[e.from] + e.dist;

因此我们只需要构造出上面的式子,然后 从from 到 to 连一条边,权值为e.dist 求最短路即可。

这里 对于最远的牛。

u,v,w 

d[v] – d[u] <= w  —>    d[v] <= d[u] + w

因此我们从u到v 连一条边。 边权为w。

对于最近的牛。

u,v,w;

d[v] – d[u] >= w —> d[u] <= d[v] – w

因此 我们从v到u 连一条边,边权为-w

还有一个条件 第i 只牛一定在第i-1只牛的右边

那么d[i] – d[i-1] >= 0 –>  d[i-1] <= d[i]

因此从i 到i-1 连一条边 边权为0 .

求最短路即可, 出现负环 显然无解,  d[n] 是inf 表示无限远。

二逼的我 bellman_ford 一个小地方写错了 wa 了一中午。

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

const int maxn = 1000 + 7;

const int inf = 0x3f3f3f3f;

int n,q1,q2;

struct Edge{
    int f,t,w;
    Edge(int f = 0, int t = 0, int w = 0): f(f), t(t), w(w){}
};



struct BBBBB{

    int n,m;
    vector<int> g[maxn];
    vector<Edge> edges;
    int d[maxn];
    queue<int>q;
    bool inq[maxn];
    int cnt[maxn];
    void init(int n){
        this->n = n;
        for (int i = 1; i <= n; ++i)g[i].clear();
        edges.clear();
        m = 0;
    }

    void add(int f,int t,int w){
        edges.push_back(Edge(f,t,w));
        g[f].push_back(m++);
    }

    bool bellman_ford(int s){
        memset(d,inf,sizeof d);

        memset(inq,0,sizeof inq);
        memset(cnt,0,sizeof cnt);
        while(!q.empty()) q.pop();
        d[s] = 0;
        inq[s] = 1;
        q.push(s);

        while(!q.empty()){
            int u = q.front(); q.pop();
            inq[u] = 0;
            for (int i = 0; i < g[u].size(); ++i){

                int v = g[u][i];
                Edge& e =  edges[v];
                if (d[u] < inf && d[e.t] > d[u] + e.w){
                    d[e.t] = d[u] + e.w;
                    if (!inq[e.t]){
                        q.push(e.t);
                        inq[e.t] = 1;
                        if (++cnt[e.t] > n) return false;
                    }
                }
            }
        }
        return true;
    }
}dic;

int main(){

    scanf("%d %d %d",&n, &q1, &q2);
    int u,v,w;
    dic.init(n);
    while(q1--){
        scanf("%d %d %d",&u, &v, &w);
        dic.add(u,v,w);
    }
    while(q2--){
        scanf("%d %d %d",&u, &v, &w);
        dic.add(v,u,-w);
    }

    for (int i = 2; i <= n; ++i){
        dic.add(i,i-1,0);
    }

    if (!dic.bellman_ford(1)) puts("-1");
    else {
        if (dic.d[n] == inf) puts("-2");
        else printf("%d\n",dic.d[n]);
    }
    return 0;
}

/**
5 2 1
2 4 10
3 5 20
3 4 17
**/

Layout

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11054 Accepted: 5316

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

Hint

Explanation of the sample: 

There are 4 cows. Cows #1 and #3 must be no more than 10 units apart, cows #2 and #4 must be no more than 20 units apart, and cows #2 and #3 dislike each other and must be no fewer than 3 units apart. 

The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.

Source

USACO 2005 December Gold

[Submit]   [Go Back]   [Status]   [Discuss]

《POJ 3169 Layout(差分约束_ Bellman-ford)》Home Page   《POJ 3169 Layout(差分约束_ Bellman-ford)》Go Back  《POJ 3169 Layout(差分约束_ Bellman-ford)》To top

    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/aozil_yang/article/details/68486529
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞