HDU 4109 Instrction Arrangement

Instrction Arrangement


~~第一道关键路径问题,留下代码纪念,题目大意就是要完成n个任务(0~n-1),每个任务单独最快要1s,而有些任务是需要在某些任务完成后耗时**才能完成的,可多线程进行任务,问完成所有任务需要的最短时间。

拓扑排序、关键路径

/*
Qiu
in[] 任务的入度
T[]  任务最早完成时间
node edge.y y任务在某一任务完成后还需要edge.cost时间完成
v[] 动态存储所有任务的所有后续任务
Q 利用队列存储所有入度为0的任务
ans 记录完成所有任务的最早时间
*/

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
#define N 1010
int  in[N],T[N];
struct node{
    int y;
    int cost;
}edge;
vector<node>v[N];
void init(int n){
    for(int i=0;i<n;i++){
        in[i]=0;
        T[i]=1;
        v[i].clear();
    }

}
void topo(int n){
    queue<int>Q;
    for(int i=0;i<n;i++){
        if(in[i]==0){
            T[i]=1;
            Q.push(i);
        }
    }
    while(!Q.empty()){
        int x=Q.front(); Q.pop();
        for(int i=0;i<v[x].size();i++){
            int y=v[x][i].y;
            T[y]=max(T[y],T[x]+v[x][i].cost);
            in[y]--;
            if(in[y]==0)
                Q.push(y);
        }
    }
    int ans=0;
    for(int i=0;i<n;i++){
        ans=max(T[i],ans);
    }
    printf("%d\n",ans);
}
int main(){
    int x,n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        init(n);
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&x,&edge.y,&edge.cost);
            v[x].push_back(edge);
            in[edge.y]++;
        }
        topo(n);
    }
    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(); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值