(1)求不等式组可行解
注意:
求最大值,最短路:将不等式转化为xi<=xj+ck的形式,从xj走到xi,长度为ck的一条边
求最小值,最长路:将不等式转化为xi>=xj+ck的形式,从xj走到xi,长度为ck的一条边
记忆技巧:不等号方向和最短/长路更新的不等号方向 相反
(2) 求可行解的最值求xi的最小值,做最长路;求最大值,做最短路 dis[i]即为最值
应该保证有一个点能遍历到所有点,如果没有则建立超级源点,向所有点连一条长度为0的边
糖果
题意:每个小朋友分到糖果有相互约束条件,求最少准备多少糖果
注意: 使用deque来提高判环的效率(从尾放,从尾取)
bool spfa(){
//最长路
memset(dist,-0x3f,sizeof dist);
dist[0] = 0;
//使用deque可以提高判环的效率
deque<int> q;
q.push_back(0);
st[0] = true;
while(q.size()){
int t = q.back();
q.pop_back();
st[t] = false;
for (int i = h[t]; i!=-1; i = ne[i]){
int j = e[i];
if (dist[j] < dist[t] + w[i]){
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if (cnt[j] >= n + 1) return false;
if (!st[j]){
q.push_back(j);
st[j] = true;
}
}
}
}
return true;
}
while (m -- ){
int x, a, b;
cin >> x >> a >> b;
if (x == 1) add(b, a, 0), add(a, b, 0);//A=B
else if (x == 2) add(a, b, 1);//B≥A+1
else if (x == 3) add(b, a, 0);//A≥B
else if (x == 4) add(b, a, 1);//A≥B+1
else add(a, b, 0);//B≥A
}
//每个同学都要分到糖果 x[i]≥1
//超级源点0 x[i] ≥ x[0]+1 <=> x[i] ≥ 1
for (int i = 1; i <= n; i ++ ){
add(0, i, 1);
}
if (!spfa()) cout << "-1" << endl;
else{
ll res = 0;
for (int i = 1; i <= n; i ++ ) res += dist[i];
cout << res << endl;
}
区间
前缀和思想,s[i]表示[1,i]中被选的个数,对于题目要求即
同时满足前缀和数组要求 和
求最小值,建图跑最长路即可
#include<bits/stdc++.h>
using namespace std;
const int N=50010;
int h[N],ne[3*N],w[3*N],e[3*N],idx;
bool vis[N];
int dis[N];
void add(int a,int b,int c){
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
void spfa(){
memset(dis, -0x3f, sizeof dis);
dis[0]=0;
queue<int> q;
q.push(0);
vis[0]=true;
while(!q.empty()){
int t=q.front();q.pop();
vis[t]=false;
for(int i=h[t];~i;i=ne[i]){
int j=e[i];
if(dis[j]<dis[t]+w[i]){
dis[j]=dis[t]+w[i];
if(!vis[j]){
q.push(j);
vis[j]=true;
}
}
}
}
}
int main(){
memset(h,-1,sizeof h);
int n;
cin>>n;
for (int i = 0; i < n; i++){
int a,b,c;
cin>>a>>b>>c;
a++,b++;//坐标前移,把0留出来
add(a-1,b,c);//s[b]-s[a-1]>=c
}
for(int i = 1; i <= 50001; i++) {
add(i - 1, i, 0);//s[i]-s[i-1]>=0
add(i, i - 1, -1);//s[i]-s[i-1]<=1
}
spfa();
cout<<dis[50001];
}
排队布局
N头奶牛,编号和位置顺序相同,给定奶牛的位置之间的约束
编号和位置顺序相同,即
约束一
约束二
问题一:判断约束有没有解
从超级源点出发(等价于把所有的点入队)进行SPFA,如果有负环(这里求的是最短路)则无解
问题二:1到N是否可以无限远
如果1到N不连通,说明1和N没有约束,即可以无限远
问题三:超级源点
本题中因为有约束,所以所有点是联通的,无需超级源点
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
//第一种边n条<=1000 第2、3种边各10000条
const int N = 1010, M = 21010, INF = 0x3f3f3f3f;
int n, m1, m2;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
int q[N], cnt[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx] = b;
ne[idx] = h[a];
w[idx] = c;
h[a] = idx++;
}
bool spfa(int size)
{
memset(dist,0x3f,sizeof dist);
memset(st,0,sizeof st);
memset(cnt,0,sizeof cnt);
queue<int> q;
for(int i=1;i<=size;i++)
{
q.push(i);
dist[i] = 0;
st[i] = true;
}
while(q.size())
{
int t=q.front();
q.pop();
st[t] = false;
for(int i = h[t];~i;i=ne[i])
{
int j = e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j] = dist[t]+w[i];
cnt[j] = cnt[t]+1;
if(cnt[j]>=n)return true;//负环--无解
if(!st[j])
{
q.push(j);
st[j] = true;
}
}
}
}
return false;
}
int main()
{
cin >> n >> m1 >> m2;
memset(h,-1,sizeof h);
for(int i=1;i<n;i++) add(i+1,i,0);//x[i] ≤x[i+1]+0
while(m1--)
{
int a,b,c;
cin >> a >> b >> c;
if(a>b) swap(a,b);
add(a,b,c);//x[b] ≤x[a]+L
}
while(m2--)
{
int a,b,c;
cin >> a >> b >> c;
if(a>b) swap(a,b);
add(b,a,-c);//x[a] ≤x[b]-D
}
// 第一问 问这些约束有没有解 从超级源点出发==把所有点加入队列 看有没有负环
if(spfa(n)) cout << -1;
// 第二问 只需要把第一个点加入队列 求第1个点到第n个点的最短距离
else
{
spfa(1);
//如果 1 号奶牛和 N 号奶牛间的距离可以任意大,即1和N不连通,输出-2;
if(dist[n]==INF) cout << -2;
else cout << dist[n];
}
return 0;
}