1. 程式人生 > >ZOJ 2770 差分約束+SPFA

ZOJ 2770 差分約束+SPFA

最大值 multiple sample mea tro output problem igp data

Burn the Linked Camp

Time Limit: 2 Seconds Memory Limit: 65536 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
題意:給出n個點表示n個軍營,c[i]表示第i個軍營可容納的士兵的最大值,接著給出m條邊(i,j,k)表示從第i到第j個軍營最少有的的士兵數。求在滿足以上條件下最少有多少士兵!
我們不妨設S(i)表示從第一個兵營到第i個兵營最少的士兵數,保存在d[i]中
接著就是找出所有的不等式組。
1.(i,j,k) --> S(j)-S(i-1)>=k 即S(i-1)-S(j)<=-k
2.S(j)-S(i-1)<=c=d[j]-d[i-1];
3.設A(i)表示每個軍營的實際人數,顯然 0<=A(i)<=c[i]
即 S(i)-S(i-1)>=0&&S(i)-S(i-1)<=c[i];
接著將不等式轉化為邊存入圖中
我們令 S(u)<=S(v)+w 表示連接一條從v到u且權值為w的有向邊.

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
inline int read()
{
int s=0,f=1; char ch=getchar();
while(ch<‘0‘||ch>‘9‘) {if(ch==‘-‘) f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘) {s=s*10+ch-‘0‘;ch=getchar();}
return s*f;
}
struct Edge
{
int to;
int w;
int next;
}edges[50005];
int cnt,dis[10005];
int first[10005];
int n,m;
bool vis[10005];
int counts[10005];
int c[10005];
void add(int a,int b,int c)
{
//cout<<a<<" "<<b<<" "<<c<<endl;
edges[cnt].to=b;
edges[cnt].w=c;
edges[cnt].next=first[a];
first[a]=cnt++;
}
bool spfa(int st,int ed)
{
queue<int>Q;
dis[st]=0;
vis[st]=1;
counts[st]++;
Q.push(st);
while(!Q.empty()){
int u=Q.front();Q.pop();
vis[u]=0;
if(counts[u]>n) return false;
for(int i=first[u];i+1;i=edges[i].next){
int v=edges[i].to;
if(dis[v]>dis[u]+edges[i].w){
dis[v]=dis[u]+edges[i].w;
if(!vis[v]) {Q.push(v);vis[v]=1;counts[v]++;}
}
}
}
//for(int i=1;i<=n;++i) cout<<dis[i]<<" ";cout<<endl;
cout<<-dis[0]<<endl;
return true;
}
int main()
{
int t,i,j;
//cin>>t;
while(cin>>n>>m){
memset(first,-1,sizeof(first));
memset(vis,0,sizeof(vis));
memset(dis,inf,sizeof(dis));
memset(counts,0,sizeof(counts));
cnt=0;c[0]=0;

for(i=1;i<=n;++i) {c[i]=read();
add(i,i-1,0);
add(i-1,i,c[i]);
c[i]+=c[i-1];
}
int u,v,w;
for(i=1;i<=m;++i)
{
u=read(),v=read(),w=read();
add(v,u-1,-w);
add(u-1,v,c[v]-c[u-1]);
}

if(!spfa(n,0)) puts("Bad Estimations");
}
return 0;
}

ZOJ 2770 差分約束+SPFA