第三章图论(八)

差分约束

在这里插入图片描述
例题:【SCOI2011】糖果
【题目描述】
幼儿园里有 N 个小朋友, lxhgww 老师现在想要给这些小朋友们分配糖果,要求每个小朋友都要分到糖果。

但是小朋友们也有嫉妒心,总是会提出一些要求,比如小明不希望小红分到的糖果比他的多,于是在分配糖果的时候, lxhgww 需要满足小朋友们的 K 个要求。

幼儿园的糖果总是有限的, lxhgww 想知道他至少需要准备多少个糖果,才能使得每个小朋友都能够分到糖果,并且满足小朋友们所有的要求。
【输入】
输入的第一行是两个整数 N , K 。
接下来 K 行,表示这些点需要满足的关系,每行 3 个数字, x , A , B 。
如果 X=1 .表示第 A 个小朋友分到的糖果必须和第 B 个小朋友分到的精果一样多。
如果 X=2 ,表示第 A 个小朋友分到的糖果必须少于第 B 个小朋友分到的糖果。
如果 X=3 ,表示第 A 个小朋友分到的糖果必须不少于第 B 个小朋友分到的糖果。
如果 X=4 ,表示第 A 个小朋友分到的糖果必须多于第 B 个小朋友分到的糖果。
如果 X=5 ,表示第 A 个小朋友分到的糖果必须不多于第 B 个小朋友分到的糖果。
【输出】
输出一行,表示 lxhgww 老师至少需要准备的糖果数,如果不能满足小朋友们的所有要求,就输出 −1 。
【输入样例】
5 7
1 1 2
2 3 2
4 4 1
3 4 5
5 4 5
2 3 5
4 5 1
【输出样例】
11
【提示】
数据范围与提示
对于 30% 的数据,保证 N<100N<100N<100 。
对于 100% 的数据,保证 N<100000,K≤100000,1≤X≤5,1≤A,B≤N 。
————————————————————————————————————————————————————

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int INF = Integer.MAX_VALUE >> 1;
    static int N = 100010;
    static int M = 300010;
    static int n;
    static int m;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] w = new int[M];
    static int[] ne = new int[M];
    static int idx;
    static int[] q = new int[N];
    static int[] cnt = new int[N];
    static int[] dist = new int[N];
    static boolean[] st = new boolean[N];

    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(a, b, 0);
                add(b, a, 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.println(-1);
        }else {
            long res = 0;
            for (int i = 1; i <= n; i++) {
                res += dist[i];
            }
            System.out.println(res);
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static boolean spfa() {
        int hh = 0, tt = 1;
        Arrays.fill(dist, -INF);
        dist[0] = 0;
        q[0] = 0;
        st[0] = true;
        while (hh != tt) {
            int t = q[--tt];//栈的写法
            //if (hh == N) hh = 0;使用循环队列会TLE,优化成栈
            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[tt++] = j;
                        st[j] = true;
                    }
                }
            }
        }
        return true;
    }
}

例题: 362. 区间
在这里插入图片描述
输入样例:
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1
输出样例:
6
————————————————————————————————————————————————————

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int INF = Integer.MAX_VALUE >> 1;
    static int N = 50010;
    static int M = 150010;
    static int n;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] w = new int[M];
    static int[] ne = new int[M];
    static int idx;
    static int[] q = new int[N];
    static int[] dist = new int[N];
    static boolean[] st = new boolean[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= 50001; 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.println(dist[50001]);
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static void spfa() {
        int hh = 0, tt = 1;
        Arrays.fill(dist, -INF);
        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];
                    if (!st[j]) {
                        q[tt++] = j;
                        if (tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }
    }
}

例题:排队布局
在这里插入图片描述
【输入样例】
4 2 1
1 3 10
2 4 20
2 3 3
【输出样例】
27
在这里插入图片描述
————————————————————————————————————————————————————

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int INF = Integer.MAX_VALUE >> 1;
    static int N = 1010;
    static int M = 21010;
    static int n;
    static int m1;
    static int m2;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] w = new int[M];
    static int[] ne = new int[M];
    static int idx;
    static int[] q = new int[N];
    static int[] dist = new int[N];
    static int[] cnt = new int[N];
    static boolean[] st = new boolean[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m1 = sc.nextInt();
        m2 = sc.nextInt();
        Arrays.fill(h, -1);
        for (int i = 1; i <= n; i++) {
            add(i + 1, i, 0);
        }
        while (m1-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            if (b < a) {
                int t = a;
                a = b;
                b = t;
            }
            add(a, b, c);
        }
        while (m2-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            if (b < a) {
                int t = a;
                a = b;
                b = t;
            }
            add(b, a, -c);
        }
        if (!spfa(n)) {
            System.out.println(-1);
        } else {
            spfa(1);
            if (dist[n] == INF) {
                System.out.println(-2);
            } else {
                System.out.println(dist[n]);
            }
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static boolean spfa(int size) {
        int hh = 0, tt = 0;
        Arrays.fill(dist, INF);
        Arrays.fill(cnt, 0);
        Arrays.fill(st, false);
        q[0] = 0;
        dist[0] = 0;
        st[0] = true;
        for (int i = 1; i <= size; i++) {
            dist[i] = 0;
            q[tt++] = i;
            st[i] = 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) return false;
                    if (!st[j]) {
                        q[tt++] = j;
                        if (tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }
        return true;
    }
}

例题:393. 雇佣收银员
在这里插入图片描述

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int INF = Integer.MAX_VALUE >> 1;
    static int N = 30;
    static int M = 100;
    static int n;
    static int[] h = new int[N];
    static int[] e = new int[M];
    static int[] w = new int[M];
    static int[] ne = new int[M];
    static int idx;
    static int[] q = new int[N];
    static int[] dist = new int[N];
    static int[] cnt = new int[N];
    static boolean[] st = new boolean[N];
    static int[] r = new int[N];
    static int[] num = new int[N];

    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();
            }
            n = sc.nextInt();
            Arrays.fill(num, 0);
            for (int i = 0; i < n; i++) {
                int t = sc.nextInt();
                num[t + 1]++;

            }
            boolean success = false;
            for (int i = 0; i <= 1000; i++) {
                if (spfa(i)) {
                    System.out.println(i);
                    success = true;
                    break;
                }
            }
            if (!success) System.out.println("No Solution");
        }
    }

    private static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }

    private static boolean spfa(int s24) {
        build(s24);
        int hh = 0, tt = 1;
        Arrays.fill(dist, -INF);
        Arrays.fill(cnt, 0);
        Arrays.fill(st, false);
        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] >= 25) return false;
                    if (!st[j]) {
                        q[tt++] = j;
                        if (tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }
        return true;
    }

    private static void build(int s24) {
        Arrays.fill(h, -1);
        idx = 0;
        for (int i = 1; i <= 24; i++) {
            add(i - 1, i, 0);
            add(i, i - 1, -num[i]);
        }
        for (int i = 8; i <= 24; i++) {
            add(i - 8, i, r[i]);
        }
        for (int i = 1; i <= 7; i++) {
            add(i + 16, i, -s24 + r[i]);
        }
        add(0, 24, s24);
        add(24, 0, -s24);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值