Submission #489792

#TimeUsernameProblemLanguageResultExecution timeMemory
489792rainliofficialRobot (JOI21_ho_t4)Java
0 / 100
3082 ms511420 KiB
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, Integer>[] 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 ArrayList<>(); 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); addEdge(e, s, color, cost); } // 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<>(); pq.add(new State(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; } for (Edge e : arr[curr.node]){ 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.cost, curr.cost + e.cost, e.cost, e.id); } } long ans = Long.MAX_VALUE; for (int c : dist[n-1].keySet()){ ans = Math.min(dist[n-1].get(c), ans); } if (ans == Long.MAX_VALUE) ans = -1; System.out.println(ans); } public static void addEdge(int ind, int to, int color, int cost){ if (!recolor[ind].containsKey(color)){ // arr[ind].put(color, new ArrayList<>()); recolor[ind].put(color, 0); } // arr[ind].get(color).add(new Edge(to, color, cost)); recolor[ind].put(color, recolor[ind].get(color) + cost); } public static void addState(PriorityQueue<State> pq, int ind, int prevColor, long newDist, int prevEdgeCost, int id){ // Return true if element added, or false if not added 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 (stderr)

Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...