单源最短路的综合应用
例题:新年好
【题目描述】
原题来自:CQOI 2005
重庆城里有 n 个车站,m 条双向公路连接其中的某些车站。每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费的时间可能不同。在一条路径上花费的时间等于路径上所有公路需要的时间之和。
佳佳的家在车站 1,他有五个亲戚,分别住在车站 a,b,c,d,e。过年了,他需要从自己的家出发,拜访每个亲戚(顺序任意),给他们送去节日的祝福。怎样走,才需要最少的时间?
【输入】
第一行:n,m 为车站数目和公路的数目。
第二行:a,b,c,d,e 为五个亲戚所在车站编号。
以下 m 行,每行三个整数 x,y,t,为公路连接的两个车站编号和时间。
【输出】
输出仅一行,包含一个整数 T,为最少的总时间。
【输入样例】
6 6
2 3 4 5 6
1 2 8
2 3 3
3 4 4
4 5 5
5 6 2
1 6 7
【输出样例】
21
【提示】
数据范围:
对于全部数据,1≤n≤50000,1≤m≤105,1<a,b,c,d,e≤n,1≤x,y≤n,1≤t≤100。
——————————————————————————————————————————————————————
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 = 200010;
static int n;
static int m;
static int[] source = new int[6];
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[6][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();
source[0] = 1;
for (int i = 1; i <= 5; i++) {
source[i] = sc.nextInt();
}
Arrays.fill(h, -1);
while (m-- > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
add(a, b, c);
add(b, a, c);
}
for (int i = 0; i < 6; i++) {
spfa(source[i], dist[i]);
}
System.out.println(dfs(1, 0, 0));
}
private static int dfs(int u, int start, int distance) {
if (u == 6) return distance;
int res = INF;
for (int i = 1; i <= 5; i++) {
if (!st[i]) {
int next = source[i];
st[i] = true;
res = Math.min(res, dfs(u + 1, i, distance + dist[start][next]));
st[i] = false;
}
}
return res;
}
private static void spfa(int start, int[] dist) {
Arrays.fill(dist, INF);
dist[start] = 0;
int hh = 0, tt = 1;
q[0] = start;
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++;
}
}
例题:340. 通信线路
摘自–“小呆呆”
单源最短路 + 二分
题目描述到有k条边可以免费升级,因此只需要求1~N的所有路径中第k + 1大的值的最小值,是最大最小值模型,因此可以使用二分求解
对于区间[0,1000001]中的某一个点x:
1、check(x)函数表示:从1走到N,最少经过的长度大于x的边数的数量是否小于等于k,若是则返回true,否则返回false
2、求出从1到N最少经过几条长度大于x的边
可以分类成:
如果边大于x,则边权看成1
如果边长小于等于x,则边权看成0
注意:
1、初始l = 0,r = 1000001的原因是:如果1号点到n号点是不连通的,最后二分出来的值一定是1000001,表示无解
2、对于只有两种边权是0,1可以使用双端队列求解,下面使用的是堆优化版Dijkstra
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Main {
static int N = 1010;
static int M = 10010 * 2;
static int m;
static int n;
static int k;
static int INF = 0x3f3f3f3f;
static boolean[] st = new boolean[N];
static int[] dist = 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 = 0;
static void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
static boolean check(int x) {
PriorityQueue<PIIs> q = new PriorityQueue<PIIs>();
Arrays.fill(st, false);
Arrays.fill(dist, INF);
q.add(new PIIs(0, 1));
dist[1] = 0;
while (!q.isEmpty()) {
PIIs p = q.poll();
int t = p.second;
int distance = p.first;
if (st[t]) continue;
st[t] = true;
for (int i = h[t]; i != -1; i = ne[i]) {
int j = e[i];
//若权值大于x则权值看成1
if (w[i] > x) {
dist[j] = Math.min(dist[j], distance + 1);
q.add(new PIIs(dist[j], j));
}
//若权值小于等于x则权值看成0
else {
dist[j] = Math.min(dist[j], distance);
q.add(new PIIs(dist[j], j));
}
}
}
if (dist[n] <= k) return true;
return false;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s1 = br.readLine().split(" ");
n = Integer.parseInt(s1[0]);
m = Integer.parseInt(s1[1]);
k = Integer.parseInt(s1[2]);
Arrays.fill(h, -1);
while (m-- > 0) {
String[] s2 = br.readLine().split(" ");
int a = Integer.parseInt(s2[0]);
int b = Integer.parseInt(s2[1]);
int c = Integer.parseInt(s2[2]);
add(a, b, c);
add(b, a, c);
}
int l = 0, r = 1000001;
while (l < r) {
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
if (r == 1000001) System.out.println("-1");
else System.out.println(r);
}
}
class PIIs implements Comparable<PIIs> {
public int first;
public int second;
public PIIs(int first, int second) {
this.first = first;
this.second = second;
}
@Override
public int compareTo(PIIs o) {
return Integer.compare(first, o.first);
}
}
例题:342. 道路与航线
【WA】了…
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int INF = Integer.MAX_VALUE >> 1;
static int N = 2510;
static int M = 150010;
static int mr;
static int mp;
static int n;
static int S;
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 = 0;
static int[] id = new int[N];
static int[] dist = new int[N];
static int[] din = new int[N];
static int bcnt;//连通块个数
static ArrayList<Integer>[] block = new ArrayList[N];
static boolean[] st = new boolean[N];
static Queue<Integer> q = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s1 = br.readLine().split(" ");
for (int i = 0; i < block.length; i++) {
block[i] = new ArrayList<>();
}
n = Integer.parseInt(s1[0]);
mr = Integer.parseInt(s1[1]);
mp = Integer.parseInt(s1[2]);
S = Integer.parseInt(s1[2]);
Arrays.fill(h, -1);
while (mr-- > 0) {
String[] s2 = br.readLine().split(" ");
int a = Integer.parseInt(s2[0]);
int b = Integer.parseInt(s2[1]);
int c = Integer.parseInt(s2[2]);
add(a, b, c);
add(b, a, c);
}
for (int i = 1; i <= n; i++) {
if (id[i] == 0) {
dfs(i, ++bcnt);
}
}
while (mp-- > 0) {
String[] s2 = br.readLine().split(" ");
int a = Integer.parseInt(s2[0]);
int b = Integer.parseInt(s2[1]);
int c = Integer.parseInt(s2[2]);
add(a, b, c);
din[id[b]]++;
}
topsort();
for (int i = 1; i <= n; i++) {
if (dist[i] > INF / 2) {
System.out.println("NO PATH");
}else {
System.out.println(dist[i]);
}
}
}
private static void topsort() {
Arrays.fill(dist, INF);
dist[S] = 0;
for (int i = 1; i <= bcnt; i++) {//遍历所有连通块
if (din[i] == 0) {//入度为零
q.add(i);
}
}
while (!q.isEmpty()) {
int t = q.poll();
dijkstra(t);
}
}
private static void dijkstra(int bid) {
Queue<Pair> heap = new PriorityQueue<>(new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.x - o2.x;
}
});
for (int ver : block[bid]) heap.add(new Pair(dist[ver], ver));
while (!heap.isEmpty()) {
Pair t = heap.poll();
int ver = t.y;
int distance = t.x;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
if (dist[j] > dist[ver] + w[i]) {
dist[j] = dist[ver] + w[i];
if (id[j] == id[ver]) heap.add(new Pair(dist[j], j));
}
if (id[j] != id[ver] && --din[id[j]] == 0) q.add(id[j]);
}
}
}
private static void dfs(int u, int bid) {
id[u] = bid;
block[bid].add(u);
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (id[j] == 0) {
dfs(j, bid);
}
}
}
private static void add(int a, int b, int c) {
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
}
class Pair{
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
例题:341. 最优贸易
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 = 100010;
static int M = 2000010;
static int n;
static int m;
static int[] w = new int[N];
static int[] hs = new int[N];
static int[] ht = new int[N];
static int[] e = new int[M];
static int[] ne = new int[M];
static int idx = 0;
static boolean[] st = new boolean[N];
static int[] cnt = new int[N];
static int INF = 0x3f3f3f3f;
static int[] dmin = new int[N];
static int[] dmax = new int[N];
static void add(int[] h, int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
//type == 0表示从1到k正向求最小值
//type == 1表示从n到k反向求最大值
static void spfa(int[] h, int[] dist, int type) {
Queue<Integer> q = new LinkedList<Integer>();
if (type == 0) {
Arrays.fill(dist, INF);
q.add(1);
dist[1] = w[1];
st[1] = true;
} else {
Arrays.fill(dist, -INF);
q.add(n);
dist[n] = w[n];
st[n] = 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 ((type == 0 && dist[j] > Math.min(dist[t], w[j])) || (type == 1 && dist[j] < Math.max(dist[t], w[j]))) {
if (type == 0) dist[j] = Math.min(dist[t], w[j]);
else dist[j] = Math.max(dist[t], w[j]);
if (!st[j]) {
q.add(j);
st[j] = true;
}
}
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s1 = br.readLine().split(" ");
n = Integer.parseInt(s1[0]);
m = Integer.parseInt(s1[1]);
String[] s2 = br.readLine().split(" ");
for (int i = 1; i <= n; i++) w[i] = Integer.parseInt(s2[i - 1]);
Arrays.fill(hs, -1);
Arrays.fill(ht, -1);
while (m-- > 0) {
String[] s3 = br.readLine().split(" ");
int a = Integer.parseInt(s3[0]);
int b = Integer.parseInt(s3[1]);
int c = Integer.parseInt(s3[2]);
add(hs, a, b);
add(ht, b, a);//建立反向边
if (c == 2) {
add(hs, b, a);
add(ht, a, b);
}
}
spfa(hs, dmin, 0);
spfa(ht, dmax, 1);
int res = -INF;
for (int i = 1; i <= n; i++) res = Math.max(res, dmax[i] - dmin[i]);
System.out.println(res);
}
}