Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя utkarshchauhan2812

Автор utkarshchauhan2812, история, 9 месяцев назад, По-английски

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

Your code here...
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#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;
}

Полный текст и комментарии »

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится