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




转载于:https://www.cnblogs.com/tangcong/archive/2011/08/05/2128728.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值