o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
#define INF INTMAX int minimumStressLevel(int N, vector<vector<int>>& abw, int X, int Y)…
- User
#define INF INT_MAX
int minimumStressLevel(int N, vector<vector>& abw, int X, int Y) {
codevector<vector<pair<int, int>>> graph(N + 1); for (auto& road : abw) { int a = road[0], b = road[1], w = road[2]; graph[a].push_back({b, w}); graph[b].push_back({a, w}); } priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; vector<int> dist(N + 1, INF); pq.push({0, X}); dist[X] = 0; while (!pq.empty()) { int u = pq.top().second; int stress = pq.top().first; pq.pop(); if (u == Y) return stress; for (auto& neighbor : graph[u]) { int v = neighbor.first; int w = neighbor.second; int new_stress = max(stress, w); if (new_stress < dist[v]) { dist[v] = new_stress; pq.push({new_stress, v}); } } } return -1;}//ZETA ROBIN HOOD
write it in python
- Assistantpython
def minimumStressLevel(N, abw, X, Y): from heapq import heappush, heappop # Build the graph graph = [[] for _ in range(N + 1)] # Nodes are 1-based indices for road in abw: a, b, w = road graph[a].append((b, w)) graph[b].append((a, w)) # Initialize distances and priority queue INF = float('inf') dist = [INF] * (N + 1) dist[X] = 0 pq = [] heappush(pq, (0, X)) # (stress level, node) # Modified Dijkstra's algorithm to minimize maximum edge weight along the path while pq: stress, u = heappop(pq) if u == Y: return stress for v, w in graph[u]: new_stress = max(stress, w) if new_stress < dist[v]: dist[v] = new_stress heappush(pq, (new_stress, v)) # Return -1 if Y is not reachable from X return -1