import std.stdio; import std.array; import std.conv; import std.container; import std.math; struct Edge{ int to; long weight; bool animal; } void main(){ auto l = readln.split.to!(int[]); auto n = l[0]; auto m = l[1]; auto e = new Edge[][n]; for(auto i = 0; i < m; i++){ l = readln.split.to!(int[]); auto a = l[0] - 1; auto b = l[1] - 1; auto c = l[2]; auto x = (l[3] == 1); auto edge = Edge(b, c, x); e[a] ~= edge; edge = Edge(a, c, x); e[b] ~= edge; } auto distance = new long[n]; distance[] = long.max; auto pq = new BinaryHeap!(Array!Edge, (a, b) => (a.weight > b.weight)); auto edge = Edge(n - 1, 0, false); pq.insert(edge); while(!pq.empty){ edge = pq.front; pq.removeFront; if(edge.animal){ if(edge.weight < distance[edge.to]){ distance[edge.to] = edge.weight; }else{ continue; } }else{ if(edge.weight < distance[edge.to] - 10L ^^ 15){ distance[edge.to] = edge.weight + 10L ^^ 15; }else{ continue; } } foreach(c; e[edge.to]){ auto d = Edge(c.to, edge.weight + c.weight, edge.animal || c.animal); pq.insert(d); if(!edge.animal && c.animal){ d = Edge(c.to, edge.weight + c.weight * 2, edge.animal || c.animal); pq.insert(d); } } } stderr.writeln(distance); for(auto i = 0; i < n - 1; i++){ distance[i].writeln; } }