zoj 2770 差分约束系统---2--2022年5月20日

bellman-ford算法

允许负数环,即允许边权值为负数。
只能做判断,不能算出权值。
松弛算法,对边松弛,dijkstra 算法的一些推论。
在这里插入图片描述dist[u] // 表示 从起点到u这个中间过渡点

S(s, vi ) = s( s, v(i-1) ) + w( vi-1, vi )

optimal substructure(最优子结构)

d[v0]=0 初始化时候。

#include <stdio.h>
int main()
{
    int dis[10], i, k, n, m;
    int inf = 999999999;
    n = m = 5;  // 顶点  边的个数
    int u[10] = { 0, 2,1,1,4,3 };  // 起点
    int v[10] = { 0, 3,2,5,5,4 };  // 终点
    int w[10] = { 0, 2,-3,5,2,3 };  // 权值
    for (i = 1; i <= n; i++)	// 初始化为估计值
        dis[i] = inf;
    dis[1] = 0;
    // 对每一条边进行  n - 1 次松弛
    for (k = 1; k <= n - 1; k++) {
        for (i = 1; i <= m; i++) {
    	    if (dis[v[i]] > dis[u[i]] + w[i])
    		dis[v[i]] = dis[u[i]] + w[i];
  	}
    }
    for (i = 1; i <= n; i++) {
        printf("%d ", dis[i]);
    }
    return 0;
}

差分约束系统

判断从s到u这个点
在这里插入图片描述

首先是线性规划问题。
Ax<=b
A表示一个矩阵。 x 表示一个向量, b 表示一个向量。
这些相当于一些条件。
一系列的约束条件。
每行只有一个1和一个-1,其他都是0.
表示只有两个变量的出现。
在这里插入图片描述

求解方程

把左边的不等式,转成右边的。
发现,就是松弛技术 的不等式。
然后就找到了起点终点和路径长度。
xj是终点, xi是起点, bk是路径长度。
在这里插入图片描述

差分约束系统

一、概念
如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统。

二、引例
给定n个变量和m个不等式,每个不等式的形式为 x[i] - x[j] <= a[k] (0 <= i, j < n, 0 <= k < m, a[k]已知),求 x[i] - x[j] 的最大值 。
例如当n = 4,m = 5,给出如下图所示的不等式组,求x3 - x0的最大值。
n表示结点数,M表示边的数量。在这里插入图片描述

把不等式左边看成一个直线。起点是被减数,终点是减数。边的权值为wij在这里插入图片描述 一般思路:我们可以尝试把几个不等式组合得到最后我们要求的式子,于是这些式子里最小的那个就是答案。

比如,在这个例子中:
所以最后结果就是3.
在这个过程中,我们是否想到这种方法与我们已经学的一种算法有所联系,是的,就是最短路算法。

约束图

把约束可视化,变成图。
在这里插入图片描述

四、构图求解
4.1 基本构图

在这里插入图片描述
在这里插入图片描述

以上的左边相加为0, 右边相加可能不为0。所以是矛盾的。

在这里插入图片描述

4.3 增加源点X0

具体实现时,往往在原图上附加一个顶点,这个顶点与每个顶点都连接一条权值为 0 0 0 的边,以上述不等式为例,也就是新加入一个未知数 x 0 ,然后对每个未知数都对 x 0 加一个不等式,得到在这里插入图片描述
化成顶点图
在这里插入图片描述
加源点之后发现 从源点到所有其他顶点的距离为0,并且可以变小,负数权值,

此时该图有最短路径

在这里插入图片描述在这里插入图片描述

在这里插入图片描述

对vO进行Bellman-Ford算法,可以得到d(vi),令xi=d(vi)+c,即为所求,其中C是任意一个常数,均满足原不等式组。当有xi为负数且题目要求xi非负时,常常令C为最小的xi的相反数。

课程问题

