(关键路径)HDU 4109 Instrction Arrangement

本文介绍了一种基于关键路径算法的任务调度方法,通过拓扑排序思想实现任务间的有序执行,并确保每个任务有足够的安全间隔时间。使用C++实现了一个具体的示例程序,展示了如何计算并找出整个任务流程中的最长路径。

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

题意

N条指令M个约束条件,每条指令之间必须有安全间隔,执行一条指令用时1s

思路

关键路径,类似拓扑排序中求最长路径。设定map容器、入度为0的队列、入度数组、到每一点最长的路。

代码

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct edge{
    int next;
    int cost;
};
int main(int argc, char *argv[])
{
    int n,m;
    while(cin>>n>>m){
        int a;
        vector<edge> map[1005];
        queue<int> zero;
        edge temp;
        int in[1005]={0},step[1005]={0};
        while(m--){
            cin>>a>>temp.next>>temp.cost;
            map[a].push_back(temp);
            in[temp.next]++;
        }
        for(int i=0;i<n;i++){
            if(in[i]==0){
                zero.push(i);
                step[i]=1;//入度为0的起点初设1,和终点用时抵消
            }
        }
        int time=0;
        while (!zero.empty()){
            int top=zero.front();
            zero.pop();
            for(int i=0,size=map[top].size();i<size;i++){
                int next=map[top][i].next;
                int cost=map[top][i].cost;
                step[next]=max(step[next],step[top]+cost);//选择最长路中关键点的最迟开始时间
                in[next]--;
                if(in[next]==0){
                    zero.push(next);
                }
            }
        }
        for(int i=0;i<n;i++){
            if(time<step[i]){
                time=step[i];
            }
        }
        cout<<time<<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、付费专栏及课程。

余额充值