using System; using System.Collections.Generic; class Program { public class Node { public long id; public List link = new List(); public long tourCost; public long ticketReduce; public Node(long i) { id = i; link = new List(); tourCost = -1; ticketReduce = 0; } } public class Edge { public long cost; public Node nodeFrom; public Node nodeTo; public Edge(Node f, Node t, long c) { cost = c; nodeFrom = f; nodeTo = t; } } static void connect (Node a, Node b, long c) { a.link.Add(new Edge(a, b, c)); b.link.Add(new Edge(b, a, c)); return; } static void addQueue(List queue, Node node) { int agent = -1; int danger = queue.Count; while (danger - agent > 1) { int middle = (agent + danger) / 2; //Console.Error.WriteLine("middle " + middle + " tourcost: " + node.tourCost); if (queue[middle].tourCost > node.tourCost) danger = middle; else agent = middle; } queue.Insert(agent + 1, node); } static void Main(string[] args) { string[] input = Console.ReadLine().Split(' '); long n = long.Parse(input[0]), m = long.Parse(input[1]); List nodes = new List(); for (long i=0; i queue = new List(); nodes[0].tourCost = 0; queue.Add(nodes[0]); while(queue.Count > 0) { Node looking = queue[0]; foreach(Edge edge in looking.link) { Node node = edge.nodeTo; if (node.tourCost >= 0 && node.tourCost < looking.tourCost) continue; if (edge.cost > looking.ticketReduce) { long newCost = (long)looking.tourCost + looking.ticketReduce; if (node.tourCost < 0) { node.tourCost = newCost; node.ticketReduce = edge.cost; addQueue(queue, node); } else if (newCost < node.tourCost) { node.tourCost = newCost; node.ticketReduce = edge.cost; if(queue.Contains(node)) queue.Remove(node); addQueue(queue, node); } } else{ long newCost = looking.tourCost + edge.cost; if (node.tourCost < 0) { node.tourCost = newCost; addQueue(queue, node); } else if (newCost < node.tourCost) { node.tourCost = newCost; if(queue.Contains(node)) queue.Remove(node); addQueue(queue, node); } } } queue.Remove(looking); } long[] cost1 = new long[n]; for (int i=0; i(); queue.Add(nodes[0]); while(queue.Count > 0) { Node looking = queue[0]; foreach(Edge edge in looking.link) { Node node = edge.nodeTo; if (node.tourCost >= 0 && node.tourCost < looking.tourCost) continue; long newCost = looking.tourCost + edge.cost; //Console.Error.WriteLine(newCost); if (node.tourCost < 0) { node.tourCost = newCost; addQueue(queue, node); } else if (newCost < node.tourCost) { node.tourCost = newCost; if(queue.Contains(node)) queue.Remove(node); addQueue(queue, node); } } queue.Remove(looking); } long[] cost2 = new long[n]; for (int i=0; i