差分约束

(1)求不等式组的可行解

        源点需满足的条件:从源点出发,一定可以到达所有的边

求最短路

        步骤:1.先将每个不等式xi<=xj + ck,转化成一条从xj走到xi,长度为ck的一条边

                   2.找一个超级源点,使得该源点一定可以遍历到所有的边

                   3.从该源点求一遍单源最短路

                        结果1:如果存在负环,则原不等式一定无解

                        结果2:如果没有负环,则dist[i]就是原不等式组的一个可行解

(2)如何求最大值或者最小值(这里的最值指的是每个变量的最值)

        问题:如何转化xi<=c,其中c是一个常数,这类的不等式

        方法:建立一个超级源点0,然后建立一条0->i,长度为c的边

        结论:如果求的是最小值,则应该求最长路;如果求的是最大值,则应该求最短路

 

1169. 糖果 - AcWing题库 

 

超时:用队列来写

import java.util.*;

//要求最小值应求最长路
public class Main{
    static int N = 100010, M = 3 * N;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static long[] dist = new long[N];
    static boolean[] st = new boolean[N];
    static int[] q = new int[N];
    static int[] cnt = new int[N];//边数
    static int n, m, idx;
    
    //邻接表存储
    public static void add(int a, int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx ++;
    }
    
    public static boolean spfa(){
        Arrays.fill(dist, -0x3f3f3f3f);//求最长路,应更新为负无穷
        int hh = 0, tt = 1;
        q[0] = 0;
        dist[0] = 0;
        st[0] = true;
        
        while(hh != tt){
            int t = q[hh ++];//一开始写成循环队列
            if(hh == N) hh = 0;
            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 true;
                    if(!st[j]){
                        q[tt ++] = j;
                        if(tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }
        return false;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        
        Arrays.fill(h, -1);//初始化队头
        
        while(m -- > 0){
            int x = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            
            if(x == 1){
                add(b, a, 0);
                add(a, b, 0);
            }else if(x == 2){
                add(a, b, 1);
            }else if(x == 3){
                add(b, a, 0);
            }else if(x == 4){
                add(b, a, 1);
            }else add(a, b, 0);
        }
        
        for(int i = 1; i <= n; i ++){
            add(0, i, 1);//超级源点到每个点连一条边
        }
        
        if(spfa()){
            System.out.print("-1");
        }else{
            long res = 0;
            for(int i = 1; i <= n; i ++){
                res += dist[i];
            }
            System.out.print(res);
        }
    }
}

 

正确解法:用栈来写 

import java.util.*;

//要求最小值应求最长路
public class Main{
    static int N = 100010, M = 3 * N;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static long[] dist = new long[N];
    static boolean[] st = new boolean[N];
    static int[] q = new int[N];
    static int[] cnt = new int[N];//边数
    static int n, m, idx;
    
    //邻接表存储
    public static void add(int a, int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx ++;
    }
    
    public static boolean spfa(){
        Arrays.fill(dist, -0x3f3f3f3f);//求最长路,应更新为负无穷
        Stack<Integer> sk = new Stack<>();//栈
        sk.push(0);
        dist[0] = 0;
        st[0] = true;
        
        while(!sk.isEmpty()){
            int t = sk.pop();//一开始写成循环队列

            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 true;
                    if(!st[j]){
                        sk.push(j);
                        st[j] = true;
                    }
                }
            }
        }
        return false;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        
        Arrays.fill(h, -1);//初始化队头
        
        while(m -- > 0){
            int x = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            
            if(x == 1){
                add(b, a, 0);
                add(a, b, 0);
            }else if(x == 2){
                add(a, b, 1);
            }else if(x == 3){
                add(b, a, 0);
            }else if(x == 4){
                add(b, a, 1);
            }else add(a, b, 0);
        }
        
        for(int i = 1; i <= n; i ++){
            add(0, i, 1);//超级源点到每个点连一条边
        }
        
        if(spfa()){
            System.out.print("-1");
        }else{
            long res = 0;
            for(int i = 1; i <= n; i ++){
                res += dist[i];
            }
            System.out.print(res);
        }
    }
}

 正确解法:手写栈

import java.util.*;

//要求最小值应求最长路
public class Main{
    static int N = 100010, M = 3 * N;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static long[] dist = new long[N];
    static boolean[] st = new boolean[N];
    static int[] q = new int[N];
    static int[] cnt = new int[N];//边数
    static int n, m, idx;
    
    //邻接表存储
    public static void add(int a, int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx ++;
    }
    
