Submission #1963083


Source Code Expand

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

class Main{

    static class Dijkstra{
        class Edge{
            int to,cost;
            Edge(int t,int c){to=t;cost=c;}
        }

        int N;
        List<Edge>[] G;
        
        Dijkstra(int N){
            this.N=N;
            G = new ArrayList[N];
            for(int i=0;i<N;++i)G[i] = new ArrayList<>();
        }

        void addEdge(int from,int to,int cost){
            G[from].add(new Edge(to, cost));
        }

        int[] dijkstra(int s){
            class QueNode{
                int v,w;
                QueNode(int v,int w){this.v=v;this.w=w;}
            }
            int[] dis = new int[N];
            Arrays.fill(dis, Integer.MAX_VALUE);
            dis[s] = 0;
            PriorityQueue<QueNode> que = new PriorityQueue<>((a,b) -> a.w - b.w);
            que.add(new QueNode(s,0));
            while(!que.isEmpty()){
                QueNode node = que.poll();
                if(dis[node.v] < node.w)continue;
                for(Edge e : G[node.v]){
                    if(dis[e.to] > dis[node.v] + e.cost){
                        dis[e.to] = dis[node.v]+e.cost;
                        que.add(new QueNode(e.to, dis[e.to]));
                    }
                }
            }
            return dis;
        }
    }

    static class Edge{
        int cost;
        int sleepCost;
        int endPoint;
        int to;
        int w;
        Edge(int end,int scost,int to,int w){
            this.endPoint=end;
            this.sleepCost=scost;
            this.to=to;
            this.w=w;
        }
    }

    static List<Edge>[] edgeList;
    static boolean[] used;

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int M = scan.nextInt();
        int src=scan.nextInt();
        int dst=scan.nextInt();

        Dijkstra djk = new Dijkstra(N);
        edgeList = new ArrayList[N];
        for(int i=0;i<N;++i)edgeList[i] = new ArrayList<>();

        while(M-->0){
            int L = scan.nextInt();
            int[] s = new int[L];
            int[] w = new int[L-1];
            for(int i=0;i<L;++i)s[i] = scan.nextInt();
            for(int i=0;i<L-1;++i)w[i] = scan.nextInt();

            for(int i=0;i<L-1;++i){
                djk.addEdge(s[i], s[i+1], w[i]);
                djk.addEdge(s[i+1], s[i], w[i]);
            }

            int sleepcost=0;
            for(int i=0;i<L-1;++i)sleepcost += w[i];
            for(int i=0;i<L-1;++i){
                edgeList[s[i]].add(new Edge(s[L-1],sleepcost,s[i+1],w[i]));
                sleepcost -= w[i];
            }

            sleepcost=0;
            for(int i=0;i<L-1;++i)sleepcost += w[i];
            for(int i=L-1;i>0;--i){
                edgeList[s[i]].add(new Edge(s[0], sleepcost,s[i-1],w[i-1]));
                sleepcost -=w[i-1];
            }
        }

        int[] dstdis = djk.dijkstra(dst);
        for(int i=0;i<N;++i){
            for(Edge e : edgeList[i]){
                e.cost = e.sleepCost + dstdis[e.endPoint];
            }
        }

        //事前にdis[src]を計算できない!(使えない辺があるから)




        class QueNode{
            int v,w;
            QueNode(int v,int w){this.v=v;this.w=w;}
        }

        int left=0;
        int right=Integer.MAX_VALUE/2;
        while(right-left>1){
            int center = (right+left)/2;
            PriorityQueue<QueNode> que = new PriorityQueue<>((a,b) -> a.w-b.w);
            int[] dis = new int[N];
            Arrays.fill(dis, Integer.MAX_VALUE);
            dis[src] = 0;
            que.add(new QueNode(src, 0));
            while(!que.isEmpty()){
                QueNode node = que.poll();
                if(node.w > dis[node.v])continue;
                for(Edge e : edgeList[node.v]){
                    if(dis[node.v] + e.cost > center)continue;
                    if(dis[node.v] + e.w < dis[e.to]){
                        dis[e.to] = dis[node.v]+e.w;
                        que.add(new QueNode(e.to, dis[e.to]));
                    }
                }
            }
            if(dis[dst] == Integer.MAX_VALUE){
                left = center;
            }else{
                right = center;
            }
        }
        System.out.println(right);
    }
}

Submission Info

Submission Time
Task C - メンテナンス明け
User inmir
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 4561 Byte
Status AC
Exec Time 1708 ms
Memory 90972 KB

Compile Error

Note: ./Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Judge Result

