AcWing 3305. 作物杂交 java spfa
✨ 原题链接
🤠 题目分析
dijkstra 算法:贪心
Bellman-ford:BFS优化后就是 spfa
spfa算法:DP
floyd:DP
😋 spfa + 滚动数组优化空间
import java.io.*;
import java.util.*;
public class Main
{
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
static int N = 2010;
static int M = 200010;
static int n, m, k, t;
// 邻接表存图
static int[] w = new int[N];// 作物生长周期
static int[] h = new int[N];
static int[] e = new int[M];
static int[] target = new int[M];// 作物可以合成的目标作物
static int[] ne = new int[M];
static int idx;
static int[] dist = new int[N];//存最短路径的值
static Queue<Integer> q = new LinkedList<>();//队列,用于记录最短路径被改变的点
static boolean[] st = new boolean[N];
public static void main(String[] args) throws IOException
{
String[] split = in.readLine().split(" ");
n = Integer.parseInt(split[0]);
m = Integer.parseInt(split[1]);
k = Integer.parseInt(split[2]);
t = Integer.parseInt(split[3]);
// 生长周期
String[] split2 = in.readLine().split(" ");
for (int i = 1; i <= n; i++)//注意:1-n 号种子,下标从 1 开始
w[i] = Integer.parseInt(split2[i - 1]);
// 已存在种子
Arrays.fill(dist, 0x3f3f3f3f);
String[] split3 = in.readLine().split(" ");
for (int i = 0; i < m; i++)
{
int x = Integer.parseInt(split3[i]);
dist[x] = 0;
q.add(x);
st[x] = true;
}
Arrays.fill(h, -1);
for (int i = 0; i < k; i++)
{
String[] split4 = in.readLine().split(" ");
int a = Integer.parseInt(split4[0]);
int b = Integer.parseInt(split4[1]);
int c = Integer.parseInt(split4[2]);
add(a, b, c);
add(b, a, c);
}
spfa();
out.write(dist[t] + "");
out.flush();
}
private static void spfa()
{
while (q.size() != 0)
{
int x = q.poll();
st[x] = false;
for (int i = h[x]; i != -1; i = ne[i])
{
int y = e[i];
int z = target[i];
if (dist[z] > Math.max(dist[x], dist[y]) + Math.max(w[x], w[y]))
{
// 子作物各自的合成时间的最大值 + 子作物生长周期的最大值(合成 z 需要的时间)
dist[z] = Math.max(dist[x], dist[y]) + Math.max(w[x], w[y]);
if (!st[z])
{
q.add(z);
st[z] = true;
}
}
}
}
}
/**
* a -> b = c 在 a 链表后边加上可以 一起杂交的作物,顺便把可以生成的作物也存在 b 对应下标的 target 数组中
*
* @param a
* @param b
* @param c
*/
private static void add(int a, int b, int c)
{
e[idx] = b;
target[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
}
🤠 dijkstra
未完待续……
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】