POJ 3169 Layout(差分约束 线性差分约束)

题意:

有N头牛, 有以下关系:

(1)A牛与B牛相距不能大于k

(2)A牛与B牛相距不能小于k

(3)第i+1头牛必须在第i头牛前面

给出若干对关系(1),(2)

求出第N头牛与第一头牛的最长可能距离, 若无解输出-1, 若无限长输出-2

分析:

3个关系对应的 <= 式子是:

dis[b] - dis[a] <= d(1)

dis[a] - dis[b] <= -d(2)

dis[i] - dis[i+1] <= -1(2)

目标式:dis[N] - dis[1] <= T

要求这个T就是建好图后跑源点为1的最短路, 有负权环路则无解输出-1, 不连通则输出-2(无约束关系)。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstring>
#include <cmath>
#include <iomanip>
#define rep(i,a,b) for(int i = a; i < b;i++)
#define _rep(i,a,b) for(int i = a; i <= b;i++)
using namespace std;
const int inf = 1e9 + 7;
const int maxn = 1000 + 7;
int n , m;
struct edge{
    int to , d;
    edge(int _to, int _d): to(_to), d(_d){}
};
vector<edge> G[maxn];
void add_edge(int u, int v, int d){
    G[u].push_back(edge(v,d));
}
int N , ML, MD;
int dis[maxn], enter_cnt[maxn];
int spfa(){
    fill(dis, dis+maxn, inf);
    memset(enter_cnt, 0, sizeof(enter_cnt));
    bool vis[maxn];
    memset(vis, 0, sizeof(vis));

    queue<int> q;
    vis[1] = 1;
    dis[1] = 0;
    q.push(1);
    ++enter_cnt[1];

    while(!q.empty()){
        int u = q.front();
        rep(i,0,G[u].size()){
            int v = G[u][i].to, d = G[u][i].d;
            if(dis[v] > dis[u] + d){
                dis[v] = dis[u] + d;
                if(!vis[v]){
                    if(++enter_cnt[v] >= N) return -1;//入队次数大于等于N代表存在负权环路
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
        vis[u] = 0;
        q.pop();
    }
    return dis[N];
}
int main(){
    cin >> N >> ML >> MD;
    rep(i,0,ML){
        int a, b, d;
        cin >> a >> b >> d; //dis[b] - dis[a] <= d
        add_edge(a,b,d);
    }
    rep(i,0,MD){
        int a, b, d;
        cin >> a >> b >> d; // dis[b] - dis[a] >= d  ->  dis[a] - dis[b] <= -d
        add_edge(b,a,-d);
    }
    rep(i,1,N){ //dis[i+1] - dis[i] >= 1 ->  dis[i] - dis[i+1] <= -1
        add_edge(i+1,i,-1);
    }
    int ans = spfa();
    if(ans == -1){
        printf("-1\n");
    }else if(ans == inf){
        printf("-2\n");
    }else printf("%d\n", ans);
    return 0;
}

原文地址:https://www.cnblogs.com/Jadon97/p/8353598.html

时间: 2024-10-13 12:06:15

POJ 3169 Layout(差分约束 线性差分约束)的相关文章

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

POJ 3169 Layout (图论-差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 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

poj 3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6549   Accepted: 3168 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

POJ 3169 Layout (差分约束系统)

题目地址:POJ 3169 很简单的差分约束..公式很明显.当输入最大值的时候,是a-b<=c,最小值的时候是a-b>=c.然后根据这个式子用最短路求. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctyp

poj 3169 Layout (差分约束+Bellman )

题目链接:http://poj.org/problem?id=3169 题意:输入N, ML, MD, N默示有N个牛按1-N排成一排,ML,默示有ML行,每行输入A, B, D默示A牛和B牛最远间隔为D, MD默示有MD行,每行输入A,B,D默示A牛和B来间隔为D,求满足所有前提的1-N的最大间隔. 比较简单的差分约束,这个周周赛的A题 #include <iostream> #include <cstdlib> #include <cstdio> #include

POJ 3169 Layout (差分约束)

题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个,D[i] - D[i+1] <= 0,  D[j] -D[i ]<= k, D[i] - D[j] <= - k,那么这个题就可以用差分约束来求这个不等式组了. 1.对于差分不等式,a - b <= c ,建一条 b 到 a 的权值为 c 的边,求的是最短路,得到的是最大值(本题求的就

(简单) POJ 3169 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 numbe

POJ 3169 Layout(差分约束啊)

题目链接:http://poj.org/problem?id=3169 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

差分约束Poj 3169 Layout

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 #include <cstdio> #include <