Please read the new rule regarding the restriction on the use of AI tools. ×

TLE is shortest routes 1 of cses

Revision en1, by utkarshchauhan2812, 2024-01-04 17:54:01

i am having trouble finding out the reason for tle in my code(for the last case of the problem shortest routes 1 of cses:https://cses.fi/problemset/task/1671/) below is my code for the same

include <bits/stdc++.h>

using namespace std; using ll = long long; using vi = vector;

define pb push_back

define rsz resize

define all(x) begin(x), end(x)

define sz(x) (int)(x).size()

using pi = pair<int, int>;

define f first

define s second

define mp make_pair

void setIO(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); if (sz(name)) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } }

int main() { ll n, m; cin >> n >> m;

vector<pair<ll, ll>>adj[n + 1];
for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;

    adj[a].push_back({b, c});
    // adj[b].push_back({a, c});
}
vector<ll>distance(n + 1, LLONG_MAX);
distance[1] = 0;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>q;
q.push({0, 1});
while (!q.empty()) {
    ll d = q.top().first;
    int node = q.top().second; 
    q.pop();
    for (auto i : adj[node]) {
       int it = i.first;
       int wt = i.second;
       if (d + wt < distance[it]) {
         distance[it] = d + wt;
         q.push({distance[it], it});
       }
    }
}
for (int i = 1; i <= n; i++)cout << distance[i] << " ";
return 0;

}

Tags c++, dijkstra, graphs

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English utkarshchauhan2812 2024-01-04 17:55:30 41
en1 English utkarshchauhan2812 2024-01-04 17:54:01 1436 Initial revision (published)