Submission #489799

# Submission time Handle Problem Language Result Execution time Memory
489799 2021-11-24T17:45:23 Z rainliofficial Robot (JOI21_ho_t4) Java 11
0 / 100
3000 ms 204068 KB
import java.io.*;
import java.util.*;

public class Main {
    static int n, m;
    // static ArrayList<Edge>[] arr;
    static HashMap<Integer, Long>[] dist;
    static HashMap<Integer, ArrayList<Edge>>[] arr;
    static HashMap<Integer, Long>[] recolor; // Stores the cost to recolor all edges of a certain color at a node

    public static void main(String[] args) throws IOException{
        BufferedReader file = new BufferedReader(new InputStreamReader(System.in));
        // BufferedReader file = new BufferedReader(new FileReader("file.in"));
        // PrintWriter outfile = new PrintWriter (new BufferedWriter(new FileWriter("Robot.out")));
        StringTokenizer st = new StringTokenizer(file.readLine());
        n = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        arr = new HashMap[n];
        // arr = new ArrayList[n];
        recolor = new HashMap[n];
        dist = new HashMap[n];
        recolor  = new HashMap[n];
        for (int i=0; i<n; i++){
            arr[i] = new HashMap<>();
            recolor[i] = new HashMap<>();
            dist[i] = new HashMap<>();
            recolor[i] = new HashMap<>();
        }
        for (int i=0; i<m; i++){
            st = new StringTokenizer(file.readLine());
            int s = Integer.parseInt(st.nextToken())-1;
            int e = Integer.parseInt(st.nextToken())-1;
            int color = Integer.parseInt(st.nextToken());
            int cost = Integer.parseInt(st.nextToken());
            // arr[s].add(new Edge(e, color, cost, i));
            // arr[e].add(new Edge(s, color, cost, i));
            addEdge(s, e, color, cost, i);
            addEdge(e, s, color, cost, i);
        }
        // Do Dijkstra on expanded states. State = current node, the original color of the edge we just walked through,
        // (if we recolored it). Total N+M states. 
        PriorityQueue<State> pq = new PriorityQueue<>();
        addState(pq, 0, -1, 0, 0, -1);
        while (!pq.isEmpty()){
            State curr = pq.poll();
            if (dist[curr.node].containsKey(curr.prevColor) && dist[curr.node].get(curr.prevColor) != curr.cost){
                continue; 
            }
            if (curr.node == n-1){
                System.out.println(curr.cost);
                System.exit(0);
            }
            // if (curr.prevColor == -1){
                for (int cc : arr[curr.node].keySet()){
                    for (Edge e : arr[curr.node].get(cc)){
                        if (!recolor[curr.node].containsKey(e.color) || e.id == curr.prevEdgeId){
                            continue;
                        }
                        long nc = curr.cost + recolor[curr.node].get(e.color) - e.cost;
                        if (curr.prevColor == e.color){
                            nc -= curr.prevEdgeCost;
                        }
                        addState(pq, e.to, -1, nc, e.cost, e.id);
                        addState(pq, e.to, e.color, curr.cost + e.cost, e.cost, e.id);
                    }
                }
            // }else{
            //     for (Edge e : arr[curr.node].get(curr.prevColor)){
            //         if (!recolor[curr.node].containsKey(e.color) || e.id == curr.prevEdgeId){
            //             continue;
            //         }
            //         long nc = curr.cost + recolor[curr.node].get(e.color) - e.cost;
            //         if (curr.prevColor == e.color){
            //             nc -= curr.prevEdgeCost;
            //         }
            //         addState(pq, e.to, -1, nc, e.cost, e.id);
            //         addState(pq, e.to, e.color, curr.cost + e.cost, e.cost, e.id);
            //     }
            // }
        }
        System.out.println(-1);
    }
    public static void addEdge(int ind, int to, int color, int cost, int id){
        if (!recolor[ind].containsKey(color)){
            arr[ind].put(color, new ArrayList<>());
            recolor[ind].put(color, 0L);
        }
        arr[ind].get(color).add(new Edge(to, color, cost, id));
        recolor[ind].put(color, recolor[ind].get(color) + (long)cost);
    }
    public static void addState(PriorityQueue<State> pq, int ind, int prevColor, long newDist, int prevEdgeCost, int id){ 
        if (!dist[ind].containsKey(prevColor) || dist[ind].get(prevColor) > newDist){
            dist[ind].put(prevColor, newDist);
            pq.add(new State(ind, prevColor, newDist, prevEdgeCost, id));
        }
    }
    static class State implements Comparable<State>{
        int node, prevColor, prevEdgeCost, prevEdgeId;
        long cost;
        public State(int node, int prevColor, long cost, int prevEdgeCost, int prevEdgeId){
            this.node = node;
            this.prevColor = prevColor;
            this.cost = cost;
            this.prevEdgeCost = prevEdgeCost;
            this.prevEdgeId = prevEdgeId;
        }
        public int compareTo(State o){
            return Long.compare(this.cost, o.cost);
        }
    }
    static class Edge{
        int to, color, cost, id;
        public Edge(int to, int color, int cost, int id){
            this.to = to;
            this.color = color;
            this.cost = cost;
            this.id = id;
        }
    }
}

Compilation message

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# Verdict Execution time Memory Grader output
1 Correct 63 ms 8208 KB Output is correct
2 Correct 67 ms 8368 KB Output is correct
3 Correct 58 ms 8544 KB Output is correct
4 Correct 61 ms 8356 KB Output is correct
5 Correct 84 ms 8372 KB Output is correct
6 Correct 68 ms 8144 KB Output is correct
7 Correct 197 ms 11920 KB Output is correct
8 Correct 91 ms 8636 KB Output is correct
9 Correct 485 ms 20812 KB Output is correct
10 Correct 439 ms 20532 KB Output is correct
11 Correct 288 ms 14240 KB Output is correct
12 Incorrect 250 ms 14268 KB Output isn't correct
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1134 ms 65676 KB Output is correct
2 Correct 863 ms 48100 KB Output is correct
3 Correct 840 ms 42416 KB Output is correct
4 Correct 741 ms 44092 KB Output is correct
5 Execution timed out 3067 ms 204068 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 63 ms 8208 KB Output is correct
2 Correct 67 ms 8368 KB Output is correct
3 Correct 58 ms 8544 KB Output is correct
4 Correct 61 ms 8356 KB Output is correct
5 Correct 84 ms 8372 KB Output is correct
6 Correct 68 ms 8144 KB Output is correct
7 Correct 197 ms 11920 KB Output is correct
8 Correct 91 ms 8636 KB Output is correct
9 Correct 485 ms 20812 KB Output is correct
10 Correct 439 ms 20532 KB Output is correct
11 Correct 288 ms 14240 KB Output is correct
12 Incorrect 250 ms 14268 KB Output isn't correct
13 Halted 0 ms 0 KB -