第三章图论(一)

单源最短路的建图方式

例题:热浪(heatwv)
【题目描述】

德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。Farmer John此时以先天下之忧而忧,后天下之乐而乐的精神,身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。

FJ已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。这些路线包括起始点和终点先一共经过T (1 ≤ T ≤ 2,500)个城镇,方便地标号為1到T。除了起点和终点外的每个城镇由两条双向道路连向至少两个其它的城镇。每条道路有一个通过费用(包括油费,过路费等等)。

给定一个地图,包含C (1 ≤ C ≤ 6,200)条直接连接2个城镇的道路。每条道路由道路的起点Rs,终点Re (1 ≤ Rs ≤ T; 1 ≤ Re ≤ T),和花费(1 ≤ Ci ≤ 1,000)组成。求从起始的城镇Ts (1 ≤ Ts ≤ T)到终点的城镇Te(1 ≤ Te ≤ T)最小的总费用。
【输入】
第一行: 4个由空格隔开的整数: T, C, Ts, Te;
第2到第C+1行: 第i+1行描述第i条道路。有3个由空格隔开的整数: Rs, Re和Ci。
【输出】
一个单独的整数表示从Ts到Te的最小总费用。数据保证至少存在一条道路。
【输入样例】
7 11 5 4
2 4 2
1 4 3
7 2 2
3 4 3
5 7 5
7 3 3
6 1 1
6 3 4
2 4 3
5 6 3
7 2 1
【输出样例】
7
【提示】
【样例说明】
5->6->1->4 (3 + 1 + 3)
——————————————————————————————————————————————————

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

public class Main {
    static int INF = Integer.MAX_VALUE >> 1;
    static int N = 2510;
    static int M = 6200 * 2 + 10;
    static int n;
    static int m;
    static int S;
    static int T;
    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[] dist = new int[N];
    static int[] q = 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();
        S = sc.nextInt();
        T = sc.nextInt();
        Arrays.fill(h, - 1);
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        spfa();
        System.out.println(dist[T]);
    }

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

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

例题:信使(msner)
在这里插入图片描述

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

public class Main {
    static int INF = Integer.MAX_VALUE >> 1;
    static int N = 110;
    static int n;
    static int m;
    static int[][] d = new int[N][N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        for (int[] x : d) Arrays.fill(x, INF);
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            d[a][b] = Math.min(d[a][b], c);
            d[b][a] = d[a][b];
        }
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }
        int res = 0;
        for (int i = 1; i <= n; i++) {
            if (d[1][i] == INF) {
                res = -1;
                break;
            }else {
                res = Math.max(res, d[1][i]);
            }
        }
        System.out.println(res);
    }
}

例题:香甜的黄油
【题目描述】
农夫John发现做出全威斯康辛州最甜的黄油的方法:糖。把糖放在一片牧场上,他知道N(1≤N≤500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油。当然,他将付出额外的费用在奶牛上。
农夫John很狡猾。像以前的巴甫洛夫,他知道他可以训练这些奶牛,让它们在听到铃声时去一个特定的牧场。他打算将糖放在那里然后下午发出铃声,以至他可以在晚上挤奶。
农夫John知道每只奶牛都在各自喜欢的牧场(一个牧场不一定只有一头牛)。给出各头牛在的牧场和牧场间的路线,找出使所有牛到达的路程和最短的牧场(他将把糖放在那)。
【输入】
第一行: 三个数:奶牛数N,牧场数P(2≤P≤800),牧场间道路数C(1≤C≤1450)。
第二行到第N+1行: 1到N头奶牛所在的牧场号。
第N+2行到第N+C+1行:每行有三个数:相连的牧场A、B,两牧场间距(1≤D≤255),当然,连接是双向的。
【输出】
一行 输出奶牛必须行走的最小的距离和。
【输入样例】
3 4 5
2
3
4
1 2 1
1 3 5
2 3 7
2 4 3
3 4 5
【输出样例】
8
【提示】
说明:放在4号牧场最优。
在这里插入图片描述
————————————————————————————————————————————————————

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

public class Main {
    static int N = 810;
    static int M = 3000;
    static int INF = Integer.MAX_VALUE >> 1;
    static int n;
    static int p;
    static int m;
    static int[] id = new 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[] dist = new int[N];
    static int[] q = new int[N];
    static boolean[] st = new boolean[N];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        p = sc.nextInt();
        m = sc.nextInt();
        for (int i = 0; i < n; i++) {
            id[i] = sc.nextInt();
        }
        Arrays.fill(h, -1);
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        int res = INF;
        for (int i = 1; i <= p; i++) {
            res = Math.min(res, spfa(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 int spfa(int start) {
        Arrays.fill(dist, INF);
        dist[start] = 0;
        int hh = 0, tt = 1;
        q[0] = start;
        st[start] = 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;
                    }
                }
            }
        }
        int res = 0;
        for (int i = 0; i < n; i++) {
            int j = id[i];
            if (dist[j] == INF) return INF;
            res += dist[j];
        }
        return res;
    }
}

