structEdge { int to, next, w; } e[Maxm]; int c1, head[Maxn]; inlinevoidadd_edge(int u, int v, int w){ e[c1].to = v; e[c1].w = w; e[c1].next = head[u]; head[u] = c1++; }
inlinevoidAdd_edge(int u, int v, int w){ for (int i = 1; i <= 50; ++i) add_edge(u + i * n, v, (i + w) * (i + w)); add_edge(u, v + w * n, 0); }
structQueue { int k, d;
friendbooloperator < (const Queue &u, const Queue &v) { return u.d > v.d; } }; bool vis[Maxn]; int dis[Maxn]; priority_queue<Queue> Q; voiddijkstra(int s){ fill(dis, dis + Maxn, INF); dis[s] = 0; Q.push({ s, 0 }); while (!Q.empty()) { int u = Q.top().k; Q.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to, w = e[i].w; if (dis[v] > dis[u] + w) { dis[v] = dis[u] + w; Q.push({ v, dis[v] }); } } } }
intmain(){ fill(head, head + Maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= m; ++i) { int x, y, z; cin >> x >> y >> z; Add_edge(x, y, z); Add_edge(y, x, z); } dijkstra(1); for (int i = 1; i <= n; ++i) cout << (dis[i] == INF ? -1 : dis[i]) << " \n"[i == n]; return0; }