差分约束
由n个变量和m个他们之间关系的不等式组成的有约束条件的问题。(废话hiahiahia
往往可以转换成图论中单源最短路或最长路问题来进行求解,约束条件与其松弛操作类似。
·形如d[j]-d[i]>=a[i,j],可以转化为要满足d[j]>=d[i]+a[i,j],当d[j]<d[i]+a[i,j]时,为满足条件需要进行松弛操作,d[j]=d[i]+a[i,j],这样就转换成了求最长路问题;
·形如d[j]-d[i]<=a[i,j],可以转化为要满足d[j]<=d[i]+a[i,j],同理,就转换成了求最短路问题;
1)在求解时,同样要注意负环问题:
存在负环,SPFA算法中,体现为某一结点入队次数大于总结点数。
2)终点不可达时还是inf;
3)“隐形”条件:
根据题意,相邻点之间常常会存在约束条件,例如d[j]-d[i]>=0,d[j]-d[i]<=a[j]之类的,后者可以转换为,d[j]-a[j]<=d[i],也就意味着,路上权值有可能为负,所以还要注意此时d[i]初始化为-inf。
HDU1384
Intervals
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4845 Accepted Submission(s): 1818
Problem Description
You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
> reads the number of intervals, their endpoints and integers c1, ..., cn from the standard input,
> computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i = 1, 2, ..., n,
> writes the answer to the standard output
Input
The first line of the input contains an integer n (1 <= n <= 50 000) - the number of intervals. The following n lines describe the intervals. The i+1-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50 000 and 1 <= ci <= bi - ai + 1.
Process to the end of file.
Output
The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i = 1, 2, ..., n.
Sample Input
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
Sample Output
6
根据题意建图,邻接表用vector储存(btw,多组输入一定记得初始化清空vector!!!),单独写一个函数清晰明了。
3个条件:
1)d[j]-d[i]>=w;--->d[j]>=d[i]+w;
2)d[i]-d[i-1]>=0;--->d[i]>=d[i-1]+0;
3)d[i]-d[i-1]<=a[i];--->d[i-1]>=d[i]+(-a[i]);
spfa即可。
//读不懂题考百度题意很尴尬(google翻译了也读不懂题意,是我语文太差嘤嘤嘤
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=50000;
const int inf=0x3f3f3f3f;
int n;
int d[maxn+10],vst[maxn+10];
struct node{
int v,w;
};
vector <node> e[maxn+10];
void add_edge(int u,int v,int w)
{
node newe;
newe.v=v;newe.w=w;
e[u].push_back(newe);
}
void SPFA(int s)
{
queue <int> q;
memset(d,-inf,sizeof(d)); //最长路初始化为负无穷
memset(vst,0,sizeof(vst));
q.push(s);
d[s]=0;vst[s]=1;
while(!q.empty())
{
int u=q.front();q.pop();
vst[u]=0; //????!
for(int i=0;i<e[u].size();++i){
node x=e[u][i];
int v=x.v,w=x.w;
if(d[v]<d[u]+w){
d[v]=d[u]+w;
if(!vst[v]){
q.push(v);
vst[v]=1;
}
}
}
}
}
int main()
{
int a,b,c;
int s,t;
while(~scanf("%d",&n)){
s=inf;t=-inf;
for(int i=0;i<=maxn;++i)
e[i].clear();
for(int i=0;i<n;++i){
scanf("%d%d%d",&a,&b,&c);
s=min(a-1,s);t=max(b,t);//a-1! //起点是所有点中最小的,终点最大的
add_edge(a-1,b,c); //a-1!左开右闭!不是说一个区间的第一个点不能放!
}
for(int i=s;i<=t;++i){
add_edge(i,i+1,0);
add_edge(i+1,i,-1);
} //区间条件使之能过去
SPFA(s);
printf("%d\n",d[t]);
}
return 0;
}
ZOJ2770
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
火烧连营hiahiahia
与上题没有很大区别,只是多了判断负环,就多开一个数组记录入队次数并判断即可;
当时一直wa一直卡的一个地方:m=0的时候,因为开始与结束端点都没有更新,会崩掉emmm,边界多考虑清楚。
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn=1050;
const int maxm=10020;
const int inf=0x3f3f3f3f;
const long long linf=0x3f3f3f3f3f3f;
int n,m;
int c[maxn],vst[maxn],cnt[maxn];
long long d[maxn];
struct node{
int v;
long long w;
};
vector <node> e[maxn];
void add_edge(int u,int v,long long w)
{
node x;
x.v=v;x.w=w;
e[u].push_back(x);
}
bool SPFA(int s)
{
queue <int> q;
for(int i=0;i<maxn;++i) d[i]=-linf;
memset(vst,0,sizeof(vst));
memset(cnt,0,sizeof(cnt));
q.push(s);
d[s]=0;vst[s]=cnt[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
vst[u]=0;
for(int i=0;i<e[u].size();++i){
node x=e[u][i];
int v=x.v;
long long w=x.w;
if(d[v]<d[u]+w){
d[v]=d[u]+w;
if(!vst[v]){
vst[v]=1;
q.push(v);
if(++cnt[v]>n)
return false; //有负权回路?!最长路,正?!
}
}
}
}
return true;
}
int main()
{
int a,b;
long long k;
int s,t;
while(~scanf("%d%d",&n,&m)){
s=inf;t=-inf;e[0].clear();
for(int i=1;i<=n;++i){
scanf("%d",&c[i]);
e[i].clear();
}
if(!m){ //坑!m=0时!
printf("0\n");
continue;
}
for(int i=0;i<m;++i){
scanf("%d%d%lld",&a,&b,&k);
add_edge(a-1,b,k);
s=min(a-1,s);t=max(b,t);
}
for(int i=s+1;i<=t;++i){
add_edge(i-1,i,0);
add_edge(i,i-1,-c[i]);
}
if(SPFA(s))
printf("%lld\n",d[t]);
else
printf("Bad Estimations\n");
}
return 0;
}

本文探讨了如何将带有约束条件的问题转化为图论中的单源最短路径或最长路径问题。通过具体实例介绍了如何建立图模型,并使用SPFA算法解决这类问题。包括处理负环的情况及确保解的存在性。
6014

被折叠的 条评论
为什么被折叠?



