HUAWEI Programming Contest 2024(AtCoder Beginner Contest 342) --- E - Last Train -- 题解

本文介绍了一种解决E-LastTrain问题的方法,通过动态规划和优先队列,计算每个车站到第N个车站的最晚出发时间。关键思路是先确定到达第N站的最晚时间,然后回溯计算其他车站的最晚出发时刻。
摘要由CSDN通过智能技术生成

目录

E - Last Train:

题目大意: 

思路解析:

代码实现:


 

E - Last Train:

题目大意: 

思路解析:

        计算每个车站到第N个车站最晚的出发时间,如果我们从前往后查询答案是很复杂的(并且并不显然)但是如果我们假设我们到达第N个车站的时间为无限大,那么我们就可以得到这些车站最晚多久出发能到达这个车站,得到这些车站的最晚出发信息,我们就可以往前查询连接他们的车站最晚多久出发能到达他们。

        为什么这样一定是对的,因为如果他在最晚时间都无法到达这个车站,那么他在更少的时间也无法到达。(并且这样可以简单的往前继续查询怎么最晚到达这个车站)

代码实现:

        

import java.io.*;
import java.util.*;


public class Main {
    static int inf = (int) 1e9;
    static int mod = (int) 1e9 + 7;
    static int mod9 = 998244353;
    static Vector<Node>[] g;
    static char[] s;
    static char[] a;

    public static void main(String[] args) throws IOException {
        int n = input.nextInt();
        int m = input.nextInt();
        g = new Vector[n+1];
        for (int i = 0; i < n + 1; i++) {
            g[i] = new Vector<>();
        }

        for (int i = 0; i < m; i++) {
            int l = input.nextInt();
            int d = input.nextInt();
            int k = input.nextInt();
            int c = input.nextInt();
            int A = input.nextInt();
            int B = input.nextInt();
            g[B].add(new Node(A, l, d, k, c));
        }
        long[] dp = new long[n+1];
        Arrays.fill(dp, -1);
        PriorityQueue<Pair> que = new PriorityQueue<>(new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                if (o1.val > o2.val) return -1;
                else if (o1.val < o2.val) return 1;
                return 0;
            }
        });
        que.add(new Pair((long) 4e18, n));
        while (!que.isEmpty()){
            Pair cur = que.poll();
            if (dp[cur.x] != -1) continue;
            dp[cur.x] = cur.val;
            for (int i = 0; i < g[cur.x].size(); i++) {
                Node a = g[cur.x].get(i);
                long ms = cur.val - a.c;
                if (ms >= a.l){
                    ms = Math.min((ms - a.l) / a.d, (a.k - 1) ) * a.d+ a.l;
                    que.add(new Pair(ms, a.A));
                }


            }
        }
        for (int i = 1; i < n; i++) {
            if (dp[i] == -1) out.println("Unreachable");
            else out.println(dp[i]);
        }
        out.flush();
        out.close();
        br.close();
    }
    public static class Pair{
        long val;
        int x;
        public Pair(long val, int x){
            this.val = val;
            this.x = x;
        }

    }
    public static class Node{
        int A;
        long l;
        long d;
        long k;
        long c;
        public Node(int A, long l, long d, long k, long c){
            this.A = A;
            this.l = l;
            this.d = d;
            this.k = k;
            this.c = c;
        }
    }


    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static Input input = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public char[] nextChars(){return next().toCharArray();}
        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }
    }
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Studying~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值