    public static boolean spfa(){
        Arrays.fill(dist, -0x3f3f3f3f);//求最长路,应更新为负无穷
        int hh = 0, tt = 1;
        q[0] = 0;
        dist[0] = 0;
        st[0] = true;
        
        while(hh != tt){
            int t = q[-- tt];//手写栈
            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 true;
                    if(!st[j]){
                        q[tt ++] = j;

                        st[j] = true;
                    }
                }
            }
        }
        return false;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        
        Arrays.fill(h, -1);//初始化队头
        
        while(m -- > 0){
            int x = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            
            if(x == 1){
                add(b, a, 0);
                add(a, b, 0);
            }else if(x == 2){
                add(a, b, 1);
            }else if(x == 3){
                add(b, a, 0);
            }else if(x == 4){
                add(b, a, 1);
            }else add(a, b, 0);
        }
        
        for(int i = 1; i <= n; i ++){
            add(0, i, 1);//超级源点到每个点连一条边
        }
        
        if(spfa()){
            System.out.print("-1");
        }else{
            long res = 0;
            for(int i = 1; i <= n; i ++){
                res += dist[i];
            }
            System.out.print(res);
        }
    }
}

 

362. 区间 - AcWing题库

 

import java.util.*;

public class Main{
    static int N = 50010, M = 3 * N + 10;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] dist = new int[N];
    static boolean[] st = new boolean[N];
    static int n, idx;
    
    public static void add(int a, int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx ++;
    }
    
    public static void spfa(){
        Arrays.fill(dist, -0x3f3f3f3f);//求最小值也就是求最长路,先初始化为负无穷
        Queue<Integer> q = new LinkedList<>();
        q.offer(0);
        dist[0] = 0;
        st[0] = true;
        
        while(!q.isEmpty()){
            int t = q.poll();
            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];
                    if(!st[j]){
                        q.offer(j);
                        st[j] = true;
                    }
                }
            }
        }
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        
        Arrays.fill(h, -1);
        
        for(int i = 1; i < N; i ++){
            add(i - 1, i, 0);
            add(i, i - 1, -1);
        }
        
        while(n -- > 0){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            a ++;//整体右移一位
            b ++;
            add(a - 1, b, c);
        }
        
        spfa();//数据保证一定有解,不用判断是否有负环
        
        System.out.print(dist[50001]);
    }
}

 

1170. 排队布局 - AcWing题库

import java.util.*;

public class Main{
    static int N = 1010, M = 21010, INF = 0x3f3f3f3f;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] dist = new int[N];
    static int[] cnt = new int[N];
    static boolean[] st = new boolean[N];
    static int n, ml, md, idx;
    
    public static void temp(int a, int b){
        int temp = a;
        a = b;
        b = a;
    }
    
    public static void add(int a, int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx ++;
    }
    
    public static boolean spfa(int size){
        Arrays.fill(dist, INF);
        Arrays.fill(cnt, 0);
        Arrays.fill(st, false);
        
        Queue<Integer> q = new LinkedList<>();
        for(int i = 1; i <= size; i ++){
            dist[i] = 0;
            q.offer(i);
            st[i] = true;
        }
        
        while(!q.isEmpty()){
            int t = q.poll();
            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) return true;
                    
                    if(!st[j]){
                        q.offer(j);
                        st[j] = true;
                    }
                }
            }
        }
        return false;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Arrays.fill(h, -1);
        
        n = sc.nextInt();
        ml = sc.nextInt();
        md = sc.nextInt();
        
        for(int i = 1; i < n; i ++){
            add(i + 1, i, 0);
        }
        
        while(ml -- > 0){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            if(b < a) temp(a, b);
            add(a, b, c);
        }
        
        while(md -- > 0){
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            if(b < a) temp(b, a);
            add(b, a, -c);
        }
        
        if(spfa(n)) System.out.print("-1");//判断是否有负环
        else{
            spfa(1);
            if(dist[n] == INF) System.out.print("-2");
            else System.out.print(dist[n]);
        }
    }
}

 

393. 雇佣收银员 - AcWing题库

 

import java.util.*;

