您的位置:首页 > 其它

ZOJ 2770 Burn the Linked Camp 差分约束

2015-08-10 16:44 323 查看
Burn the Linked Camp
Time Limit:2000MS Memory Limit:65536KB 64bit
IO Format:
%lld & %llu

Description

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


///差分约束
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>

using namespace std;

#define INF 0x3f3f3f3f
#define maxn 20005
int n,m;
int fir[maxn],u[maxn],v[maxn],w[maxn],nex[maxn];
int e_max;

void init()
{
for(int i=0;i<=2*n;i++)
fir[i]=-1;
e_max=0;
}

void add_edge(int s,int t,int c)
{
int e=e_max++;
u[e]=s;
v[e]=t;
w[e]=c;
nex[e]=fir[s];
fir[s]=e;
}

int que[2*maxn];
int spfa()
{
int d[maxn];
int vis[maxn];
int c[maxn];
memset(d,0x3f,sizeof d);
memset(vis,0,sizeof vis);
memset(c,0,sizeof c);
int f=0,r=-1;

que[++r]=n;
d
=0;
c
=1;
while(f<=r)
{
int k=que[f++];
vis[k]=0;
c[k]++;
if(c[k]>n) return -1;
for(int i=fir[k];~i;i=nex[i])
{
int e=v[i];
if(w[i]+d[k]<d[e])
{
d[e]=w[i]+d[k];
if(!vis[e])
{
vis[e]=1;
que[++r]=e;
}
}
}
}
return d
-d[0];
}

int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=1;i<=n;i++)
{
int c;
scanf("%d",&c);
add_edge(i-1,i,c);
add_edge(i,i-1,0);
}

for(int i=0;i<m;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add_edge(b,a-1,-c);
}
int flag=spfa();
if(flag==-1) printf("Bad Estimations\n");
else printf("%d\n",flag);
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: