#include <bits/stdc++.h> using namespace std; #define ll long long void…
- User
#include <bits/stdc++.h> using namespace std; #define ll long long
void dijkstra(vector<vector<pair<ll, ll>>> &adj, ll src, ll n) { priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; vector dist(n, LLONG_MAX); vector vis(n, 0);
codedist[src] = 0; pq.push({0, src}); while (!pq.empty()) { ll node = pq.top().second; ll node_dist = pq.top().first; pq.pop(); vis[node] = 1; if (node_dist > dist[node]) continue; for (auto it : adj[node]) { if (!vis[it.first]) { ll adj_node = it.first; ll adj_dist = it.second; if (adj_dist < dist[adj_node]) { dist[adj_node] = adj_dist; pq.push({dist[adj_node], adj_node}); } } } } int ans = 0; for (auto it : vis) { if (it == 0) { cout << "IMPOSSIBLE"; return; } } for (auto it : dist) { ans += it; } cout << ans;}
int main() { ll n, m; cin >> n >> m;
codevector<vector<pair<ll, ll>>> adj(n); for (ll i = 0; i < m; i++) { ll a, b, c; cin >> a >> b >> c; adj[a - 1].push_back({b - 1, c}); adj[b - 1].push_back({a - 1, c}); } dijkstra(adj, 0, n); return 0;} what is wrong
- Assistant
- User
here are n cities and m roads between them. Unfortunately, the condition of the roads is so poor that they cannot be used. Your task is to repair some of the roads so that there will be a decent route between any two cities.
For each road, you know its reparation cost, and you should find a solution where the total cost is as small as possible.
Input The first input line has two integers n and m: the number of cities and roads. The cities are numbered .
Then, there are m lines describing the roads. Each line has three integers a, b and c: there is a road between cities a and b, and its reparation cost is c. All roads are two-way roads.
Every road is between two different cities, and there is at most one road between two cities.
Output Print one integer: the minimum total reparation cost. However, if there are no solutions, print "IMPOSSIBLE".
- Assistant
- User
#include <bits/stdc++.h> using namespace std; #define ll long long
void dijkstra(vector<vector<pair<ll, ll>>> &adj, ll src, ll n) { priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; vector dist(n, LLONG_MAX); vector vis(n, 0);
codedist[src] = 0; pq.push({0, src}); while (!pq.empty()) { ll node = pq.top().second; ll node_dist = pq.top().first; pq.pop(); vis[node] = 1; if (node_dist > dist[node]) continue; for (auto it : adj[node]) { if (!vis[it.first]) { ll adj_node = it.first; ll adj_dist = it.second; if (adj_dist < dist[adj_node]) { dist[adj_node] = adj_dist; pq.push({dist[adj_node], adj_node}); } } } } int ans = 0; for (auto it : vis) { if (it == 0) { cout << "IMPOSSIBLE"; return; } } for (auto it : dist) { ans += it; } cout << ans; - Assistant
- User
#include <bits/stdc++.h> using namespace std; #define ll long long
void dijkstra(vector<vector<pair<ll, ll>>> &adj, ll src, ll n) { priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; vector dist(n, LLONG_MAX); vector vis(n, 0);
codedist[src] = 0; pq.push({0, src}); while (!pq.empty()) { ll node = pq.top().second; ll node_dist = pq.top().first; pq.pop(); vis[node] = 1; if (node_dist > dist[node]) continue; for (auto it : adj[node]) { if (!vis[it.first]) { ll adj_node = it.first; ll adj_dist = it.second; if (adj_dist < dist[adj_node]) { dist[adj_node] = adj_dist; pq.push({dist[adj_node], adj_node}); } } } } int ans = 0; for (auto it : vis) { if (it == 0) { cout << "IMPOSSIBLE"; return; } } for (auto it : dist) { ans += it; } cout << ans; - Assistant