POJ-3169 Layout---差分约束系统+Bellman

题目链接:

https://vjudge.net/problem/POJ-3169

题目大意:

一些母牛按序号排成一条直线。有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离。如果没有输出-1,如果可以随便排输出-2,否则输出最大的距离。

Sample Input

4 2 1
1 3 10
2 4 20
2 3 3

Sample Output

27

思路:

设xi为第i头牛的x坐标

对于第一种要求,A和B之间距离不超过X(题目中A<B)

那就有:xB - xA <= X

对于第二种要求,A和B之间距离少于X(题目中A<B)

那就有:xB - xA >= X

对于上述两种不等式,可知这道题就是一堆不等式组,可以用差分约束系统来做。

u是起点,v是终点

对于第一种不等式,转化成A->B的边,权值为X

对于第二种不等式,先转化成上述形式,xA - xB <= -X,转化成B->A的边,权值是-X

在题目中隐含了一组不等式xi-1<=xi,转化成上述形式就是xi-1 - xi <= 0,边为i -> i-1权值是0

然后要看题目中求的是什么:

求的是从1到n的最大距离

就等价于xn-x1最大

等价于xn-x1 <= M需要求M的最小值(因为M没有最大值,最大值是正无穷)

这个不等式就是1到n的边,权值为M,说明题目求的是1到n的最短路,如果存在负环,输出-1,如果是INF输出-2,否则输出最短路长度。分析到这里就可以套模板啦

 1 #include<iostream>
 2 #include<vector>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdio>
 7 using namespace std;
 8 typedef pair<int, int> Pair;
 9 const int maxn = 25000 + 10;
10 const int INF = 0x3f3f3f3f;
11 int T, n, m, m1, m2;
12 struct edge
13 {
14     int u, v, w;
15     edge(int u, int v, int w):u(u), v(v), w(w){}
16     edge(){}
17 };
18 edge e[maxn];
19 int d[1005], tot;
20 void addedge(int u, int v, int w)
21 {
22     e[tot++] = edge(u, v, w);
23 }
24 bool Bellman()
25 {
26     int u, v, w;
27     memset(d, INF, sizeof(d));
28     d[1] = 0;
29     for(int j = 0; j < n; j++)
30     {
31         for(int i = 0; i < tot; i++)
32         {
33             u = e[i].u, v = e[i].v,  w = e[i].w;
34             if(d[u] < INF && d[v] > d[u] + w)
35             {
36                 d[v] = d[u] + w;
37                 if(j == n - 1)return true;//存在负环,方程组无解
38             }
39         }
40     }
41     return false;
42 }
43 int main()
44 {
45     cin >> n >> m1 >> m2;
46     int u, v, w;
47     for(int i = 0; i < m1; i++)//第一组边,u->v 权值w
48     {
49         scanf("%d%d%d", &u, &v, &w);
50         addedge(u, v, w);
51     }
52     for(int i = 0; i < m2; i++)//第二组边 v->u 权值-w
53     {
54         scanf("%d%d%d", &u, &v, &w);
55         addedge(v, u, -w);
56     }
57     for(int i = 1; i < n; i++)//第三组边 i+1->i 权值0
58         addedge(i + 1, i, 0);
59     if(Bellman())
60     {
61         cout<<"-1"<<endl;
62     }
63     else
64     {
65         if(d[n] == INF)cout<<"-2"<<endl;
66         else cout<<d[n]<<endl;
67     }
68     /*for(int i = 1; i <= n; i++)
69     {
70         cout<<i<<":::";
71         for(int j = 0; j < G[i].size(); j++)cout<<G[i][j].v<<"-"<<G[i][j].w<<"   ";
72         cout<<endl;
73     }*/
74 }

原文地址:https://www.cnblogs.com/fzl194/p/8798507.html

时间: 2024-12-26 10:27:05

POJ-3169 Layout---差分约束系统+Bellman的相关文章

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

PKU 3169 Layout(差分约束系统+Bellman Ford)

题目大意:原题链接 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相同的.因为奶牛相当苗条,所以可能有两头或者更多奶牛站在同一位置上(即间距可能为0).即是说,如果我们想象奶牛是站在一条数轴上的话,允许有两头或更多奶牛拥有相同的横坐标. 一些奶牛相互间存有好感,它们希望两者之间的距离不超过一个给定的数L.另一方面,一些奶牛相互间非常反感,它们希望两者间的距离不小于一个给定的数D.

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

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

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

题意: 有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(差分约束)

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

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