目录
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());
}
}
}