赞
踩
有一个bool A[]
的原数组 (取值0/1
), 他对应一个int sum[]
的前缀和数组;
我们有关系: A[l, l+1, ..., r] >= k
, 即对应不等式: sum[r] - sum[l-1] >= k
, 对应差分约束的有向边为: sum[l-1] + k <= sum[r]
即(l-1) -> (r) [w]
;
这一类关系: 即(l-1) -> (r) [w]
;
第二类关系 也容易考虑到, 即A[] >= 0
, 对应(i-1) -> (i) [0]
;
但一定不要忘记, 还有一个限制, 即A[] <= 1
, 故必须还要有: (i) -> (i-1) [-1]
, 否则就错了;
//{ InequationSystem-Declaration template< class _Type_Edge, class _Type_Distance> class InequationSystem{ public: const _Type_Distance * Distance; //-- variable ends InequationSystem( const Graph_Weighted< _Type_Edge> *); bool Initialize(); private: const Graph_Weighted< _Type_Edge> * graph; int points_range; _Type_Distance * distance; bool * isInQueue; int * queue; int queue_head, queue_tail; int * pathLength; }; //} InequationSystem-Declaration //{ InequationSystem-Implementation //< // @Brief // . A edge `a->b (w)` in `graph`, means a inequation `Distance[a] + w <= Distance[b]` (`w` can be any-integer); // . This algorithm use **SPFA** to get the **Maximal-Distance** for each point, finally `Distance[x] >= 0` if this Inequation-System is solvable // . More precisely, `Distance[x]` would be the **Minimal-Value (>=0)** under the whole Inequation-System; // @Function( Initialize) // . If return `true` when this Inequation-System is solvable (then the answer is `Distance[]`), otherwise return `false; // @Hint // . Cuz the core-algorithm is SPFA, if TimeOut occurs, you can use the next-code in `@Mark_0`; // . if( ++ timeOut > points_range * @Unfinished(5/10/15/...)){ return true;} template< class _Type_Edge, class _Type_Distance> InequationSystem< _Type_Edge, _Type_Distance>::InequationSystem( const Graph_Weighted< _Type_Edge> * _graph) : graph( _graph){ distance = new _Type_Distance[ graph->Get_pointsCountMaximum()]; Distance = distance; isInQueue = new bool[ graph->Get_pointsCountMaximum()]; queue = new int[ graph->Get_pointsCountMaximum() + 1]; pathLength = new int[ graph->Get_pointsCountMaximum()]; } template< class _Type_Edge, class _Type_Distance> bool InequationSystem< _Type_Edge, _Type_Distance>::Initialize(){ points_range = graph->Get_pointsCount(); memset( distance, 0, sizeof( _Type_Distance) * points_range); memset( pathLength, 0, sizeof( int) * points_range); for( int i = 0; i < points_range; ++i){ queue[ i] = i;} memset( isInQueue, true, sizeof( bool) * points_range); queue_head = 0, queue_tail = points_range; //-- int cur, nex, edge; while( queue_head != queue_tail){ //>< @Mark_0 cur = queue[ queue_head ++]; isInQueue[ cur] = false; if( queue_head > points_range){ queue_head = 0;} for( edge = graph->Head[ cur]; ~edge; edge = graph->Next[ edge]){ nex = graph->Vertex[ edge]; if( distance[ cur] + graph->Weight[ edge] > distance[ nex]){ pathLength[ nex] = pathLength[ cur] + 1; if( pathLength[ nex] >= points_range){ return false; } distance[ nex] = distance[ cur] + graph->Weight[ edge]; if( false == isInQueue[ nex]){ isInQueue[ nex] = true; queue[ queue_tail ++] = nex; if( queue_tail > points_range){ queue_tail = 0;} } } } } return true; } //} InequationSystem-Implementation
CSDN-129476594
;
Cuz this kind of problem must corresponds to a Graph
G
G
G (an edge
a
→
b
(
w
)
a\to b \ (w)
a→b (w) always denotes
V
[
a
]
+
w
≤
V
[
b
]
V[a] + w \leq V[b]
V[a]+w≤V[b], then calculating the Minimal-Value for every
V
[
x
]
V[x]
V[x] under the premise
V
[
a
l
l
]
≥
0
V[ all] \geq 0
V[all]≥0);
.
Although the Normal-SPFA can solve the problem, but it maybe Out-Of-Time, that is, if there is better algorithms, we should always give up the idea of SPFA;
.
According to the different characteristics of the Graph, there are the best-method to solving the problem :
+
If
G
G
G is a
D
A
G
DAG
DAG (whatever the edge-weight is, positive/zero/negative), see the below-section;
+
If all edges-weight of
G
G
G are
≥
1
\geq 1
≥1, see the below-section;
+
If all edges-weight of
G
G
G are
≥
0
\geq0
≥0, see the below-section;
--
The optimal-method is:
ASSERT( the problem must be Consistent/Solvable);
V[] = 0;
for( c : $(the topological-sequence of G){
ASSERT( V[c] is the answer of `c`);
for( n : $(the adjacent-points of `c`)){
w = the weight of the edge;
V[ n] = max( V[n], max( V[c] + w, 0)); //< find the Maximal-Distance of `n` from any start-point in the DAG, and also ensure `V[all]` always `>= 0`;
}
}
G
G
G such that all edges-weight are
≥
1
\geq 1
≥1, an edge denotes an equality
a
+
w
≤
b
a + w \leq b
a+w≤b where
w
≥
1
w \geq 1
w≥1;
+
The problem is Inconsistent if and only if
G
G
G is not a DAG;
.
Otherwise (i.e., Consistent), let
D
i
s
t
[
x
]
Dist[ x]
Dist[x] be the Maximal-Distance from any start-point in the DAG to
x
x
x; finally, the answer is
D
i
s
t
Dist
Dist (that is, the Minimal-Value under the condition
D
i
s
t
[
x
]
≥
0
Dist[x] \geq 0
Dist[x]≥0);
G
G
G such that all edges-weight are
≥
0
\geq 0
≥0, an edge denotes an equality
a
+
w
≤
b
a + w \leq b
a+w≤b where
w
≥
0
w \geq 0
w≥0;
+
The problem is Inconsistent if and only if There is an Non-Zero edge within a
S
C
C
SCC
SCC of
G
G
G;
.
Otherwise (i.e., Consistent), let
G
1
G1
G1 be the DAG after shrinking all
S
C
C
SCC
SCCs of
G
G
G,
D
i
s
t
[
x
]
Dist[ x]
Dist[x] where
x
∈
G
1
x \in G1
x∈G1 be the Maximal-Distance from any start-point in the DAG
G
1
G1
G1 to
x
x
x; finally, the answer of
a
a
a equals to
D
i
s
t
[
A
]
Dist[ A]
Dist[A] where
A
∈
G
1
A \in G1
A∈G1 is the SCC of
a
∈
G
a \in G
a∈G (that is, the Minimal-Value under the condition
∀
a
∈
G
,
≥
0
\forall a \in G, \geq 0
∀a∈G,≥0);
.
Note that, any edge
a
→
b
(
w
)
a\to b (w)
a→b(w) in
G
G
G where
a
,
b
a,b
a,b not belongs to the same
S
C
C
SCC
SCC, should corresponds to an edges
A
→
B
(
w
)
A\to B (w)
A→B(w) in
G
1
G1
G1 where
A
,
B
A,B
A,B denotes the
S
C
C
SCC
SCC; equivalently, we can choose a Maximal-Edge
a
→
b
(
w
)
a\to b(w)
a→b(w) as the unique-edge
A
→
B
A \to B
A→B
@Delimiter( below are the old-interpretation)
If
w
≥
0
w \geq 0
w≥0 for all
x
i
+
w
≤
x
j
xi + w \leq xj
xi+w≤xj, as we know, it forms a Directed-Graph, and we should calculate Greatest-Path with the origin
D
i
s
t
[
a
l
l
]
=
0
Dist[ all] = 0
Dist[all]=0;
1
Inconsistent (there exists a Positive-Circuit)
⇔
\Leftrightarrow
⇔ there exists a edge
>
0
> 0
>0 in a SCC;
.
Proof
p
→
q
p \to q
p→q, a Positive-Circuit must belongs to a SCC;
.
Proof
q
→
p
q \to p
q→p, due to all edges
≥
0
\geq 0
≥0, a circuit that contains a
>
0
>0
>0 edge in a SCC, the sum of this circuit must
>
0
> 0
>0;
.
As a lemma, if it is consistent, all edges in a SCC are
0
0
0.
2
The maximal-distance of a point
a
a
a
⇔
\Leftrightarrow
⇔ the maximal-distance of a SCC (to which the point
a
a
a belongs) in the DAG (after shrinking-SCC);
.
Now, all edges in a SCC are
0
0
0 (so, the
D
i
s
t
[
]
Dist[]
Dist[] of all points in a SCC are the same), all edge in DAG
≥
0
\geq 0
≥0; the maximal-distance of a point
a
a
a, must with the form
s
→
.
.
.
→
a
s \to ... \to a
s→...→a, it is equivalent to its corresponding DAG-Path
S
→
.
.
.
→
A
S \to ... \to A
S→...→A (cuz any edge within a SCC is
0
0
0); therefore, we calculate the Maximal-distance on a DAG;
Total cost: O ( n + m ) ∗ 2 O(n+m) * 2 O(n+m)∗2
Tarjan_SCC< int> tarjan( &G); //< all edges in `G` are `> 0` tarjan.Initialize(); //{ for( int i = 0; i < n; ++i){ for( int j, z = G.Head[ i]; ~z; z = G.Next[ z]){ j = G.Vertex[ z]; if( (tarjan.Scc_id[ i] == tarjan.Scc_id[ j]) && (G.Weight[ z] != 0)){ cout<< "Inconsistent"; return; } } } //} auto DAG = tarjan.Build_DAG(); DAG_Tools< int> dag_tool( DAG); dag_tool.IfIsDAG_andThen_getTopologySequence(); vector< int> Dist( DAG->Get_pointsCount(), 0); for( int i = 0; i < DAG->Get_pointsCount(); ++i){ int c = dag_tool.Topological_Sequence[ i]; FOR_EDGES_( c, nex, wth, DAG->) Dist[ nex] = max( Dist[ c] + wth, Dist[ nex]); } } for( int i = 0; i < n; ++i){ Dist[ tarjan.Scc_id[ i]]; //< the answer of point `i`; (the minimal-value under the condition `all xi >=0 ` }
The usual question is calculating the minimal-value for every x i xi xi under the condition x i ≥ d ∀ x i xi \geq d \quad \forall xi xi≥d∀xi
But when the question is that calculating the maximal-value for every x i xi xi under the condition x i ≤ d ∀ x i xi \leq d \quad \forall xi xi≤d∀xi, it also can be solved:
Whatever the problem is, making sure that every inequality always be the form of x i + w ≤ x j xi + w \leq xj xi+w≤xj which corresponds to a edge x i → x j xi \to xj xi→xj with weight w w w. (Any Inequalities-System problem can always be solved in this form)
The Greatest-Path of a point may contains Negative-Edge (e.g., the edges on the path are 10, -1
)
Given a sequence of variables x 1 , x 2 , . . . , x n x1, x2, ..., xn x1,x2,...,xn, and a Inequality-Group which consists of a set of relations with the form x i + c ≤ x j xi + c \leq xj xi+c≤xj where c c c is a constant.
The question is, find out the minimal value of every x i xi xi under the condition ( a l l x i ) ≥ d (all \ xi) \geq d (all xi)≥d (or find out the minimal sum of x i xi xi under the same condition, actually they are the same), (or find out the maximal value of every x i xi xi under the condition ( a l l x i ) ≤ d (all \ xi) \leq d (all xi)≤d, in fact they are also the same). A another view-point see Geometrical description
It can be proved that the solution-set is either infinite or empty (no solution / inconsistent) .
For example
.
x
1
+
1
≤
x
2
x1 + 1 \leq x2
x1+1≤x2 implies the solutions
.
.
.
(
−
1
,
≥
0
)
,
(
0
,
≥
1
)
,
(
1
,
≥
2
)
.
.
.
... (-1, \geq 0), (0, \geq 1), (1, \geq 2) ...
...(−1,≥0),(0,≥1),(1,≥2)... (abbr.,
(
x
,
>
x
)
(x, > x)
(x,>x))
.
x
1
≤
x
2
,
x
2
≤
x
1
x1 \leq x2, x2 \leq x1
x1≤x2,x2≤x1 implies the solutions
.
.
.
(
−
1
,
−
1
)
,
(
0
,
0
)
,
(
1
,
1
)
.
.
.
... (-1, -1), (0, 0), (1, 1) ...
...(−1,−1),(0,0),(1,1)... (abbr.,
(
x
,
x
)
(x,x)
(x,x))
.
x
1
+
1
≤
x
2
,
x
2
+
1
≤
x
1
x1 + 1\leq x2, x2 + 1\leq x1
x1+1≤x2,x2+1≤x1 implies no-solution, that is inconsistent.
As a lemma, if ( c 1 , c 2 , c 3 , . . . ) (c1, c2, c3, ...) (c1,c2,c3,...) is a solution, then ( c 1 + k , c 2 + k , c 3 + k , . . . ) (c1 + k, c2 + k, c3 + k, ...) (c1+k,c2+k,c3+k,...) is also a solution. (cuz x i + w ≤ x j xi + w \leq xj xi+w≤xj then x i + w + k ≤ x j + k xi + w + k \leq xj + k xi+w+k≤xj+k)
Cuz
x
i
<
x
j
xi < xj
xi<xj can transformed to
x
i
+
1
≤
x
j
xi + 1 \leq xj
xi+1≤xj
x
i
>
x
j
→
x
j
+
1
≤
x
i
xi > xj \quad \to \quad xj +1 \leq xi
xi>xj→xj+1≤xi
x
i
=
x
j
→
x
j
≤
x
i
,
x
i
≤
x
j
xi = xj \quad \to \quad xj \leq xi, xi \leq xj
xi=xj→xj≤xi,xi≤xj
x
i
≥
x
j
→
x
j
≤
x
i
xi \geq xj \quad \to \quad xj \leq xi
xi≥xj→xj≤xi
Consequently, the general form of a relation is
x
i
+
c
R
x
j
xi + c \ R \ xj
xi+c R xj where
R
R
R can be
=
,
<
,
>
,
≤
,
≥
=, <, >, \leq, \geq
=,<,>,≤,≥ and
c
c
c is a constant.
.
But, the relation
x
i
R
c
xi \ R \ c
xi R c where
c
c
c is a constant is Invalid, it can’t be contained in this system, cuz it not satisfies the form
x
i
+
c
≤
x
j
xi +c \ \leq \ xj
xi+c ≤ xj.
.
Moreover, the relation
x
i
+
x
j
≤
c
xi + xj \leq c
xi+xj≤c is invalid, it corresponds to
x
i
−
c
≤
−
x
j
xi - c \leq -xj
xi−c≤−xj, note this case.
Firstly, let’s introduce the notion of Constant-Compare and Variable-Compare.
Constant-Compare:
x
<
y
x < y
x<y where
x
,
y
x, y
x,y are both certain constant (e.g.,
x
=
1
,
y
=
2
x=1, y=2
x=1,y=2).
.
let
a
,
b
,
c
a, b, c
a,b,c be arbitrary constant, if we have
a
<
b
a < b
a<b and
b
<
c
b < c
b<c, then there must have
a
<
c
a < c
a<c; more generally,
(
a
+
w
1
<
b
,
b
+
w
2
<
c
)
→
(
a
+
w
1
+
w
2
<
c
)
(a+w1<b, b+w2<c) \to (a+w1+w2<c)
(a+w1<b,b+w2<c)→(a+w1+w2<c), cuz
a
+
w
1
<
b
<
c
−
w
2
a + w1 < b < c - w2
a+w1<b<c−w2, and then
a
+
w
1
+
w
2
<
b
+
w
2
<
c
a+w1+w2<b+w2<c
a+w1+w2<b+w2<c; we call it the property of Transitive. (i.e., we deduced a new relation based on two relations)
Variable-Compare:
x
<
y
x < y
x<y where
x
,
y
x, y
x,y are variable, that is the value of
x
,
y
x, y
x,y has multiple choice.
.
let
x
,
y
,
z
x, y, z
x,y,z be variable, if we have
x
<
y
x < y
x<y and
y
<
z
y < z
y<z, then it not satisfies the property Transitivity. More precisely,
x
<
z
x < z
x<z is just a necessity, but not a Necessity-Sufficiency. Cuz based on
x
<
y
,
y
<
z
x<y, y<z
x<y,y<z we can deduced that
z
>
x
+
1
z > x + 1
z>x+1 (cuz the range of
y
y
y is
[
x
+
1
,
∞
)
[x +1, \infty)
[x+1,∞) from
x
<
y
x<y
x<y, the range of
z
z
z is
[
y
+
1
,
∞
)
[y + 1, \infty)
[y+1,∞) from
y
<
z
y < z
y<z, so combine them we conclude that the range of
z
z
z is
[
x
+
2
,
∞
)
[x+2, \infty)
[x+2,∞), so,
x
<
z
x < z
x<z is just a necessity of
x
+
1
<
z
x + 1 < z
x+1<z which is the correct-relation. (e.g.,
x
<
y
,
y
<
z
,
z
<
k
x<y, y<z, z<k
x<y,y<z,z<k, we have
x
+
2
<
k
x + 2<k
x+2<k)
.
This maybe some sophisticated, let’s discuss further, the reason why Transitivity must based on Necessity-Sufficiency is that, two relations
R
1
,
R
2
R1, R2
R1,R2 deduced a new relation
R
3
R3
R3, so that
R
3
R3
R3 can totally replace these previous two relations! that is
R
1
:
x
+
w
1
<
y
R1: x + w1 < y
R1:x+w1<y and
R
2
:
y
+
w
2
<
z
R2: y + w2 < z
R2:y+w2<z, then we know that
z
z
z is confined by
R
1
,
R
2
R1, R2
R1,R2 simultaneously, now we want to use just one relation
R
3
R3
R3 to replace the role acted above by
R
1
,
R
2
R1, R2
R1,R2. Therefore,
R
3
R3
R3 must be equivalent to
R
1
,
R
2
R1, R2
R1,R2 (not just necessity). For example,
x
<
y
,
y
<
z
x<y, y<z
x<y,y<z tells us that
x
+
1
<
z
x + 1 < z
x+1<z; however the relation
x
<
z
x < z
x<z deduced by Transitivity tells us
x
<
z
x < z
x<z which indicates that
z
z
z can choose
x
+
1
x + 1
x+1, but in fact
z
z
z can’t be
x
+
1
x+1
x+1.
In summary,
<
,
>
<, >
<,> has the property of Transitivity under Constant-Compare, but not for Variable-Compare.
That is,
(
a
1
+
w
1
<
a
2
,
a
2
+
w
2
<
a
3
,
.
.
.
,
a
m
+
w
m
<
a
k
)
→
(
a
1
+
w
1
+
w
2
+
.
.
.
+
w
m
<
a
k
(a1 + w1 < a2, a2 + w2 < a3, ..., am + wm < ak) \to (a1 + w1 + w2 + ... + wm < ak
(a1+w1<a2,a2+w2<a3,...,am+wm<ak)→(a1+w1+w2+...+wm<ak satisfied only under Constant-Compare (i.e.,
a
i
ai
ai is constant).
Let’s see the case of
≤
,
≥
\leq, \geq
≤,≥.
You can prove that, the Transitivity
(
a
1
+
w
1
≤
a
2
,
a
2
+
w
2
≤
a
3
,
.
.
.
,
a
m
+
w
m
≤
a
k
)
→
(
a
1
+
w
1
+
w
2
+
.
.
.
+
w
m
≤
a
k
(a1 + w1 \leq a2, a2 + w2 \leq a3, ..., am + wm \leq ak) \to (a1 + w1 + w2 + ... + wm \leq ak
(a1+w1≤a2,a2+w2≤a3,...,am+wm≤ak)→(a1+w1+w2+...+wm≤ak satisfied whichever Constant-Compare or Variable-Compare (i.e., it also satisfied if
a
i
ai
ai is variable).
Cuz this question is based on Variable-Compare and only ≤ ≥ \leq \geq ≤≥ satisfied the Transitivity, so we should transform all relation to the form x i + w 1 ≤ x j xi + w1 \leq xj xi+w1≤xj ( ≥ \geq ≥ is also usable, we will talk about it below, but the default choice is ≤ \leq ≤)
There is a rule (or stipulation), the constant-value w w w should always be placed on the left-side, that is the form x + w ≤ y x + w \leq y x+w≤y and x + w ≥ y x + w \geq y x+w≥y. If not, you should move the constant w w w to the left.
We introduced Transitivity above, now we talk about its application.
For a relation
x
+
w
≤
y
x + w \leq y
x+w≤y, cuz it shows a infinite solution set
.
.
.
(
−
1
,
≥
(
−
1
+
w
)
)
,
(
0
,
≥
(
0
+
w
)
)
,
(
1
,
≥
(
1
+
w
)
)
,
.
.
.
... (-1, \geq (-1+w)), (0, \geq (0+w)), (1, \geq (1+w)), ...
...(−1,≥(−1+w)),(0,≥(0+w)),(1,≥(1+w)),..., or abbreviated
(
x
,
≥
(
x
+
w
)
)
(x, \geq (x+w))
(x,≥(x+w)); we find that
x
x
x has infinite choices
.
.
.
,
−
1
,
0
,
1
,
.
.
.
...,-1,0,1,...
...,−1,0,1,... and
y
y
y has also infinite choices
x
+
w
,
x
+
w
+
1
,
x
+
w
+
2
,
.
.
.
x+w, x+w+1, x+w+2, ...
x+w,x+w+1,x+w+2,..., that is two sides are both infinite. Our device is fixing the left-side, then only the right-side is infinite.
Subsequently, once
x
x
x has fixed, then the solution/range of
y
y
y is the form
[
a
,
∞
)
[a, \infty)
[a,∞), therefore we can use a single number
a
a
a to denote the minimal available number of
y
y
y.
If we construct a graph by using a edge
x
→
y
x \to y
x→y with weight
w
w
w to represents the relation
x
+
w
≤
y
x + w \leq y
x+w≤y.
e.g., for the fixed
x
x
x, the relations
x
+
2
≤
y
x + 2 \leq y
x+2≤y and
x
+
5
≤
y
x + 5 \leq y
x+5≤y implies the range of
y
y
y are
[
x
+
2
,
.
.
.
)
[x+2, ...)
[x+2,...) and
[
x
+
5
,
.
.
.
)
[x+5, ...)
[x+5,...) respectively, then the answer range of
y
y
y should be intersection of these two ranges, that is
[
x
+
5
,
.
.
.
)
[x+5, ...)
[x+5,...) is the answer, we use a single number
x
+
5
x+5
x+5 to denote the answer. Consequently, we should run the Greatest-Path on this graph.
Conclusion: We set the start-points D i s t [ a l l ] = 0 Dist[all] = 0 Dist[all]=0, then run a Greatest-Path SPFA on G, if we found a Positive-Loop meaning that it’s No-Solution; otherwise, D i s t [ x ] Dist[x] Dist[x] means that the minimal-available-number of x x x under the condition D i s t [ a l l ] ≥ 0 Dist[all] \geq 0 Dist[all]≥0.
Cuz we have stipulated that
x
i
xi
xi is fixed and
x
j
xj
xj is confined by
x
i
xi
xi by the relation
x
i
+
w
≤
x
j
xi + w \leq xj
xi+w≤xj; cuz
x
i
xi
xi maybe also confined by
x
z
+
w
≤
x
i
xz + w \leq xi
xz+w≤xi, that is
x
j
xj
xj is confined by two relation, and maybe more;
So, any restriction on
x
i
xi
xi is a chain
x
a
+
w
≤
x
b
,
x
b
+
w
≤
x
c
,
.
.
.
,
x
d
+
w
≤
x
i
xa + w \leq xb, xb + w \leq xc, ..., xd + w \leq xi
xa+w≤xb,xb+w≤xc,...,xd+w≤xi, according to the property of Transitivity, it can be combined into
x
z
+
w
1
+
w
2
+
.
.
.
≤
x
i
xz + w1 + w2 + ... \leq xi
xz+w1+w2+...≤xi; we found that it corresponds to a path on the graph, i.e., any path
s
→
.
.
.
→
x
i
s \to ... \to xi
s→...→xi means a restriction on
x
i
xi
xi; e.g., two paths
D
i
s
t
[
s
1
]
+
w
1
=
d
1
Dist[s1] + w1 = d1
Dist[s1]+w1=d1 and
D
i
s
t
[
s
2
]
+
w
2
=
d
2
Dist[s2] + w2 =d2
Dist[s2]+w2=d2 means that the range of
x
i
xi
xi should be
[
d
1
,
∞
)
[d1, \infty)
[d1,∞) and
[
d
2
,
∞
)
[d2, \infty)
[d2,∞), and then we choose the intersection
m
a
x
(
d
1
,
d
2
)
max(d1,d2)
max(d1,d2) which corresponds to the Greatest-Path.
According to the property of Greatest-Path, if it is Solvable,
D
i
s
t
[
a
l
l
]
≥
0
Dist[all] \geq 0
Dist[all]≥0 finally. Cuz
D
i
s
t
[
x
]
Dist[x]
Dist[x] is unique, the answer solution-set
D
i
s
t
[
.
.
.
]
Dist[...]
Dist[...] is unique;
e.g.,
x
+
1
≤
y
,
y
+
0
≤
z
,
z
+
0
≤
y
,
x
+
2
≤
w
x + 1 \leq y, y + 0 \leq z, z + 0 \leq y, x + 2 \leq w
x+1≤y,y+0≤z,z+0≤y,x+2≤w, finally we get the solution-set:
D
i
s
t
[
x
]
=
0
,
D
i
s
t
[
y
,
z
]
=
1
,
D
i
s
t
[
w
]
=
2
Dist[x] = 0, Dist[y,z] = 1, Dist[w] = 2
Dist[x]=0,Dist[y,z]=1,Dist[w]=2 which is unique; in other words, under the condition
D
i
s
t
[
a
l
l
]
≥
0
Dist[all] \geq 0
Dist[all]≥0, the minimal value of
x
x
x is
0
0
0 which is unique (
0
0
0 maybe also the only available choice of
x
x
x under this solution-set (i.e.,
(
x
,
1
,
1
,
2
)
(x,1,1,2)
(x,1,1,2) is not a solution-set if
x
≠
0
x \neq 0
x=0), but also maybe not (i.e.,
(
x
,
1
,
1
,
2
)
(x,1,1,2)
(x,1,1,2) is also a solution-set for some values
x
≠
0
x \neq 0
x=0), it will discussed further in the next section).
As a lemma, under the condition
x
i
≥
0
xi \geq 0
xi≥0, two statements: 1. find out the minimal-value for every
x
i
xi
xi and 2. find out the minimal-value of the sum of all
x
i
xi
xi, are equivalent.
x − 5 ≤ y x - 5 \leq y x−5≤y, when we set x = 0 x = 0 x=0, the range of y y y is [ − 5 , ∞ ) [-5, \infty) [−5,∞), but the algorithm will get D i s t [ y ] = 0 Dist[y] = 0 Dist[y]=0 not − 5 -5 −5; because we have the restriction The value of all variables should ≥ 0 \geq 0 ≥0.
According to the property of Greatest-Path and set all-points be the start-points with the same
D
i
s
t
[
a
l
l
]
=
c
Dist[all]=c
Dist[all]=c, the answer got above under the condition
x
i
≥
0
xi \geq 0
xi≥0 can be generalized to the condition
x
i
≥
d
xi \geq d
xi≥d.
That is, set the start-points
D
i
s
t
[
a
l
l
]
=
d
Dist[all] = d
Dist[all]=d not
0
0
0, we have
D
i
s
t
[
x
]
=
D
[
x
]
+
d
Dist[x] = D[x] + d
Dist[x]=D[x]+d where
D
[
x
]
D[x]
D[x] is the
D
i
s
t
[
x
]
Dist[x]
Dist[x] under the case setting start-points
D
i
s
t
[
a
l
l
]
=
0
Dist[all] = 0
Dist[all]=0.
Cuz the Inequality-Group is unchanged,
x
i
+
w
≤
x
j
xi + w \leq xj
xi+w≤xj can also transformed to
x
j
−
w
≥
x
i
xj - w \geq xi
xj−w≥xi;
The rules are the same as previous, assuming
x
j
xj
xj is fixed, then the range of
x
i
xi
xi is
(
∞
,
x
j
−
w
]
(\infty, xj - w]
(∞,xj−w] (this differs from previous), now the intersection is
m
i
n
(
d
1
,
d
2
)
min(d1, d2)
min(d1,d2) for two ranges
(
∞
,
d
1
]
(\infty, d1]
(∞,d1] and
(
∞
,
d
2
]
(\infty, d2]
(∞,d2], so it corresponds to Least-Path.
That is, transfer all relations to the form x i + c ≥ x j xi + c \geq xj xi+c≥xj, set the start-points D i s t [ a l l ] = 0 Dist[all] = 0 Dist[all]=0, and then run Least-Path (If no Negative-Loop), we will get D i s t [ a l l ] ≤ 0 Dist[all] \leq 0 Dist[all]≤0; e.g., D i s t [ x ] = k Dist[x] = k Dist[x]=k means the maximal-available-value of x x x is k k k under the condition ∀ x i ≤ 0 \forall xi \leq 0 ∀xi≤0.
So, the maximal-value of x i xi xi under the condition x i ≤ d xi \leq d xi≤d equals D i s t [ x ] + d Dist[x] + d Dist[x]+d.
Moreover, there is a conclusion: D 1 [ x ] = − 1 ∗ D [ x ] D1[x] = -1 * D[x] D1[x]=−1∗D[x] where D [ x ] D[x] D[x] is the D i s t [ x ] Dist[x] Dist[x] (minimal-available-value) under the Greatest-Path of setting start-points D i s t [ a l l ] = 0 Dist[all] = 0 Dist[all]=0 with the form x i + w ≤ x j xi + w \leq xj xi+w≤xj, and D 1 [ x ] D1[x] D1[x] (maximal-available-value) under the Least-Path of setting start-points D i s t [ a l l ] = 0 Dist[all] = 0 Dist[all]=0 with the form x i + w ≥ x j xi + w \geq xj xi+w≥xj.
Therefore, the case of
≥
\geq
≥ could also always transferred to
≤
\leq
≤.
For instance, get the maximal-value under the condition
x
i
≤
d
xi \leq d
xi≤d;
Firstly, we transform all relations to the form
x
i
+
w
≤
x
j
xi + w \leq xj
xi+w≤xj, build a graph, set start-points
D
i
s
t
[
a
l
l
]
=
0
Dist[all] = 0
Dist[all]=0, run a Greatest-Path SPFA; finally,
D
i
s
t
[
x
]
Dist[x]
Dist[x] is the minimal-value of
x
x
x under the condition
∀
x
i
≥
0
\forall xi \geq 0
∀xi≥0
Secondly,
−
1
∗
D
i
s
t
[
x
]
-1 * Dist[x]
−1∗Dist[x] means the maximal-value of
x
x
x under the condition
∀
x
i
≤
0
\forall xi \leq 0
∀xi≤0;
Further,
−
1
∗
D
i
s
t
[
x
]
+
d
-1 * Dist[x] + d
−1∗Dist[x]+d is the maximal-value of
x
x
x under the condition
∀
x
i
≤
d
\forall xi \leq d
∀xi≤d.
If the condition is ∀ x i ≥ d \forall xi \geq d ∀xi≥d, it means that every number x i xi xi in the number-axis should as closely as possible to d d d from the right-side (i.e., x i ≥ d xi \geq d xi≥d) (the condition ∀ x i ≤ d \forall xi \leq d ∀xi≤d means from the left-side), so that the sum of ∣ x i − d ∣ |xi - d| ∣xi−d∣ attains the minimum among other solutions.
Suppose that there is a structure SPFA: Check-Positive-Loop with Start-Distance be
0
0
0 (that is, setting start-points
D
i
s
t
[
a
l
l
]
=
0
Dist[all] = 0
Dist[all]=0 and then run Greatest-Path SPFA), then all Inequality-Group problems can be solvable using this same structure.
In other words, if your device for Inequality-Group using a different SPFA which is not SPFA: Check-Positive-Loop with Start-Distance be
0
0
0, then, your device is wrong.
If there is relation x i = k xi = k xi=k, it can be also solvable using a trick by transfer it to x i = x j + c xi = xj + c xi=xj+c where c c c is also a constant, then for the relation x i = x j + c xi = xj + c xi=xj+c that is valid, it corresponds to two relations x i ≤ x j + c xi \leq xj + c xi≤xj+c and x i ≥ x j + c xi \geq xj + c xi≥xj+c which correspond to the standard relations x i − c ≤ x j xi - c \leq xj xi−c≤xj and x j + c ≤ x i xj + c \leq xi xj+c≤xi.
If you ensure that there always exists a point x j xj xj such that D i s t [ x j ] Dist[xj] Dist[xj] always equals to a constant c c c whatever the graph G (the premise is G is solvable), then x i = k = k + c − c = c + ( k − c ) = x j + ( k − c ) xi = k = k + c - c = c + (k - c) = xj + (k-c) xi=k=k+c−c=c+(k−c)=xj+(k−c), that is x i = x j + ( k − c ) xi = xj + (k-c) xi=xj+(k−c) where k , c k,c k,c are knowns, so it can transferred to x i − ( k − c ) ≤ x j xi - (k-c) \leq xj xi−(k−c)≤xj and x j + ( k − c ) ≤ x i xj + (k-c) \leq xi xj+(k−c)≤xi;
For example, if there is a Zero-Chain that involves all points
x
1
→
x
2
→
.
.
.
→
x
n
x1 \to x2 \to ... \to xn
x1→x2→...→xn with all edges be
0
0
0, there has a property from Greatest-Path: If G is solvable (no Positive-Loop), then
x
1
x1
x1 must be a real-start-point, that is
D
i
s
t
[
x
1
]
Dist[x1]
Dist[x1] always equals
0
0
0. So, we conclude that
D
i
s
t
[
x
1
]
=
0
Dist[x1] = 0
Dist[x1]=0 whatever the graph G is but contains a Zero-Chain and be solvable;
Therefore, a relation
x
i
=
c
xi = c
xi=c can be transformed to
x
i
=
c
+
0
=
x
1
+
c
xi = c + 0 = x1 + c
xi=c+0=x1+c, then
x
i
−
c
≤
x
1
xi -c \leq x1
xi−c≤x1 and
x
1
+
c
≤
x
i
x1 + c \leq xi
x1+c≤xi
If you get a inequality x l + . . . + x r R c xl + ... + xr \ R \ c xl+...+xr R c, we find that if we transform it to Prefix-Sum-Array, then it corresponds to S [ r ] − S [ l − 1 ] R c S[r] - S[l-1] \ R \ c S[r]−S[l−1] R c which satisfies the standard inequality form.
Note that, the index of
x
i
xi
xi should starts at
1
1
1 so that every
x
i
xi
xi corresponds to two Prefix-Sum. (in other words,
x
0...
x
n
−
1
x0...x_{n-1}
x0...xn−1$ should move abstractly to
x
1...
x
n
x1...xn
x1...xn, then we have a Prefix-Sum
S
0
,
.
.
.
,
S
n
S0, ..., Sn
S0,...,Sn, that is
S
1
−
S
0
=
x
0
S1 - S0 = x0
S1−S0=x0)
If you use
S
0
=
x
0
,
S
i
−
S
i
−
1
=
x
i
S0 = x0, Si - S_{i-1} = xi
S0=x0,Si−Si−1=xi, then for a relation
x
l
+
.
.
.
x
r
R
c
xl + ... xr \ R \ c
xl+...xr R c where if
l
=
0
l = 0
l=0 then it corresponds to a relation with one variable
S
r
R
c
Sr \ R \ c
Sr R c, and corresponds to a relation with two variables
S
r
−
S
l
−
1
R
c
Sr - S_{l-1} \ R \ c
Sr−Sl−1 R c if
l
>
0
l > 0
l>0;
This makes a bit of cumbersome cuz you need handle by cases, and moreover, a relation with just one relation
S
r
R
c
Sr \ R \ c
Sr R c is not valid.
There are two types inequalities: x i ≥ 1 ∀ x i xi \geq 1 \ \ \forall xi xi≥1 ∀xi and x i R x j xi \ R \ xj xi R xj where R R R is = , < , > , ≤ , ≥ =,<,>,\leq,\geq =,<,>,≤,≥ (it can transformed to x i + w ≤ x j xi + w \leq xj xi+w≤xj). Then calculate the minimal-sum of x i xi xi.
It is the standard Inequality-Group form, so after the algorithm, you will get the solution-set for the minimal value of x i ≥ 0 xi \geq 0 xi≥0, then perform x i = x i + 1 xi = xi + 1 xi=xi+1 to making sure x i ≥ 1 xi \geq 1 xi≥1;
A similar question link which says that the interval has o d d / e v e n odd/even odd/even number of 1 1 1 (Disjoint-Set with Depth can solve it)
Let
A
i
Ai
Ai denotes whether it is
1
1
1 in the position of
i
i
i where the index of
A
A
A starts at
1
1
1, then
A
i
=
0
/
1
Ai = 0/1
Ai=0/1.
We have the relation:
A
i
≥
0
Ai \geq 0
Ai≥0
A
i
≤
1
Ai \leq 1
Ai≤1
A
l
+
.
.
.
+
A
r
≥
c
Al + ... + Ar \geq c
Al+...+Ar≥c (not satisfy the standard-form)
When involving a sum of interval elements, it usually transferred to Prefix-Sum (there also has a trick that there must exists a empty element in Prefix-Sum S 0 S0 S0; in other words, n n n variables x 1 , . . . , x n x1, ..., xn x1,...,xn, correspond to n + 1 n + 1 n+1 variables S 0 , S 1 , . . . , S n S0, S1, ..., Sn S0,S1,...,Sn when transformed to Prefix-Sum; the reason why doing so is explained in link)
Now
S
i
−
S
i
−
1
≥
0
Si - S_{i-1} \geq 0
Si−Si−1≥0
S
i
−
S
i
−
1
≤
1
Si - S_{i-1} \leq 1
Si−Si−1≤1
S
r
−
S
l
−
1
≥
c
Sr - S_{l-1} \geq c
Sr−Sl−1≥c
which is a standard Inequality-Group.
The answer (the sum of A 1 , . . . , A n A1, ..., An A1,...,An) equals S n − S 0 Sn - S0 Sn−S0.
More speaking, when handling with Prefix-Sum, usually it will exists a Zero-Chain, as here S 0 → S 1... → S n S0 \to S1 ... \to Sn S0→S1...→Sn where every edge is 0 0 0, it means that if it is consistent, then S 0 S0 S0 must be a real-start-point, that is, D i s t [ S 0 ] Dist[S0] Dist[S0] must be 0 0 0 in any cases.
n
n
n points on a line from left to right
x
1
≤
.
.
.
≤
x
n
x1 \leq ... \leq xn
x1≤...≤xn
It has the relations:
x
i
≤
x
i
+
1
∀
i
∈
[
1
,
n
)
xi \leq x_{i+1} \quad \forall i \in [1, n)
xi≤xi+1∀i∈[1,n)
x
j
−
x
i
≤
c
xj - xi \leq c
xj−xi≤c where
j
>
i
j > i
j>i
x
j
−
x
i
≥
c
xj - xi \geq c
xj−xi≥c where
j
>
i
j > i
j>i
It already could be transformed to the standard-form (where has no condition of the
x
i
≤
o
r
≥
d
∀
x
i
xi \leq or \geq d \quad \forall xi
xi≤or≥d∀xi which is a optional restriction);
After the algorithm, if it returns False that is it is inconsistent; otherwise we got the minimal value D i s t [ x ] Dist[x] Dist[x] for every x x x such that x i ≥ 0 xi \geq 0 xi≥0.
How should we calculate the maximal-distance between
x
1
,
x
n
x1, xn
x1,xn?
After the algorithm, we get
D
i
s
t
[
x
1
]
=
0
Dist[x1] = 0
Dist[x1]=0 and
D
i
s
t
[
x
1
]
≤
D
i
s
t
[
x
2
]
≤
.
.
.
≤
D
i
s
t
[
x
n
]
Dist[x1] \leq Dist[x2] \leq ... \leq Dist[xn]
Dist[x1]≤Dist[x2]≤...≤Dist[xn] (due to the Zero-Chain
x
1
→
x
2
→
.
.
.
→
x
n
x1 \to x2 \to ... \to xn
x1→x2→...→xn), as we discussed in the article above, for this current solution-set
a
1
,
a
2
,
.
.
.
,
a
n
a1, a2, ..., an
a1,a2,...,an where
a
i
=
D
i
s
t
[
x
i
]
ai = Dist[xi]
ai=Dist[xi], for any
x
i
xi
xi, there are two cases: 1
maybe the solution-set
a
1
,
.
.
.
[
a
i
,
∞
)
,
.
.
.
,
a
n
a1, ... [ai, \infty), ..., an
a1,...[ai,∞),...,an is also valid under the condition of without modify
a
1
a1
a1; 2
maybe just a few of solution-sets
a
1
,
.
.
.
[
a
i
,
a
i
+
k
]
,
.
.
.
,
a
n
a1, ... [ai, ai + k], ..., an
a1,...[ai,ai+k],...,an are valid under the condition of without modify
a
1
a1
a1.
1
e.g.,
n
=
3
n=3
n=3 with
x
1
≤
x
2
≤
x
3
x1 \leq x2 \leq x3
x1≤x2≤x3 and
x
2
≥
x
3
x2 \geq x3
x2≥x3, we also got the solution-set
(
0
,
0
,
0
)
(0,0,0)
(0,0,0), we find that
0
,
x
,
x
0, x, x
0,x,x where
x
∈
[
0
,
∞
)
x \in [0, \infty)
x∈[0,∞) is also a solution.
2
e.g.,
n
=
3
n=3
n=3 with
x
1
≤
x
2
≤
x
3
x1 \leq x2 \leq x3
x1≤x2≤x3 and
x
2
≥
x
3
,
x
1
≥
x
2
x2 \geq x3, x1 \geq x2
x2≥x3,x1≥x2, the solution-set is
(
0
,
0
,
0
)
(0,0,0)
(0,0,0), however, the range of
x
n
xn
xn could only be
[
0
,
0
]
[0, 0]
[0,0] not infinite (if
x
3
=
1
x3 = 1
x3=1, then it must will affect
x
1
x1
x1)
3
e.g.,
n
=
3
n=3
n=3 with
x
1
≤
x
2
≤
x
3
x1 \leq x2 \leq x3
x1≤x2≤x3 and
x
2
≥
x
3
,
x
1
≤
x
2
,
x
2
−
2
≤
x
1
x2 \geq x3, x1 \leq x2, x2 - 2 \leq x1
x2≥x3,x1≤x2,x2−2≤x1, the solution-set is
(
0
,
0
,
0
)
(0,0, 0)
(0,0,0), but the solution-set
(
0
,
1
,
1
)
(0,1,1)
(0,1,1)
(
0
,
2
,
2
)
(0,2,2)
(0,2,2) are also solution-sets without modify
x
1
x1
x1.
This is, according to D i s t [ ] Dist[] Dist[], we could not check that whether the maximal-distance between x 1 , x n x1, xn x1,xn is infinite or not.
Conclusion: The maximal-distance between
x
1
,
x
n
x1, xn
x1,xn is infinite then there would not exists a path
x
n
→
.
.
.
→
x
1
xn \to ... \to x1
xn→...→x1; if exists such path, then the Greatest-Path
D
i
s
t
[
x
1
]
Dist[x1]
Dist[x1] with
x
n
xn
xn be start-point indicates the maximal-distance.
Proof: this path means a relation
x
n
+
w
≤
x
1
xn + w \leq x1
xn+w≤x1 and we already have a relation
x
1
≤
x
n
x1 \leq xn
x1≤xn, therefore,
x
n
−
x
1
≥
0
xn - x1 \geq 0
xn−x1≥0 and
x
n
−
x
1
≤
−
w
xn - x1 \leq -w
xn−x1≤−w we found that the distance
x
n
−
x
1
xn - x1
xn−x1 has confined in
−
w
-w
−w (moreover,
−
w
-w
−w should
≥
0
\geq 0
≥0, otherwise it causes a contradiction which means inconsistent)
The Maximal-distance between
x
1
,
x
n
x1, xn
x1,xn is that, we set
D
i
s
t
[
x
n
]
=
0
Dist[xn] = 0
Dist[xn]=0 as the only start-point, then run Greatest-Path, finally
D
i
s
t
[
0
]
∗
−
1
Dist[0] * -1
Dist[0]∗−1 is the answer.
Proof: cuz the graph G is unchanged, that is, a edge
a
→
b
a \to b
a→b with
w
w
w always denotes a relation
a
+
w
≤
b
a + w \leq b
a+w≤b (that is no matter to whether the start-points is
[
a
l
l
]
=
0
[all] = 0
[all]=0 or
[
x
n
]
=
0
[xn]=0
[xn]=0); e.g., there are two paths
d
1
≤
x
i
d1 \leq xi
d1≤xi and
d
2
≤
x
i
d2 \leq xi
d2≤xi then their intersection is
m
a
x
(
d
1
,
d
2
)
max(d1, d2)
max(d1,d2); in other words, the properties satisfies as previous are also valid when the start-point is just
x
n
xn
xn; so, it is still Greatest-Path.
Cuz the Zero-Chain (suppose that
x
1
x1
x1 is accessible for
x
n
xn
xn), so we still the property
D
i
s
t
[
x
1
]
≤
.
.
.
≤
D
i
s
t
[
x
n
]
Dist[x1] \leq ... \leq Dist[xn]
Dist[x1]≤...≤Dist[xn], and
x
n
xn
xn must be a real-start-point due to it it the only start-point and G is solvable, that is
D
i
s
t
[
x
n
]
Dist[xn]
Dist[xn] always equals
0
0
0;
Any path
x
n
→
.
.
.
→
x
1
xn \to ... \to x1
xn→...→x1 actually means a relation
x
n
+
w
≤
x
1
xn + w \leq x1
xn+w≤x1 (i.e.,
x
n
−
x
1
≤
−
w
xn - x1 \leq -w
xn−x1≤−w, where
w
≤
0
w \leq 0
w≤0 proved above) which has already shows that the maximum of
x
n
−
x
1
xn - x1
xn−x1 is
w
w
w; but the distance of
x
1
,
x
n
x1, xn
x1,xn that is
∣
x
1
−
x
n
∣
=
m
a
x
(
x
n
−
x
1
,
x
1
−
x
n
)
|x1 - xn| = max( xn - x1, x1 - xn)
∣x1−xn∣=max(xn−x1,x1−xn), now we got
x
n
−
x
1
xn - x1
xn−x1; we need consider
x
1
−
x
n
x1 - xn
x1−xn; this should be linked to the previous information
x
1
≤
x
n
x1 \leq xn
x1≤xn, so we have
x
1
−
x
n
≤
0
x1 - xn \leq 0
x1−xn≤0; therefore
∣
x
1
−
x
n
∣
=
x
n
−
x
1
|x1 - xn| = xn - x1
∣x1−xn∣=xn−x1;
One path means the
x
n
−
x
1
≤
−
w
xn - x1 \leq -w
xn−x1≤−w (the maximum-distance is
−
w
-w
−w), but there maybe multiple paths, that is we have
x
n
−
x
1
≤
−
w
1
xn - x1 \leq -w1
xn−x1≤−w1,
x
n
−
x
1
≤
−
w
2
xn - x1 \leq -w2
xn−x1≤−w2,
.
.
.
...
..., (it also represents
x
1
≥
w
1
,
x
1
≥
w
2
,
.
.
.
x1 \geq w1, x1 \geq w2, ...
x1≥w1,x1≥w2,...), according to the intersection
x
1
≥
m
a
x
(
w
1
,
w
2
,
.
.
.
)
x1 \geq max(w1, w2, ...)
x1≥max(w1,w2,...), and also the Greatest-Path would get
m
a
x
(
w
1
,
w
2
,
.
.
.
)
max(w1, w2, ...)
max(w1,w2,...) finally; so finally
D
i
s
t
[
x
1
]
=
k
Dist[x1] = k
Dist[x1]=k means that the relations between
x
1
,
x
n
x1, xn
x1,xn must satisfies
x
n
−
x
1
≤
−
k
xn - x1 \leq -k
xn−x1≤−k (that is, if
x
n
xn
xn is fixed at
0
0
0, then the longest position which can be chosen by
x
1
x1
x1 is
−
k
-k
−k);
One difference from the previous is that, in the previous D i s t [ x ] = m a x ( 0 , a l l p a t h s ) Dist[x] = max( 0, all \ paths) Dist[x]=max(0,all paths) due to every points is a start-point; but in here D i s t [ x ] = m a x ( a l l p a t h s ) Dist[x] = max( all \ paths) Dist[x]=max(all paths) due to there is only one start-point
Minimize_InequalitiesSystem< int, int> check( &G);
check.Initialize();
auto ret = check.Work();
if( false == ret){ //< inconsistent
cout<< -1;
return;
}
check.Spfa(); //< set `Dist[xn]=0` with others be `invalid`, run Grestest-Path
int invalid;
memset( &invalid, -0x7F, sizeof( invalid));
if( spfa.Distance[ 0] == invalid){ //< not accessible
cout<< -2;
return;
}
cout<< -1 * spfa.Distance[ 0];
There are some terms:
we divide one-day into
24
24
24 hour-interval,
[
0
,
1
]
,
[
1
,
2
]
,
.
.
.
,
[
23
,
0
]
[0,1], [1,2], ..., [23,0]
[0,1],[1,2],...,[23,0]; a staff has a start-work-time
x
x
x, means this staff would work with
8
8
8 hour-intervals
[
x
,
x
+
1
]
,
[
x
+
1
,
x
+
2
]
,
.
.
.
,
[
x
+
7
,
x
+
8
]
[x,x+1], [x+1,x+2], ..., [x+7, x+8]
[x,x+1],[x+1,x+2],...,[x+7,x+8];
M
i
[
x
]
Mi[x]
Mi[x] denotes there must have at least
M
i
[
x
]
Mi[x]
Mi[x] staffs at the time
[
x
,
x
+
1
]
[x,x+1]
[x,x+1]
M
a
[
x
]
Ma[x]
Ma[x] denotes there are
M
a
[
x
]
Ma[x]
Ma[x] staffs whose start-work-time is
x
x
x (that is, you can choose at most
M
a
[
x
]
Ma[x]
Ma[x] at this time)
Let’s see a wrong device using Greedy.
Let
A
[
x
]
A[x]
A[x] be the total staff-count at the time
[
x
,
x
+
1
]
[x, x+1]
[x,x+1], then we have
A
[
x
]
≥
M
i
[
x
]
A[x] \geq Mi[x]
A[x]≥Mi[x], these
A
[
x
]
A[x]
A[x] staffs consists of
a
a
a staffs whose start-work-time is
x
x
x and
b
b
b staffs whose start-work-time is
<
x
< x
<x.
If there exists a point
x
x
x such that
b
=
0
b = 0
b=0 (i.e.,
A
[
x
]
=
a
A[x] = a
A[x]=a) and
A
[
x
]
=
M
i
[
x
]
A[x] = Mi[x]
A[x]=Mi[x], let’s stop this idea cuz if we thinking rigorously you will find this is impossible, due to this problem is a Loop not linear, so there would not exists a Start-Point.
Suppose we choose the staffs be
0
,
7
,
14
,
21
0, 7, 14, 21
0,7,14,21, then
A
[
]
=
[
2
,
2
,
2
,
2
,
2
,
1
,
1
,
2
,
1
,
1
,
1
,
1
,
1
,
1
,
2
,
1
,
1
,
1
,
.
.
.
]
A[] = [2,2,2,2,2,1,1,2,1,1,1, 1,1,1,2,1,1,1,...]
A[]=[2,2,2,2,2,1,1,2,1,1,1,1,1,1,2,1,1,1,...] and we let
M
i
[
]
=
A
[
]
,
M
a
[
]
>
A
[
]
Mi[] = A[], Ma[] > A[]
Mi[]=A[],Ma[]>A[], we found that there is not a point
x
x
x such that
A
[
x
]
A[x]
A[x] equals the staff-chosen-count whose start-time is
x
x
x; (
A
[
0
/
7
/
14
/
21
]
=
2
A[0/7/14/21] = 2
A[0/7/14/21]=2 while the staff-chosen-count at that time is
1
1
1; other
A
[
]
>
=
1
A[] >= 1
A[]>=1 while the staff-chosen-count is
0
0
0)
Let’s see a wrong device using Inequality.
If we define
A
[
x
]
A[x]
A[x] be the total staff-count at the time
[
x
,
x
+
1
]
[x, x+1]
[x,x+1] (e.g., if there is a staff whose start-work-time is
a
a
a, then it will cause
A
[
a
]
,
.
.
.
,
A
[
a
+
7
]
A[a], ..., A[a+7]
A[a],...,A[a+7] added by
1
1
1.
Then we would have the next inequalities:
A
[
x
]
≤
0
A[x] \leq 0
A[x]≤0,
A
[
x
]
≥
M
i
[
x
]
A[x] \geq Mi[x]
A[x]≥Mi[x],
A
[
x
]
≤
M
a
[
x
]
A[x] \leq Ma[x]
A[x]≤Ma[x]
It is very important to realize that the relation
A
[
x
]
≤
M
a
[
x
]
A[x] \leq Ma[x]
A[x]≤Ma[x] is wrong, cuz
A
[
x
]
A[x]
A[x] is the working-staff-count at time
x
x
x, while
M
a
[
x
]
Ma[x]
Ma[x] is the maximal-staff-count whose start-work-time is
x
x
x. (e.g.,
M
a
[
i
]
=
1
,
M
a
[
i
+
1
]
=
0
Ma[i] = 1, Ma[i+1]=0
Ma[i]=1,Ma[i+1]=0, we choose a staff at
i
i
i, but we have
A
[
i
]
,
A
[
i
+
1
]
,
.
.
.
,
A
[
i
+
7
]
=
1
A[i],A[i+1], ..., A[i+7] = 1
A[i],A[i+1],...,A[i+7]=1, therefore
A
[
i
+
1
]
>
M
a
[
i
+
1
]
A[i+1] > Ma[i+1]
A[i+1]>Ma[i+1]. In fact, this is reasonable)
Actually, it should be corrected by
A
[
x
]
≤
M
a
[
x
]
+
M
a
[
x
−
1
]
+
.
.
.
+
M
a
[
x
−
7
]
A[x] \leq Ma[x] + Ma[x-1] + ... + Ma[x-7]
A[x]≤Ma[x]+Ma[x−1]+...+Ma[x−7]; it still is complicated due to the definition of
A
[
x
]
A[x]
A[x]; suppose that now we got all
A
[
0
,
1
,
2...
,
23
]
A[0,1,2...,23]
A[0,1,2...,23], we still don’t know how many staffs chosen which is the answer asked. It is important to associate your algorithm to the answer that problem-asked.
Cuz the answer that question asked is the total-staff-chosen-count, so we define
A
[
x
]
A[x]
A[x] be the staff-chosen-count whose start-work-time is
x
x
x; this definition is very vital, then the answer is clear that equals the sum
A
[
]
A[]
A[].
Then we have:
A
[
x
]
≥
0
∀
x
∈
[
0
,
23
]
A[x] \geq 0 \quad \forall x \in [0, 23]
A[x]≥0∀x∈[0,23]
A
[
x
]
≤
M
a
[
x
]
A[x] \leq Ma[x]
A[x]≤Ma[x]
∑
i
=
x
−
7
x
A
[
i
]
≥
M
i
[
x
]
\sum_{i = x - 7}^{x} A[i] \geq Mi[x]
∑i=x−7xA[i]≥Mi[x] (note here, only the staffs whose start-time is
x
−
7
,
.
.
.
,
x
x-7, ..., x
x−7,...,x would cover the current-time
[
x
,
x
+
1
]
[x, x+1]
[x,x+1])
Cuz involves the summation, so we need transform to Prefix-Sum; (as we mentioned above, Prefix-Sum must has a empty-element
S
[
0
]
S[0]
S[0]; so
A
[
0
−
23
]
→
S
[
0
−
24
]
A[0-23] \to S[0-24]
A[0−23]→S[0−24])
Let
A
[
x
]
=
S
[
x
+
1
]
−
S
[
x
]
A[x] = S[x + 1] - S[ x]
A[x]=S[x+1]−S[x] (note that,
S
[
x
]
S[x]
S[x] is Linear that is it equals the sum
A
[
0
]
+
.
.
.
+
A
[
x
−
1
]
A[0]+...+A[x-1]
A[0]+...+A[x−1]; although this problem is not Linear but Loop)
So we have
S
[
x
+
1
]
−
S
[
x
]
≥
0
S[x+1] - S[x] \geq 0
S[x+1]−S[x]≥0
S
[
x
+
1
]
−
S
[
x
]
≤
M
a
[
x
]
S[x+1] - S[x] \leq Ma[x]
S[x+1]−S[x]≤Ma[x]
@
T
a
g
0
@Tag0
@Tag0
@Tag0 is very vital, cuz A [ ] A[] A[] is a Loop, for the sum ∑ i = x − 7 x A [ i ] \sum_{i = x - 7}^{x} A[i] ∑i=x−7xA[i], when x < 7 x < 7 x<7 it will involves A [ 23 ] , A [ 22 ] . . . A[23], A[22]... A[23],A[22]...; while the Prefix-Sum S [ ] S[] S[] is Linear that is its Start-Point is A [ 0 ] A[0] A[0]; so the transformation here is vital;
So, it becomes:
S
[
x
+
1
]
−
S
[
x
]
≥
0
S[x+1] - S[x] \geq 0
S[x+1]−S[x]≥0
S
[
x
+
1
]
−
S
[
x
]
≤
M
a
[
x
]
S[x+1] - S[x] \leq Ma[x]
S[x+1]−S[x]≤Ma[x]
S
[
x
+
1
]
−
S
[
x
−
7
]
≥
M
i
[
x
]
i
f
x
≥
7
S[x+1] - S[x - 7] \geq Mi[x] \quad if \ x \geq 7
S[x+1]−S[x−7]≥Mi[x]if x≥7
S
[
x
+
1
]
+
(
S
[
24
]
−
S
[
17
+
x
]
)
≥
M
i
[
x
]
i
f
x
<
7
S[x+1] + (S[24] - S[17 + x]) \geq Mi[x] \quad if \ x < 7
S[x+1]+(S[24]−S[17+x])≥Mi[x]if x<7
The former
3
3
3 relations fit the standard-form
x
i
+
c
≤
x
j
xi + c \leq xj
xi+c≤xj, while there exists
3
3
3 unknowns
S
[
x
+
1
]
,
S
[
24
]
,
S
[
17
+
x
]
S[x+1], S[24], S[17+x]
S[x+1],S[24],S[17+x] in the last relation, we need to eliminate it to
2
2
2 unknowns
Cuz for all inequalities with the last-form, they share the same unknown
S
[
24
]
S[24]
S[24].
So we can iterate it to make it a constant (that is, we try multiple Inequality-Systems), suppose S [ 24 ] = c S[24] = c S[24]=c, now although that inequality fit the standard-form, it is very important that now you have a new relation S [ 24 ] = c S[24] = c S[24]=c.
According to the trick mentioned above, cuz there is a Zero-Chain S 0 → . . . → S 24 S0 \to ... \to S24 S0→...→S24, so it is solvable, then S 0 S0 S0 must be 0 0 0, so S 24 = c = c + 0 = c + S 0 S24 = c = c + 0 = c + S0 S24=c=c+0=c+S0, then S 24 ≤ c + s 0 S24 \leq c + s0 S24≤c+s0, S 24 ≥ c + S 0 S24 \geq c + S0 S24≥c+S0;
We find that S 24 = c S24 = c S24=c denotes the answer ( S 24 S24 S24 is the sum of all A [ ] A[] A[]), and cuz the answer is the least number; so c c c satisfies Binary-Search.
bool Check( int _mid){ for( int i = 0; i < 7; ++i){ G->Modify_edge( Edges_0.at( i), Mi[ i] - _mid); } G->Modify_edge( Edges_1.at( 0), _mid); G->Modify_edge( Edges_1.at( 1), -1 * _mid); if( false == Spfa->Work()){ return true; } return false; } //---- for( int i = 0; i < 24; ++i){ //> A[ i] >= 0 `->` Sum[ i + 1] - S[ i] >= 0 G->Add_edge( i, i + 1, 0); } for( int i = 0; i < 24; ++i){ //> A[ i] <= Ma[ i] `->` Sum[ i + 1] - S[ i] <= Ma[ i] G->Add_edge( i + 1, i, -1 * Ma[ i]); } for( int i = 7; i < 24; ++i){ //> A[ i - 7] + ... + A[ i] >= Mi[ i] `->` S[ i + 1] - S[ i - 7] >= Mi[ i] G->Add_edge( i - 7, i + 1, Mi[ i]); } Edges_0.clear(); for( int i = 0; i < 7; ++i){ //> A[ 23 - ?] + ... + A[ 23] + A[ 0] + ... + A[ i] >= Mi[ i] `->` S[ i + 1] + S[ 24] - S[ 17 + i] >= Mi[ i] G->Add_edge( 17 + i, i + 1, 0); //< `0` is just a placeholder Edges_0.push_back( G->Get_last_edgeId()); } Edges_1.clear(); //> S[ 24] = C `->` S[ 24] >= S[ 0] + C, S[ 24] <= S[ 0] + C G->Add_edge( 0, 24, 0); Edges_1.push_back( G->Get_last_edgeId()); G->Add_edge( 24, 0, 0); Edges_1.push_back( G->Get_last_edgeId()); //-- Spfa = new SPFA_CheckLoop< int, int>( G); Spfa->Initialize(); int l = 0, r = n; while( l < r){ int mid = (l + r) >> 1; if( Check( mid)){ r = mid; } else{ l = mid + 1; } } if( false == Check( r)){ cout<< "No Solution" <<endl; return; } cout<< r <<endl;
0, 1, 2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。