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

const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
struct E{
    int u,v,nxt,w;
    E(){}
    E(int u,int v,int nxt,int w):u(u),v(v),nxt(nxt),w(w){}
}eg[N*3];
int head[N],cnt[N],vis[N],tot;
ll d[N];
void init(){
    tot = 0 ; memset(head,-1,sizeof head);
}
void add(int u,int v,int w){
    eg[++tot].u = u;    eg[tot].v = v;  eg[tot].w = w;  eg[tot].nxt = head[u]; head[u] = tot;
}
int n,ml,md;
bool spfa(int s){
    fill(d,d+n+1,inf);
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    queue<int> q;
    q.push(s); d[s] = 0; vis[s] = 1;
    while(q.size()){
        int u = q.front();  q.pop();
        vis[u] = 0;
        for(int i=head[u];~i;i=eg[i].nxt){
            int v = eg[i].v , w = eg[i].w;
            if(d[v] > d[u] + w){
                d[v] = d[u] + w;
                cnt[v] = cnt[u] + 1;
                if(cnt[v]>n){
                    return true;
                }
                if(!vis[v]){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return false;
}
int main(){
    int u,v,w;
    init();
    scanf("%d%d%d",&n,&ml,&md);
    for(int i=1;i<=ml;++i){
        scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
        // add(v,u,w);
    }
    for(int i=1;i<=md;++i){
        scanf("%d%d%d",&u,&v,&w);
        add(v,u,-w);    // 移项变号
    }
    for(int i=1;i<n;++i){
        add(i+1,i,0);   // 如果能重合就是0,不能重合就是-1
    }
    if(spfa(1))    puts("-1");
    else if(d[n]==inf)  puts("-2");
    else printf("%lld\n",d[n]);
    return 0;
}

大佬博客先马住

原文地址:https://www.cnblogs.com/xxrlz/p/11617132.html

时间: 2024-10-07 15:30:21

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

题意:给定一些母牛,要求一个排列,有的母牛距离不能超过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(差分约束啊)

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

题目地址: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(