#include <bits/stdc++.h> using namespace std; int cnt = 0; int mindistance(vector<int>…
- User
#include <bits/stdc++.h> using namespace std; int cnt = 0; int min_distance(vector &dist, vector &vis, int n) { int min = 100000; int min_index = -1;
codefor (int v = 1; v <= n; v++) { if (vis[v] == 0 && dist[v] <= min) { min = dist[v], min_index = v; } } return min_index;} void dijkstra(vector<vector> &graph, int src, int n, vector &dist) { vector vis(n + 1, 0);
codefor (int i = 1; i <= n; i++) dist[i] = 100000; dist[src] = 0; for (int count = 0; count < n; count++) { int u = min_distance(dist, vis, n); if (u == -1) break; vis[u] = 1; for (int v = 1; v <= n; v++) { if (vis[v] == 0 && graph[u][v] != 100000 && dist[u] != 100000 && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; } } }}
void add_edge(vector<vector> &adj, int u, int v, int l) { if (u >= 1 && u <= adj.size() - 1 && v >= 1 && v <= adj.size() - 1) { adj[u][v] = l; adj[v][u] = l; } }
void func(vector<vector> &adj1, vector &dist1, vector &adj2, int n) {
code// int cnt = 0; for (int i = 1; i <= n; i++) { cout << "adj2[i] is " << adj2[i]<< endl; if (adj2[i]!=100000 && dist1[i] <= adj2[i]) { cnt++; } else { cout << "chsnging dist of " << i << " to " << adj2[i] << "from"<< dist1[i]<< endl; dist1[i] = adj2[i]; add_edge(adj1, 1, i, dist1[i]); cout << "changed dist of " << i << " is " << dist1[i] << endl; dijkstra(adj1, 1, n, dist1); } for (int i = 0; i < dist1.size(); i++) { cout << i << "dist 1 array" << dist1[i] << endl; } cout<<"node is "<<i<<endl; cout<<"count is " <<cnt<<endl; - Assistant
- User
but this code works for that hidden case , can you analyse what might be wrong in my original code please say in breief dont talk toom much
- Assistant
Given your request for a concise analysis, focusing on differences that could lead to discrepancies in handling hidden test cases, here are critical points to consider:
-
Global vs. Local Variables: Using global variables (like
cnt) can lead to unexpected behavior, especially if your code is called multiple times or if the online judge runs multiple test cases in the same execution environment. Ensure that variables are reset or used appropriately within their scope.
-
- User
its failing on dmoj c++ 17
- Assistant
- User
4 4 2 2 3 2 2 4 8 4 3 3 1 4 5 3 7 2 9 this test case is failing
- Assistant
- User
fix my code
- Assistant