ZOJ 2770 差分约束+SPFA

本文深入探讨了经典的火烧连营问题,通过建立数学模型和使用SPFA算法解决了一个历史军事策略问题。文章详细介绍了如何利用图论和最短路径算法来估算刘备用兵的最小兵力配置,同时考虑了军营容量限制和敌方情报的约束。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/zzqc/p/6804493.html

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值