例题:最小花费
【题目描述】
在n个人中,某些人的银行账号之间可以互相转账。这些人之间转账的手续费各不相同。给定这些人之间转账时需要从转账金额里扣除百分之几的手续费,请问A最少需要多少钱使得转账后B收到100元。
【输入】
第一行输入两个正整数n,m,分别表示总人数和可以互相转账的人的对数。
以下m行每行输入三个正整数x,y,z,表示标号为x的人和标号为y的人之间互相转账需要扣除z%的手续费 (z<100)。
最后一行输入两个正整数A,B。数据保证A与B之间可以直接或间接地转账。
【输出】
输出A使得B到账100元最少需要的总费用。精确到小数点后8位。
【输入样例】
3 3
1 2 1
2 3 2
1 3 3
1 3
【输出样例】
103.07153164
【提示】
【数据规模】
1≤n≤2000
————————————————————————————————————————————————————

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

public class Main {
    static int N = 2010;
    static int n, m, S, T;
    static double[][] g = new double[N][N];
    static double[] dist = new double[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();
        while (m-- > 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            double z = (100.0 - c) / 100;
            g[a][b] = Math.max(g[a][b], z);
            g[b][a] = g[a][b];
        }
        S = sc.nextInt();
        T = sc.nextInt();
        dijkstra();
        System.out.printf("%.8f\n", 100 / dist[T]);
    }

    private static void dijkstra() {
        dist[S] = 1;
        for (int i = 1; i <= n; i++) {
            int t = -1;
            for (int j = 1; j <= n; j++) {
                if (!st[j] && (t == -1 || dist[t] < dist[j])) {
                    t = j;
                }
            }
            st[t] = true;
            for (int j = 1; j <= n; j++) {
                dist[j] = Math.max(dist[j], dist[t] * g[t][j]);
            }
        }
    }
}

例题:920. 最优乘车
在这里插入图片描述
换乘多少次 可以转换为 乘坐过多少次车减1

  1. 在同一条路线中,任意一个在此路线上的车站均能沿着该路线的方向到达后面的车站,权值都是1,表示只乘坐一次车
  2. 通过建图,由于权值均是1,使用bfs求出1号点到n号点最少乘过多少次车
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static int N = 510;
    static int m;
    static int n;
    static boolean[][] g = new boolean[N][N];
    static int[] stop = new int[N];
    static int[] dist = new int[N];

    static void bfs() {
        Queue<Integer> q = new LinkedList<Integer>();
        Arrays.fill(dist, -1);
        q.add(1);
        dist[1] = 0;
        while (!q.isEmpty()) {
            int t = q.poll();
            for (int i = 1; i <= n; i++) {
                if (dist[i] != -1) continue;
                if (!g[t][i]) continue;
                dist[i] = dist[t] + 1;
                q.add(i);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s1 = br.readLine().split(" ");
        m = Integer.parseInt(s1[0]);
        n = Integer.parseInt(s1[1]);
        while (m-- > 0) {
            String[] s2 = br.readLine().split(" ");
            for (int i = 0; i < s2.length; i++) {
                stop[i] = Integer.parseInt(s2[i]);
            }
            for (int i = 0; i < s2.length - 1; i++)
                for (int j = i + 1; j < s2.length; j++)
                    g[stop[i]][stop[j]] = true;
        }

        bfs();
        //若不能到达
        if (dist[n] == -1) {
            System.out.println("NO");
        } else {
            System.out.println(dist[n] - 1);
        }
    }
}

例题:903. 昂贵的聘礼
在这里插入图片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    static int N = 110;
    static int INF = 0x3f3f3f3f;
    static int n;
    static int m;
    static int[][] w = new int[N][N];
    static int[] level = new int[N];
    static int[] dist = new int[N];
    static boolean[] st = new boolean[N];

    //等级必须在[left,right]中才可走
    static int dijkstra(int left, int right) {
        Arrays.fill(st, false);
        Arrays.fill(dist, INF);
        dist[0] = 0;

        for (int i = 0; i < n; i++) {
            int t = -1;
            for (int j = 0; j <= n; j++) {
                if (!st[j] && (t == -1 || dist[j] < dist[t]))
                    t = j;
            }
            st[t] = true;
            for (int j = 1; j <= n; j++) {
                //等级在指定范围
                if (level[j] >= left && level[j] <= right)
                    dist[j] = Math.min(dist[j], dist[t] + w[t][j]);
            }
        }
        return dist[1];
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] s1 = br.readLine().split(" ");
        m = Integer.parseInt(s1[0]);
        n = Integer.parseInt(s1[1]);
        for (int i = 0; i <= n; i++) Arrays.fill(w[i], INF);

        for (int i = 1; i <= n; i++) {
            String[] s2 = br.readLine().split(" ");
            int price = Integer.parseInt(s2[0]); // 价格
            level[i] = Integer.parseInt(s2[1]);
            int cnt = Integer.parseInt(s2[2]);  // 可以替换的数量
            w[0][i] = Math.min(w[0][i], price);
            while (cnt-- > 0) {
                String[] s3 = br.readLine().split(" ");
                int a = Integer.parseInt(s3[0]);
                int cost = Integer.parseInt(s3[1]);
                w[a][i] = Math.min(w[a][i], cost);
            }
        }
        int res = INF;
        for (int i = level[1] - m; i <= level[1]; i++) {
            res = Math.min(res, dijkstra(i, i + m));
        }
        System.out.println(res);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值