设x1,x2…xn是n个非负变量。关于变量x1,x2 … xn的差分约束是形如xi-xj<=c的不等式约束。其中c是常数。差分约束问题是对于给定的差分约束,确定变量x1,x2 … xn的值,使其满足差分约束,且使n个变量的最大值与最小值之差达到最小o请设计程序完成这个工作,对于给定的差分约束,计算满足差分约束的变量最大值与最小值之差的最小值。

第1行有2个正数n和m,表示有n个变量和m个差分约束,变量编号为1,2,…,n。接下来的m行中,每行有3个整数i,j,c,表示差分约束xi-xj<=c

输出要求:

输出满足差分约束的变量最大值与最小值之差的最小值.如果不存在满足要求值,则输出“Nosolution!”。
在这里插入图片描述

建立约束图

如下:
调用bellman_ford算法来完成找到最近的点。
在这里插入图片描述
在这里插入图片描述

问题升级

在这里插入图片描述
在这里插入图片描述

首先要发现一个关系: 工作i的开始时间 + 工作i的运行时间 = 工作i的截止时间

在这里插入图片描述

构建图

在这里插入图片描述
PTA zoj2770
Burn the Linked Camp
Time Limit: 2000 msMemory 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
在这里插入图片描述
这里的不等式,是双边的
由题目条件可以知道

仍然有问题的代码

//spfa+slf优化
#include<cstdio>
#include<iostream>
#define mx 999999
using namespace std;

int n;      // 一共有N个大营
int m;      //已知 从第i 个大营到第j个大营至少有多少士兵
int c[1001]; // 第i个大营最多有C[i]个士兵
int dist[1001]; //最少的兵营的士兵个数 a
int S[1001];  //前i 个大营的容量  d
const int mn = 23000;
int ei; //边的序号
struct Edge
{
    int start;
    int end;
    int cost; 
} edge[mn];

void init()
{
    ei = 0;
    int i;
    for (i = 0; i <= n; i++)
        dist[i] =0;
    dist[0] = 0;
    S[n] = 0;  //一开始初始化所有士兵总数为0
}


bool bellman_ford()
{
    int i, k, t;
    for (i = 0; i < n; i++)
    {
        /*
        假设第K条边的起点是u , 终点是v, 
        下面循环考虑第k 条边是否会使得源点V0到v 的最短距离缩短
        */
        for (k = 0; k < ei; k++)
        {
            if (dist[edge[k].start] != mx && dist[edge[k].start] + edge[k].cost < dist[edge[k].end]) //经典三角公式
            {
                dist[edge[k].end] = dist[edge[k].start] + edge[k].cost;
            }
        }
    }
    for (k = 0; k < ei; k++)
    {
        if (dist[edge[k].start] != mx && dist[edge[k].end] + edge[k].cost < edge[k].end)
            return false;//有回路
    }
    return true;
}
int main()
{
    while (cin>>n>>m)
    {
        init();
        int i,u, v, w;
        for (int i = 1; i <= n; ++i)
        {
            cin >> c[i];
            edge[ei].start = i - 1;
            edge[ei].end = i;  //构造边<i-1, i> 每一个兵营的数量是
            edge[ei].cost = c[i];
            ei++;
            //边 <i, i-1 > 这个权值为0
            edge[ei].start = i;
            edge[ei].end = i - 1;
            edge[ei].cost = 0;
            ei++;
            S[i] = c[i] + S[i - 1];
        }
        for (i = 0; i < m; i++)
        {
            cin >> u >> v >> w;
            edge[ei].start = v; edge[ei].end = u-1;
            edge[ei].cost = -w;
            ei++;
            edge[ei].start = u - 1;
            edge[ei].end = v;
            edge[ei].start = S[v] - S[u - 1];
            ei++;
        }
        if (!bellman_ford())
            cout << dist[n] - dist[0] << endl;
        else
            cout << "Bad Estimations" << endl;
    }
    return 0;
}
/*
3 2
1000 2000 3000
1 2 1100
2 3 1300 
3 1
100 200 300
2 3 600

*/
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值