#include #include #include #include using namespace std; typedef pair IntPair; typedef vector VectorIntPair; #define INF 1000000000 int i, j, u, V, n, w, Q, counter, s, t; vector AdjList; bool change; int main() { scanf("%d", &V); AdjList.assign(V, VectorIntPair()); for (i = 0; i < V; i++) { scanf("%d", &n); while (n--) { scanf("%d %d", &j, &w); AdjList[i].push_back(IntPair(j, w)); } } counter = 0; scanf("%d", &Q); while (Q--) { scanf("%d %d", &s, &t); vector dist(V, INF); dist[s] = 0; for (i = 0; i < V-1; i++) { change = false; for (u = 0; u < V; u++) for (j = 0; j < (int)AdjList[u].size(); j++) { counter++; if (counter> 1000000) { printf("TLE because iteration counter> 1000000\n"); return 1; } IntPair v = AdjList[u][j]; if (dist[u] + v.second < dist[v.first]) { dist[v.first] = dist[u] + v.second; change = true; } } if (!change) // the optimized BellmanFord break; } printf("%d\n", dist[t]); } printf("The value of counter is: %d\n", counter); return 0; }

AltStyle によって変換されたページ (->オリジナル) /