import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.PriorityQueue; import java.util.StringTokenizer;
public class boj1848 { static int N, M; static long[] D; static List<Node>[] route; static class Node implements Comparable<Node> { int e; long v; String visit = "1"; int vcnt = 0; public Node(int e, int v) { this.e = e; this.v = v; } public Node(int e, long v, String visit, int vcnt) { this.e = e; this.v = v; this.visit = visit; this.vcnt = vcnt; } public int getVcnt(){ return vcnt; } public String getVisit() { return visit; } public boolean isVisited(int e){ int idx = visit.indexOf(""+e); if(idx > -1) return true; else return false; } @Override public int compareTo(Node o) { return Long.compare(this.v, o.v); } @Override public String toString() { getVcnt(); return "Node [e=" + e + ", v=" + v + ", visit=" + visit + ", vcnt=" + vcnt + "]"; } } public static void main(String[] args) throws IOException { System.setIn(new FileInputStream("res/input1848.txt")); // System.setIn(new FileInputStream("res/out.txt")); long Start = System.currentTimeMillis(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine(), " "); N = Integer.parseInt(st.nextToken()); // number of room M = Integer.parseInt(st.nextToken()); // number of cave route = new List[N+1]; D = new long[N+1]; int a, b, c, d; for (int i = 1; i < M+1; i++) { st = new StringTokenizer(br.readLine(), " "); a = Integer.parseInt(st.nextToken()); b = Integer.parseInt(st.nextToken()); c = Integer.parseInt(st.nextToken()); d = Integer.parseInt(st.nextToken()); if(route[a]==null) route[a] = new ArrayList<Node>(); if(route[b]==null) route[b] = new ArrayList<Node>(); route[a].add(new Node(b, c)); route[b].add(new Node(a, d)); } for (int i = 1; i < N+1; i++) { D[i] = Long.MAX_VALUE; } PriorityQueue<Node> pq = new PriorityQueue<Node>(); Node nd=null; pq.add(new Node(1, 0)); boolean start = false; long min = Long.MAX_VALUE; while (!pq.isEmpty()) { nd = pq.poll(); // System.out.println(nd); if(route[nd.e]==null) continue; if(nd.e==1){ // if(nd.getVcnt()==N) break; if(!start) start = true; else { if(nd.vcnt>2 && min>nd.v) min = nd.v; continue; } } for (Node node : route[nd.e]) { if(node.e==1){ // int size = nd.visit.split("1,").length; // if(size==2) pq.add(new Node(node.e, node.v + nd.v,nd.visit+","+node.e, nd.vcnt+1)); } else if(!nd.isVisited(node.e)) if(D[node.e]>node.v + nd.v) { D[node.e]=node.v + nd.v; pq.add(new Node(node.e, D[node.e],nd.visit+","+node.e, nd.vcnt+1)); } } } // System.out.println(nd.v); System.out.println(min); System.out.println("Total : " + (System.currentTimeMillis()-Start)+" ms"); }
} |