// CERC 2012 // Problem F: Farm and Factory // Too slow solution using Bellman-Ford algorithm instead of Dijkstra's. // Author: Adam Polak #include #include #include #include using namespace std; const int N = 100000; const int Q = (1 << 17) - 1; int n; vector > graph[N]; long long d1[N], d2[N], x[N]; bool in_q[N]; queue q; void Bellman(int source, long long d[]) { for (int i=0; i new_d) { d[v] = new_d; if (!in_q[v]) { in_q[v] = true; q.push(v); } } } } } int main() { int Z; scanf("%d", &Z); while (Z--) { int m; scanf("%d%d", &n, &m); while (m--) { int a, b; long long c; scanf("%d%d%lld", &a, &b, &c); a--; b--; graph[a].push_back(make_pair(b, c)); graph[b].push_back(make_pair(a, c)); } Bellman(0, d1); Bellman(1, d2); long long result = 0; for (int k = 0; k < 2; ++k) { for (int i=0; i > temp; swap(temp, graph[i]); } } return 0; }