Set Name Subtask1 Subtask2
Score / Max Score 50 / 50 50 / 50
Status
AC × 52
AC × 6
Set Name Test Cases
Subtask1 small/00_sample00, small/00_sample01, small/00_sample02, small/10_small-0000, small/10_small-0001, small/10_small-0002, small/10_small-0003, small/10_small-0004, small/10_small-0005, small/10_small-0006, small/10_small-0007, small/10_small-0008, small/10_small-0009, small/10_small-0010, small/10_small-0011, small/10_small-0012, small/10_small-0013, small/10_small-0014, small/10_small-0015, small/10_small-0016, small/10_small-0017, small/10_small-0018, small/10_small-0019, small/30_max_small, small/40_simple_0000, small/40_simple_0001, small/40_simple_0002, small/40_simple_0003, small/40_simple_0004, small/40_simple_0005, small/40_simple_0006, small/40_simple_0007, small/40_simple_0008, small/40_simple_0009, small/40_simple_0010, small/40_simple_0011, small/40_simple_0012, small/40_simple_0013, small/40_simple_0014, small/40_simple_0015, small/40_simple_0016, small/40_simple_0017, small/40_simple_0018, small/40_simple_0019, small/90_dijkstra_killer_00, small/90_dijkstra_killer_01, small/91_tayama_killer_00, small/91_tayama_killer_01, small/91_tayama_killer_02, small/91_tayama_killer_03, small/91_tayama_killer_04, small/91_tayama_killer_05
Subtask2 large/20_large-00, large/20_large-01, large/20_large-02, large/20_large-03, large/20_large-04, large/31_max_large
Case Name Status Exec Time Memory
large/20_large-00 AC 1561 ms 79252 KB
large/20_large-01 AC 1362 ms 86496 KB
large/20_large-02 AC 1617 ms 90972 KB
large/20_large-03 AC 1568 ms 84128 KB
large/20_large-04 AC 1708 ms 88940 KB
large/31_max_large AC 662 ms 62864 KB
small/00_sample00 AC 185 ms 23636 KB
small/00_sample01 AC 182 ms 26700 KB
small/00_sample02 AC 176 ms 24264 KB
small/10_small-0000 AC 248 ms 24996 KB
small/10_small-0001 AC 238 ms 26536 KB
small/10_small-0002 AC 248 ms 24616 KB
small/10_small-0003 AC 252 ms 24844 KB
small/10_small-0004 AC 247 ms 24592 KB
small/10_small-0005 AC 249 ms 26728 KB
small/10_small-0006 AC 241 ms 25092 KB
small/10_small-0007 AC 243 ms 26980 KB
small/10_small-0008 AC 246 ms 23760 KB
small/10_small-0009 AC 254 ms 24744 KB
small/10_small-0010 AC 246 ms 23144 KB
small/10_small-0011 AC 245 ms 26992 KB
small/10_small-0012 AC 244 ms 26460 KB
small/10_small-0013 AC 229 ms 26668 KB
small/10_small-0014 AC 245 ms 25004 KB
small/10_small-0015 AC 241 ms 24888 KB
small/10_small-0016 AC 240 ms 24136 KB
small/10_small-0017 AC 249 ms 26668 KB
small/10_small-0018 AC 248 ms 24492 KB
small/10_small-0019 AC 231 ms 26480 KB
small/30_max_small AC 206 ms 26060 KB
small/40_simple_0000 AC 189 ms 24276 KB
small/40_simple_0001 AC 176 ms 24020 KB
small/40_simple_0002 AC 190 ms 25940 KB
small/40_simple_0003 AC 183 ms 24144 KB
small/40_simple_0004 AC 177 ms 24016 KB
small/40_simple_0005 AC 179 ms 26436 KB
small/40_simple_0006 AC 182 ms 26196 KB
small/40_simple_0007 AC 181 ms 26068 KB
small/40_simple_0008 AC 179 ms 24788 KB
small/40_simple_0009 AC 175 ms 26324 KB
small/40_simple_0010 AC 176 ms 24404 KB
small/40_simple_0011 AC 178 ms 24144 KB
small/40_simple_0012 AC 182 ms 24148 KB
small/40_simple_0013 AC 185 ms 28372 KB
small/40_simple_0014 AC 193 ms 23380 KB
small/40_simple_0015 AC 178 ms 24016 KB
small/40_simple_0016 AC 190 ms 26580 KB
small/40_simple_0017 AC 194 ms 26452 KB
small/40_simple_0018 AC 173 ms 24148 KB
small/40_simple_0019 AC 186 ms 24916 KB
small/90_dijkstra_killer_00 AC 190 ms 25940 KB
small/90_dijkstra_killer_01 AC 180 ms 25940 KB
small/91_tayama_killer_00 AC 195 ms 24404 KB
small/91_tayama_killer_01 AC 187 ms 23508 KB
small/91_tayama_killer_02 AC 192 ms 26564 KB
small/91_tayama_killer_03 AC 179 ms 28372 KB
small/91_tayama_killer_04 AC 179 ms 25940 KB
small/91_tayama_killer_05 AC 174 ms 24404 KB