public class Main{
    static int N = 30, M = 110;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] dist = new int[N];
    static int[] cnt = new int[N];
    static int[] r = new int[N];//最小需求量
    static int[] nums = new int[N];//申请数
    static boolean[] st = new boolean[N];
    static int n, idx;
    
    //邻接表存储
    public static void add(int a, int b, int c){
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx ++;
    }
    
    //建图
    public static void build(int c){
        Arrays.fill(h, -1);//多组数据,每次都要进行初始化
        idx = 0;
        
        add(0, 24, c);
        add(24, 0, -c);
        
        for(int i = 1; i <= 24; i ++){
            add(i - 1, i, 0);
            add(i, i - 1, - nums[i]);
        }
        
        for(int i = 1; i <= 7; i ++){
            add(i + 16, i, r[i] - c);
        }
        
        for(int i = 8; i <= 24; i ++){
            add(i - 8, i, r[i]);
        }
    }
    
    public static boolean spfa(int c){
        build(c);
        
        Arrays.fill(dist, -0x3f3f3f3f);
        Arrays.fill(st, false);
        Arrays.fill(cnt, 0);
        
        Queue<Integer> q = new LinkedList<>();
        dist[0] = 0;
        st[0] = true;
        q.offer(0);
        
        while(!q.isEmpty()){
            int t = q.poll();
            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] >= 25) return false;
                    
                    if(!st[j]){
                        q.offer(j);
                        st[j] = true;
                    }
                }
            }
        }
        return true;
    }
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        while(T -- > 0){
            for(int i = 1; i <= 24; i ++){
                r[i] = sc.nextInt();
            }
            
            Arrays.fill(nums, 0);
            n = sc.nextInt();
            for(int i = 0; i < n; i ++){
                int t = sc.nextInt();
                nums[t + 1] ++;
            }
            
            boolean flag = false;
            for(int i = 0; i <= 1000; i ++){//枚举所有s24可能的值
                if(spfa(i)){
                    System.out.println(i);
                    flag = true;//只要有一个可以就行
                    break;
                }
            }
            
            if(!flag) System.out.println("No Solution");
        }
    }
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/442747.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

CentOS7 利用remi yum源安装php8.1

目录 前言remi yum源remi yum源 支持的操作系统remi yum源 支持的php版本 安装epel源安装remi源安装 php8.1查看php版本查看php-fpm服务启动php-fpm服务查看php-fpm服务运行状态查看php-fpm服务占用的端口查看 php8.1 相关的应用 前言 CentOS Linux release 7.9.2009 (Core) …

【粉丝福利第四期】:《低代码平台开发实践:基于React》(文末送书)

文章目录 前言一、React与低代码平台的结合优势二、基于React的低代码平台开发挑战三、基于React的低代码平台开发实践四、未来展望《低代码平台开发实践&#xff1a;基于React》五、粉丝福利 前言 随着数字化转型的深入&#xff0c;企业对应用开发的效率和灵活性要求越来越高…

分销商城微信小程序:用户粘性增强,促进复购率提升

在数字化浪潮的推动下&#xff0c;微信小程序作为一种轻便、高效的移动应用形式&#xff0c;正成为越来越多企业开展电商业务的重要平台。而分销商城微信小程序的出现&#xff0c;更是为企业带来了前所未有的机遇。通过分销商城微信小程序&#xff0c;企业不仅能够拓宽销售渠道…

Double和Float类

Double类 功能&#xff1a;实现对Double基本型数据的类包 构造方法&#xff1a; (double num) double Value()方法&#xff1a;返回对象中的double型数据。 Float类 功能&#xff1a;实现对float基本型数据的类包装。 构造方法&#xff1a; (float num) Float Value()方法…

户用光伏创新技术,引领光伏时代进步

户用光伏近几年由于国家政策支持力度加大&#xff0c;技术也在快速发展&#xff0c;成功引领我国光伏时代的进步&#xff0c;掌握核心技术必将在新能源市场中抢占主导地位&#xff01; 一、制造方面 1.高效低成本晶硅太阳能电池表界面制造技术 这项技术主要涉及晶硅太阳能电池…

CraxsRat7.4 安卓手机远程管理软件

CRAXSRAT 7.4 最新视频 https://v.douyin.com/iFjrw2aD/ 官方网站下载 http://craxsrat.cn/ 不要问我是谁&#xff0c;我是活雷锋。 http://craxsrat.cn/ CraxsRat CraxsRat7 CraxsRat7.1 CraxsRat7.2 CraxsRat7.3 CraxsRat7.4

WPF 窗口添加投影效果Effect

BlurRadius&#xff1a;阴影半径 Color&#xff1a;颜色 Direction&#xff1a;投影方向 ShadowDepth&#xff1a;投影的深度 <Window.Effect><DropShadowEffect BlurRadius"10" Color"#FF858484" Direction"300" ShadowDepth&quo…

数据集下载汇总

国家数据网 https://data.stats.gov.cn/ 国家数据是国家统计局发布统计信息的网站&#xff0c;包含了我国经济、民生、农业、工业、运输、旅游、教育、科技、卫生等多个方面的数据&#xff0c;并且在月度、季度、年度都有覆盖&#xff0c;较为全面和权威&#xff0c;对于社会…

误删数据怎么恢复?四种实用方法全解析

