[hdu 4109] Instrction Arrangement

本文介绍了一个使用记忆化搜索解决图中寻找最长路径问题的实例。通过深度优先搜索(DFS)并记录已访问节点的距离,避免重复计算,实现高效的最长路径查找。文章附带了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门
就是个关键路径嘛,我一开始dfs还写挂了,于是上网找题解,怎么清一色topsort??这还叫我怎么贺
压根就不用啊,记忆化搜索具有天然拓扑序。
怎么感觉网上好多题解都是贺来贺去的
再次被hdu的多组数据坑了一发

#include<bits/stdc++.h>
#define lson (x<<1)
#define rson (x<<1|1)
#define ll long long
#define index ind
using namespace std;
const int N=1007;
const int M=N*10;
const ll INF=1e18;
int n,m;
int cnt,Next[M],head[N],v[M],w[M];
ll dis[N];

inline void read(int &x){
    char ch=getchar();x=0;
    for(;ch<'0'||ch>'9';ch=getchar());
    for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
}

inline void add(int x,int y,int z){
    Next[++cnt]=head[x];
    head[x]=cnt;
    v[cnt]=y;
    w[cnt]=z;
}

void dfs(int x){
    if (dis[x]!=-1) return;
    dis[x]=0;
    for(int i=head[x];i!=-1;i=Next[i]){
        dfs(v[i]);
        dis[x]=max(dis[x],dis[v[i]]+w[i]);
    }
}

int main(){
    while(scanf("%d%d",&n,&m)>0){
        memset(head,-1,sizeof head);
        memset(dis,-1,sizeof dis);
        cnt=0;
        //read(n);read(m);
        for(int i=1;i<=m;i++){
            int x,y,z;
            read(x);read(y);read(z);
            add(x+1,y+1,z);
        }
        for(int i=1;i<=n;i++) dfs(i);
        ll mx=0;
        for(int i=1;i<=n;i++) mx=max(mx,dis[i]);
        cout<<mx+1<<endl;
    }
    return 0;
}
HDU 4109是一道经典的算法题目,题目名称为“Agent J”。这道题目主要考察的是图论中的最短路径算法,特别是Dijkstra算法的应用。 题目描述: 在一个有向图中,给定起点和终点,求从起点到终点的最短路径。如果存在多条最短路径,输出字典序最小的路径。 解题思路: 1. 使用Dijkstra算法计算从起点到终点的最短路径。 2. 在Dijkstra算法的基础上,使用优先队列来确保找到的路径字典序最小。 3. 使用一个数组来记录每个节点的前驱节点,以便最后可以回溯出完整的路径。 代码实现: ```java import java.util.*; public class Main { static class Edge { int to, weight; Edge(int to, int weight) { this.to = to; this.weight = weight; } } static class Node implements Comparable<Node> { int id, dist; Node(int id, int dist) { this.id = id; this.dist = dist; } @Override public int compareTo(Node other) { return this.dist - other.dist; } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); while (T-- > 0) { int n = scanner.nextInt(); int m = scanner.nextInt(); List<List<Edge>> graph = new ArrayList<>(); for (int i = 0; i <= n; i++) { graph.add(new ArrayList<>()); } for (int i = 0; i < m; i++) { int from = scanner.nextInt(); int to = scanner.nextInt(); int weight = scanner.nextInt(); graph.get(from).add(new Edge(to, weight)); } int start = scanner.nextInt(); int end = scanner.nextInt(); int[] dist = new int[n + 1]; Arrays.fill(dist, Integer.MAX_VALUE); dist[start] = 0; int[] prev = new int[n + 1]; Arrays.fill(prev, -1); PriorityQueue<Node> pq = new PriorityQueue<>(); pq.offer(new Node(start, 0)); while (!pq.isEmpty()) { Node current = pq.poll(); if (current.id == end) break; if (current.dist > dist[current.id]) continue; for (Edge edge : graph.get(current.id)) { if (dist[edge.to] > current.dist + edge.weight) { dist[edge.to] = current.dist + edge.weight; prev[edge.to] = current.id; pq.offer(new Node(edge.to, dist[edge.to])); } else if (dist[edge.to] == current.dist + edge.weight && prev[edge.to] > current.id) { prev[edge.to] = current.id; } } } if (dist[end] == Integer.MAX_VALUE) { System.out.println(-1); } else { List<Integer> path = new ArrayList<>(); int current = end; while (current != -1) { path.add(current); current = prev[current]; } Collections.reverse(path); for (int i = 0; i < path.size(); i++) { System.out.print(path.get(i) + (i < path.size() - 1 ? " " : "\n")); } } } scanner.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值