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 的边,求的是最短路,得到的是最大值(本题求的就是最大值),对于不等式 a - b >= c ,建一条 b 到 a 的权值为 c 的边,求的是最长路,得到的是最小值。

2.如果检测到负环,那么无解。

3.如果d[]没有更新,那么可以是任意解。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 10000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
struct node{
    int to, w, next, from;
};
int cnt = 0;
node G[maxn<<1];
int d[maxn];

void add(int u, int v, int w){
    G[cnt].w = w;
    G[cnt].to = v;
    G[cnt++].from = u;
}

bool Bell_Ford(){
    for(int i = 1; i <= n; ++i)  d[i] = INF;

    d[1] = 0;
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < cnt; ++j){
            int u = G[j].from;
            int v = G[j].to;
            int w = G[j].w;
            if(d[u] < INF && d[v] > d[u] + w){
                d[v] = d[u] + w;
            }
        }
    }

    for(int j = 0; j < cnt; ++j){
        int u = G[j].from;
        int v = G[j].to;
        int w = G[j].w;
        if(d[u] < INF && d[v] > d[u] + w)
            return true;
    }

    return false;
}

int main(){
//    freopen("in.txt", "r", stdin);
    int ml, md, u, v, w;
    cin >> n >> ml >> md;
    cnt = 0;

    for(int i = 0; i < ml; ++i){
        scanf("%d %d %d", &u, &v, &w);
        if(u < v)   swap(u, v);
        add(v, u, w);
    }

    for(int i = 0; i < md; ++i){
        scanf("%d %d %d", &u, &v, &w);
        if(u < v)  swap(u, v);
        add(u, v, -w);
    }

    if(Bell_Ford()) printf("-1\n");
    else if(d[n] == INF)  printf("-2\n");
    else  printf("%d\n", d[n]);
    return 0;
}
时间: 2024-08-14 04:49:54

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 (差分约束+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(差分约束啊)

题目链接: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(差分约束 线性差分约束)

题意: 有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] <=

POJ 3169 Layout (差分约束入门)

线性约束 将所有不等式化成 \(d[a] - d[b] <= c\) 的形式,即有 \(a,b,c\)这条有向边,跑最短路即可 #include<cstring> #include<iostream> #include<algorithm> #include<queue> #include<vector> #define ll long long using namespace std; typedef pair<int,int>

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 (图论-差分约束)

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 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(