Comments

Just confused that why did it fail only for one of the test cases and not the rest.

Thanks Pirate_King!!

Can someone please help me understand why my code is giving RE for just 1 test case of Problem D. All other test cases are AC.

#include <bits/stdc++.h>
using namespace std;

#define int long long

int32_t main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n;
	cin >> n;

	vector<vector<pair<int, int>>> v(n);

	for (int i = 0; i < n; i++)
	{
		int a, b, x;
		cin >> a >> b >> x;
		x--;
		v[i].push_back({a, i + 1});
		v[i].push_back({b, x});
	}

	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	vector<int> dist(n, LONG_MAX);
	vector<bool> visited(n, false);

	pq.push({0, 0});
	dist[0] = 0;
	while (!pq.empty())
	{
		pair<int, int> p = pq.top();
		pq.pop();

		int x = p.second;

		if (visited[x])
		{
			continue;
		}
		// cout << "reached\n";
		visited[x] = true;

		// for (auto r : v[x])
		// {
		// 	cout << r.first << " " << r.second << '\n';
		// }

		for (int i = 0; i < v[x].size(); i++)
		{
			int to = v[x][i].second;
			int weight = v[x][i].first;
			if (dist[x] + weight < dist[to])
			{
				dist[to] = dist[x] + weight;
				pq.push({dist[to], to});
			}
		}
	}

	cout << dist[n - 1];
}

+1

On ErrichtoAtCoder Grand Contest 047, 6 years ago
0

I am new to coding and I cannot understand how Trie has been implemented in the question B editorial. Can someone please guide me through it?