Burn the Linked Camp zoj 差分约束

Burn the Linked Camp

Time Limit: 1 Second      Memory Limit: 32768 KB

It is well known that, in the period of The Three Empires, Liu Bei, the emperor of the Shu Empire, was defeated by Lu Xun, a general of the Wu Empire. The defeat was due to Liu Bei's wrong decision that he divided his large troops into a number of camps, each of which had a group of armies, and located them in a line. This was the so-called "Linked Camps".

Let's go back to that time. Lu Xun had sent many scouts to obtain the information about his enemy. From his scouts, he knew that Liu Bei had divided his troops into n camps, all of which located in a line, labeled by 1..n from left to right. The ith camp had a maximum capacity of Ci soldiers. Furthermore, by observing the activities Liu Bei's troops had been doing those days, Lu Xun could estimate the least total number of soldiers that were lived in from the ith to the jth camp. Finally, Lu Xun must estimate at least how many soldiers did Liu Bei had, so that he could decide how many troops he should send to burn Liu Bei's Linked Camps.

Input:

There are multiple test cases! On the first line of each test case, there are two integers n (0<n<=1,000) and m (0<=m<=10,000). On the second line, there are n integers C1��Cn. Then m lines follow, each line has three integers i, j, k (0<i<=j<=n, 0<=k<2^31), meaning that the total number of soldiers from the ith camp to the jth camp is at least k.

Output:

For each test case, output one integer in a single line: the least number of all soldiers in Liu Bei's army from Lu Xun's observation. However, Lu Xun's estimations given in the input data may be very unprecise. If his estimations cannot be true, output "Bad Estimations" in a single line instead.

Sample Input:

3 2
1000 2000 1000
1 2 1100
2 3 1300
3 1
100 200 300
2 3 600

Sample Output:

1300
Bad Estimations

差分约束, 就是解一系列不等式方程组,如
x2 - x1 < 3;
x3 - x2 < -2;
x3 - x4 > 4;
x4 - x5 < 8;
x6 - x3 > 4;
..............
数论中的问题。。我们可以联想到最短路算法 ,,bellman-ford算法的三角不等式。。
d[j] < d[u] + map[u][j]
d[j] > d[u] + map[u][j]
所以可以通过建模将一个数论问题化为图论问题。。再构造一个起点 v0,它到图中各顶点的距离都为0


1,...n.把它看作图中的一个顶点。
x1..xi...xn..看作dis[i]...
建好图。。然后用bellman-ford算法。。或spfa 算法求出dis[i]..
的值
dis[0],dis[1].....,dis[n]..就是我们所要求的xi的解。

现在言归正传, 本题就是一个差分约速问题。
关键是如何建图。。
刚开始dis[i]保存每个营地的人数。。
怎么建也建不出图。
后来用sum[i]保存从第一个营地到第i个营地的总的人数。
则建图很方便
。。
根据题意可得。。
sum[n] = x[0] + x[1] + x[2] +.. x[n]

sum[i] - sum[i -1] < c[i];
sum[i] -sum[i -1] > 0;
sum [j ] - sum[i -1] > k;
还有要注意的是题目所求的是最小的人数。。
所以三角不等式应为d[u] < d[u] + map[u][i];
#include <stdio.h>
#include
<string.h>
#include
<stdlib.h>
#include
<vector>
#include
<deque>

using namespace std;

const int inf = 0x7f7f7f7f;

struct node
{
int x, v;

}Nd;

vector
<node>T[11010];
deque
<int>q;

int N, M;

bool flag[11010] = {false};
int sum[11020];
int hash[11020];

int spfa( )
{
int t;
vector
<node>::iterator iter;
q.push_back(
0);
sum[
0] = 0;
flag[
0] = 1;
while ( !q.empty( ) )
{
t
= q.front( );
flag[t]
= 0;
q.pop_front( );
for ( iter = T[t].begin( ); iter != T[t].end( ); iter++) {
if ( sum[iter->x] < sum[t] + iter->v ) {
sum[iter
->x] = sum[t] + iter->v;
if ( !flag[iter->x] )
{
flag[iter
->x] = 1;
q.push_back(iter
->x);

}
if ( ++hash[iter->x] > N)
return 0;

}
}
}

return 1;
}


void init( )
{
int i;
q.clear();
for (i = 0; i <= 1000; i++)
T[i].clear(), flag[i]
= 0;
memset(sum,
0, sizeof(sum));
memset(hash,
0, sizeof(hash));
for ( i = 1; i <= 1000; i++) {
sum[i]
= -inf;
Nd.x
= i; Nd.v = 0;
T[
0].push_back(Nd);
}

}

void print( int x)
{
int i;
for (i = 0; i <= x; i++)
printf(
"%d ",sum[i]);
puts(
"");
}

int main( )
{
int i, j, a, b, c, d;
while (scanf("%d%d",&N, &M) != EOF)
{
init( );
for (i = 1; i <= N; i++) {
scanf(
"%d", &d);
Nd.x
= i, Nd.v = 0;
T[i
-1].push_back(Nd);
Nd.x
= i - 1, Nd.v = -d;
T[i].push_back(Nd);
}
for (i = 0; i < M; i++) {
scanf(
"%d%d%d",&a, &b, &c);
Nd.x
= b; Nd.v = c;
T[a
- 1].push_back(Nd);
}
if ( spfa( ) )
printf(
"%d\n",sum[N]);
else
printf(
"Bad Estimations\n");
//print(N);
}
return 0;
}




posted on 2011-08-05 15:54  more think, more gains  阅读(259)  评论(0编辑  收藏  举报

导航