1129. 热浪 - AcWing题库
这道题可以有三种方法来做,朴素版的dijkstra、堆优化版的dijkstra和spfa算法
(1)spfa算法
这里的队列用循环队列,而不是像模板那样用普通队列是因为它的队列长度不确定
import java.util.*;
public class Main{
static int N = 2510, M = 2 * 6200 + 10;
static int n, m, S, T, idx;
static int[] dist = new int[N];
static int[] q = new int[N];
static int[] h = new int[N], ne = new int[M], e = new int[M], w = new int[M];//邻接表
static boolean[] st = new boolean[N];
//邻接表存储
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);
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;
}
}
}
}
}
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.print(dist[T]);
}
}
(2) dijkstra堆优化算法
import java.util.*;
class PII implements Comparable<PII>{
int distance, num;
public PII(int distance, int num){
this.distance = distance;
this.num = num;
}
public int compareTo(PII o){
return distance - o.distance;
}
}
public class Main{
static int N = 2510, M = 2 * 6200 + 10;
static int n, m, S, T, idx;
static int[] dist = new int[N];
static int[] h = new int[N], ne = new int[M], e = new int[M], w = new int[M];//邻接表
static boolean[] st = new boolean[N];
//邻接表存储
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 dijkstra(){
PriorityQueue<PII> q = new PriorityQueue<>();//优先队列
Arrays.fill(dist, 0x3f3f3f3f);
dist[S] = 0;
q.offer(new PII(0, S));
while(!q.isEmpty()){
PII t = q.poll();
int distance = t.distance;//到起点的距离
int num = t.num;//点的编号
if(st[num]) continue;//遍历过了
st[num] = true;//标记为遍历过
for(int i = h[num]; i != -1; i = ne[i]){
int j = e[i];
if(dist[j] > distance + w[i]){
dist[j] = distance + w[i];
if(!st[j]) q.offer(new PII(dist[j], j));
}
}
}
}
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);
}
dijkstra();
System.out.print(dist[T]);
}
}
1128. 信使 - AcWing题库
这道题是让我们求出指挥部到所有点的最短距离,取一个最大值,如果存在一个点距离指挥部的距离是正无穷的话,就输出-1。
这道题虽然是让我们求单源最短路,但是我们也可以用多源最短路的Floyd算法来求。
Floyd算法
import java.util.*;
public class Main{
static int N = 110;
static int[][] dist = new int[N][N];
static int n, m;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
if(i == j) dist[i][j] = 0;
else dist[i][j] = 0x3f3f3f3f;
}
}
for(int i = 1; i <= m; i ++){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
dist[a][b] = dist[b][a] = Math.min(dist[a][b], c);//防止出现重边
}
//开始Floy算法
for(int k = 1; k <= n; k ++){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
//找到最大的那条最短路径
int res = 0;
for(int i = 1; i <= n; i ++){
if(dist[1][i] == 0x3f3f3f3f){
res = -1;
break;
}
res = Math.max(res, dist[1][i]);
}
//输出最大值
System.out.print(res);
}
}
1127. 香甜的黄油 - AcWing题库
这道题是本质上是一个多源最短路问题,让我们算出每个点到其他点的距离之和的最小值,但是这道题的数据很大,如果用Floyd算法会超时,所以这里我们可以用spfa算法算出n个点的情况,去一个最小值。
spfa算法
import java.util.*;
public class Main{
static int N = 810, M = 3000, INF = 0x3f3f3f3f;
static int n, m, p, idx;
static int[] id = new int[N];//奶牛所在牧场编号
static int[] q = new int[N];//循环队列
static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
static boolean[] st = new boolean[N];
static int[] dist = new int[N];
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 int spfa(int u){
Arrays.fill(dist, INF);
dist[u] = 0;
int hh = 0, tt = 1;
q[0] = u;
st[u] = 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;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
p = sc.nextInt();
m = sc.nextInt();
Arrays.fill(h, -1);
for(int i = 0; i < n; i ++){
id[i] = sc.nextInt();
}
for(int i = 1; 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 ++){//所有奶牛到第i个牧场的距离之和
res = Math.min(res, spfa(i));
}
System.out.print(res);
}
}
1126. 最小花费 - AcWing题库
这道题要我们求a的最少钱数,因为有100=da*w1*w2*...(其中这里的w是指1-手续费%,也就是汇率),要先da最小,那么就是说要w的乘积最大
朴素版dijkstra算法
import java.util.*;
public class Main{
static int N = 2010;
static int n, m, start, end;
static double[][] g = new double[N][N];//邻接矩阵存储边
static double[] dist = new double[N];//起点到其他点的距离
static boolean[] st = new boolean[N];
public static void dijkstra(){
//Arrays.fill(dist, 0x3f3f3f3f);我们要求的是dist的最大值,所以这里不需要这句话
dist[start] = 1;
for(int i = 1; i <= n; i ++){//遍历n次,每次找到一个
int t = -1;
for(int j = 1; j <= n; j ++){
if(!st[j] && (t == -1 || dist[j] > dist[t])){
t = j;
}
}
st[t] = true;//标记为遍历过
for(int j = 1; j <= n; j ++){
dist[j] = Math.max(dist[j], dist[t] * g[t][j]);
}
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for(int i = 0; i < m; i ++){
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
double z = (100.0 - c) / 100.0;
//这里为什么取最大值?因为原本是要求c最小的,而现在的z与c的关系是相反的,所以取最大
g[a][b] = g[b][a] = Math.max(g[a][b], z);//防止重边
}
start = sc.nextInt();
end = sc.nextInt();
dijkstra();
//我们求的dist是w乘积的最大值,如果想要da的最大值,就要用100来除dist
System.out.printf("%.8f", 100.0 / dist[end]);
}
}
920. 最优乘车 - AcWing题库
换车的最小次数=坐车的最小次数 - 1
由于这道题所有边的权重是1,所以可以直接用bfs,不需要用dijkstra、spfa什么的
import java.util.*;
import java.io.*;
public class Main{
static int N = 510;
static int n, m, res, INF = 0x3f3f3f3f;
static int[] q = new int[N];//用数组来模拟队列
static boolean[][] g = new boolean[N][N];//true表示连通,false表示不连通
static int[] dist = new int[N];//最小坐车次数
static int[] cnt = new int[N];//每条线路经过的巴士站
public static void bfs(){
Arrays.fill(dist, INF);
dist[1] = 0;
int hh = 0, tt = -1;
q[++ tt] = 1;
while(hh <= tt){
int t = q[hh ++];//取出队头
for(int i = 1; i <= n; i ++){
if(g[t][i] && dist[i] > dist[t] + 1){
dist[i] = dist[t] + 1;
q[++ tt] = i;
}
}
}
}
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] s = br.readLine().split(" ");
m = Integer.parseInt(s[0]);
n = Integer.parseInt(s[1]);
for(int i = 0; i < m; i ++){
String[] arr = br.readLine().split(" ");
int len = arr.length;//这条线路经过多少个车站
for(int j = 0; j < len; j ++) cnt[j] = Integer.parseInt(arr[j]);
for(int j = 0; j < len; j ++){//这个车站和它后面的车站之间都连通(单程)
for(int k = j + 1; k < len; k ++){
g[cnt[j]][cnt[k]] = true;//连通置为true
}
}
}
bfs();
if(dist[n] == INF) System.out.print("NO");
else System.out.print(dist[n] - 1);
}
}
903. 昂贵的聘礼 - AcWing题库
用一个虚拟远点将这些点联系起来,建图
import java.util.*;
public class Main{
static int N = 110;
static int n, m;
static int[][] g = new int[N][N];//用于建图
static int[] dist = new int[N];
static boolean[] st = new boolean[N];
static int[] level = new int[N];//每个点的等级
public static int dijkstra(int down, int up){
//因为要枚举多个区间,所以每次开始之前都要进行初始化
Arrays.fill(st, false);
Arrays.fill(dist, 0x3f3f3f3f);
dist[0] = 0;//虚拟远点的距离置为0
//朴素版的dijkstra,因为建立了一个虚拟远点,所以循环的时候要从0开始
for(int i = 0; i <= n; i ++){
int t = -1;
for(int j = 0; j <= n; j ++){
if(!st[j] && (t == -1 || dist[t] > dist[j])){
t = j;
}
}
st[t] = true;
//更新其他点的值
for(int j = 0; j <= n; j ++){
if(level[j] >= down && level[j] <= up){
dist[j] = Math.min(dist[j], dist[t] + g[t][j]);
}
}
}
return dist[1];
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
m = sc.nextInt();
n = sc.nextInt();
//初始化
for(int i = 0; i < N; i ++){
Arrays.fill(g[i], 0x3f3f3f3f);
}
for(int i = 0; i < N; i ++){
g[i][i] = 0;
}
//建图
for(int i = 1; i <= n; i ++){
int price = sc.nextInt();
level[i] = sc.nextInt();
int cnt = sc.nextInt();//有几种兑换方式
g[0][i] = price;//虚拟远点到当前点的距离(不兑换,直接购买)
for(int j = 1; j <= cnt; j ++){
int id = sc.nextInt();
int cost = sc.nextInt();
g[id][i] = cost;//兑换物品到当前点的距离
}
}
int res = 0x3f3f3f3f;
//枚举等级的左端点,找到最小的等级,因为我们一定要将第一个点包含在内
for(int i = level[1] - m; i <= level[1]; i ++) res = Math.min(res, dijkstra(i, i + m));
System.out.print(res);
}
}