如果您想知道如何恢复计算机上已删除的文件&#xff0c;您需要明确您应用了哪种删除。这完全取决于您如何删除文件。通常&#xff0c;您可以从回收站恢复已删除的文件并检索以前版本的文件。对于这些永久删除的文件&#xff0c;您在计算机上看不到&#xff0c;那么您必须尝试深…

Python绘图-12地理数据可视化

Matplotlib 自 带 4 类别 地理投影&#xff1a; Aitoff, Hammer, Mollweide 及 Lambert 投影&#xff0c;可以 结 合以下四 张 不同 的 图 了解四 种 不同投影 区别 。 12.1Aitoff投影 12.1.1图像呈现 12.1.2绘图代码 import numpy as np # 导入numpy库&#xff0c;用于…

初阶数据结构:排序(学习笔记)

目录 1. 各种排序算法的分类2. 插入排序2.1 直接插入排序2.2 希尔排序 3. 选择排序3.1 选择排序3.2 堆排序4. 交换排序4.1 冒泡排序4.2 快速排序4.2.1 霍尔法&#xff08;hoare&#xff09;4.2.2 挖坑法&#xff08;hole&#xff09;4.4.3 前后指针法4.4.4 补充&#xff1a;非递…

神经网络线性量化方法简介

可点此跳转看全篇 目录 神经网络量化量化的必要性量化方法简介线性对称量化线性非对称量化方法神经网络量化 量化的必要性 NetworkModel size (MB)GFLOPSAlexNet2330.7VGG-1652815.5VGG-1954819.6ResNet-50983.9ResNet-1011707.6ResNet-15223011.3GoogleNet271.6InceptionV38…

Codeforces Round 929 (Div. 3)- ABCDEFG

A:Turtle Puzzle: Rearrange and Negate 思路&#xff1a; 将负的元素全部排到一起&#xff0c;然后对它们符号取反&#xff0c;然后求所有元素的和&#xff0c;此时就是最大值了。 代码&#xff1a; #include<iostream> using namespace std;void solve() {int n;cin&…

资产管理系统有哪些(一体化资产管理平台推荐)

企业资产管理系统是一种关键的工具&#xff0c;旨在帮助企业有效地管理和追踪其资产。 该系统利用计算机系统和相关软件&#xff0c;通过信息化、智能化的方式&#xff0c;对资产进行全面的可视化管理&#xff0c;从而提高管理效率、降低运营成本&#xff0c;并确保资产的安全…

JVM的工作流程

目录 1.JVM 简介 2.JVM 执行流程 3. JVM 运行时数据区 3.1 堆&#xff08;线程共享&#xff09; 3.3 本地方法栈&#xff08;线程私有&#xff09; 3.4 程序计数器&#xff08;线程私有&#xff09; 3.5 方法区&#xff08;线程共享&#xff09; 4.JVM 类加载 ① 类…

【Unity】Tag、Layer、LayerMask

文章目录 层&#xff08;Layer&#xff09;什么是LayerLayer的应用场景Layer层的配置&#xff08;Tags & Layers&#xff09;Layer的数据结构LayerMaskLayer的选中和忽略Layer的管理&#xff08;架构思路&#xff09;层碰撞矩阵设置&#xff08;Layer Collision Matrix&…

搜狐新闻Hybrid AI引擎端侧离线大语言模型探索

本文字数&#xff1a;3027字 预计阅读时间&#xff1a;20分钟 01 一、导读 • LLM 以及移动平台落地趋势 • 搜狐AI引擎内建集成离线可运行的GPT模型 • Keras 定制预训练模型 • TensorFlow Lite converter 迁移到移动设备 02 二、LLM 1.1什么是LLM L…

考研复习c语言初阶(1)

本人准备考研&#xff0c;现在开始每天更新408的内容&#xff0c;目标这个月结束C语言和数据结构&#xff0c;每天更新~ 一.再次认识c语言 C语言是一门通用计算机编程语言&#xff0c;广泛应用于底层开发。C语言的设计目标是提供一种能以简易 的方式编译、处理低级存储器、产生…

【数据库-黑马笔记】基础-函数和约束

本文参考b站黑马数据库视频,总结详细全面的笔记 ,可结合视频观看27~36集 MYSQL 的基础知识框架如下 目录 一、 函数 1、字符串函数 2、数值函数 3、日期函数 4、流程函数 5、小结: 二、约束 1、概述 2、 约束演示 3、外键约束 4、外键删除更新行为 5、小结: …

【npm】前端工程项目配置文件package.json详解

简言 详细介绍了package.json中每个字段的作用。 package.json 本文档将为您介绍 package.json 文件的所有要求。它必须是实际的 JSON&#xff0c;而不仅仅是 JavaScript 对象文字。 如果你要发布你的项目&#xff0c;这是一个特别重要的文件&#xff0c;其中name和version是…
最新文章