제출 #525717

#제출 시각아이디문제언어결과실행 시간메모리
525717JosephO_oCommuter Pass (JOI18_commuter_pass)Java
24 / 100
2072 ms53040 KiB
import java.util.*; import java.io.*; public class commuter_pass { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static class Edge{ int v; long w; public Edge(int v0, long w0){v=v0;w=w0;} } static class cmp implements Comparator<Edge>{ public int compare(Edge a, Edge b){ return Long.compare(a.w,b.w); } } static long[] dijkstra(int src){ PriorityQueue<Edge> pq = new PriorityQueue(new cmp()); long dis[] = new long[n+1]; Arrays.fill(dis,Long.MAX_VALUE/3); pq.add(new Edge(src,0)); dis[src] = 0; while(!pq.isEmpty()){ Edge cur = pq.poll(); if(dis[cur.v] < cur.w)continue; for(Edge e:adj[cur.v]){ if(dis[cur.v] + e.w < dis[e.v]){ dis[e.v] = dis[cur.v] + e.w; pq.add(new Edge(e.v, dis[e.v])); } } } return dis; } static int n,m,S,T,U,V; static ArrayList<Edge> adj[]; static long disS[], disT[], disU[], disV[], dpST[], dpTS[]; static boolean vis[]; public static void main(String[] args) throws IOException { n = readInt(); m = readInt(); S = readInt(); T = readInt(); U = readInt(); V = readInt(); adj = new ArrayList[n+1]; for(int i = 1; i <= n; i++)adj[i] = new ArrayList(); for(int i = 1; i <= m; i++){int u = readInt(), v = readInt(), w = readInt(); adj[u].add(new Edge(v,w)); adj[v].add(new Edge(u,w));} disS = dijkstra(S); disT = dijkstra(T); disU = dijkstra(U); disV = dijkstra(V); Edge orddisT[] = new Edge[n], orddisS[] = new Edge[n]; for(int i = 1; i <= n; i++){orddisT[i-1]=new Edge(i, disT[i]); orddisS[i-1] = new Edge(i, disS[i]);} Arrays.sort(orddisT, new cmp()); Arrays.sort(orddisS, new cmp()); int sortdisS[] = new int[n+1], sortdisT[] = new int[n+1]; for(int i = 1; i <= n; i++){sortdisT[i]=orddisT[i-1].v; sortdisS[i]=orddisS[i-1].v;} dpST = new long[n+1]; dpTS = new long[n+1]; for(int i = 0; i <= n; i++){dpST[i]=dpTS[i]=Long.MAX_VALUE;} for(int i = 1; i <= n; i++){ int cur = sortdisT[i]; if(disS[cur]+disT[cur] != disS[T])continue; //System.out.println(cur+" ::::::::::::::::::::"); //System.out.println(cur+" ::::"); dpST[cur] = disV[cur]; for(Edge e:adj[cur]){if(disT[e.v] <= disT[cur]){ //System.out.println("In adj: "+e.v); dpST[cur] = Math.min(dpST[cur], dpST[e.v]);}} } for(int i = 1; i <= n; i++){ int cur = sortdisS[i]; if(disS[cur]+disT[cur] != disS[T])continue; dpTS[cur] = disV[cur]; for(Edge e:adj[cur]){if(disS[e.v] <= disS[cur]){dpTS[cur] = Math.min(dpTS[cur], dpTS[e.v]);}} } long ans = disU[V]; for(int i = 1; i <= n; i++){ if(dpST[i]==Long.MAX_VALUE || dpTS[i]==Long.MAX_VALUE)continue; ans = Math.min(ans, Math.min(disU[i]+dpTS[i],disU[i]+dpST[i])); } //System.out.println(Arrays.toString(dpST)); //System.out.println(Arrays.toString(dpTS)); System.out.println(ans); } static String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine().trim()); } return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readLine() throws IOException { return br.readLine().trim(); } //IF NOT IN ARR: Idx of first one < key //IF IN ARR: Idx of last occurrence static int upper_bound(int a[], int key) { int lo = 0, hi = a.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (a[mid] == key) { lo = mid + 1; } else if (a[mid] > key) { hi = mid - 1; } else { lo = mid + 1; } } return hi; } //IF NOT IN ARRR: Idx of first one > key //IF IN ARR: Idx of first occurrence static int lower_bound(int a[], int key) { int lo = 0, hi = a.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (a[mid] == key) { hi = mid - 1; } else if (a[mid] > key) { hi = mid - 1; } else { lo = mid + 1; } } return lo; } static long gcd(long x, long y) { return y == 0 ? x : gcd(y, x % y); } static long lcm(long x, long y) { return x * y / gcd(x, y); } public static int modexponent(int x, int exp, int mod) { if (exp == 0) { return 1; } int t = modexponent(x, exp / 2, mod); t = (t * t) % mod; if (exp % 2 == 0) { return t; } return (t * x % mod) % mod; } public static boolean next_permutation(long a[]) { int n = a.length; int p = n - 2; int q = n - 1; while (p >= 0 && a[p] >= a[p + 1]) { p--; } if (p < 0) { return false; } while (a[q] <= a[p]) { q--; } long t = a[p]; a[p] = a[q]; a[q] = t; for (int i = p + 1, j = n - 1; i < j; i++, j--) { t = a[i]; a[i] = a[j]; a[j] = t; } return true; } static long getSubHash(long hash[], long pow[], int l, int r) { return hash[r] - hash[l - 1] * pow[r - l + 1]; } }

컴파일 시 표준 에러 (stderr) 메시지

Note: commuter_pass.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...
#Verdict Execution timeMemoryGrader output
Fetching results...