Submission #1087186

#TimeUsernameProblemLanguageResultExecution timeMemory
1087186quangminh412Commuter Pass (JOI18_commuter_pass)C++14
0 / 100
127 ms44444 KiB
#include <bits/stdc++.h>
using namespace std;

/*
  John Watson
  https://codeforces.com/profile/quangminh98

  Mua Code nhu mua Florentino !!
*/

#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

const ll oo = 1e18;
const int maxn = 1e5 + 9;

struct Near
{
	int tar, id;
	ll w;
	
	Near(int tar, int id, ll w) : tar(tar), id(id), w(w) {}
};

struct Edge
{
	int u, v;
	ll w;
	
	Edge(int u, int v, ll w) : u(u), v(v), w(w) {}
};

vector<Near> adj[maxn];
vector<Edge> edges;
int state[maxn];
int n, m, s, t, u, v;

void dijkstra2(int s)
{
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	vector<ll> dist(n + 2, oo);
	pq.push({dist[s] = 0, s});
	
	while (!pq.empty())
	{
		int u = pq.top().second;
		ll w = pq.top().first;
		pq.pop();
		
		if (dist[u] != w) continue;
		
		for (Near nxt : adj[u])
		{
			int v = nxt.tar;
			int id = nxt.id;
			ll ww = nxt.w;
			
			if (dist[v] > dist[u] + (state[id] == 1 ? 0 : ww))
				pq.push({dist[v] = dist[u] + (state[id] == 1 ? 0 : ww), v});		
		}
	}
	
	cout << dist[v] << '\n';
}

void dijkstra1(int s)
{
	priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
	vector<pair<int, int>> prev(n + 2, {0, 0});
	vector<ll> dist(n + 2, oo);
	pq.push({dist[s] = 0, s});
	
	while (!pq.empty())
	{
		int u = pq.top().second;
		ll w = pq.top().first;
		pq.pop();
		
		if (dist[u] != w) continue;
		
		for (Near nxt : adj[u])
		{
			int v = nxt.tar;
			ll ww = nxt.w;
			int id = nxt.id;
			
			if (dist[v] > dist[u] + ww)
			{
				prev[v] = {u, id};
				pq.push({dist[v] = dist[u] + ww, v});
			}
		}
	}
	
	while (t != s)
	{
		state[prev[t].second] = 1;
		t = prev[t].first;
	}
}

signed main()
{
	if (fopen("test.inp", "r"))
	{
		freopen("test.inp", "r", stdin);
		freopen("test.out", "w", stdout);
	}
	faster();

	cin >> n >> m >> s >> t >> u >> v;
	for (int i = 0; i < m; i++)
	{
		int u, v; cin >> u >> v;
		ll w; cin >> w;
		adj[u].emplace_back(v, i, w);
		adj[v].emplace_back(u, i, w);
		edges.emplace_back(u, v, w);
	}

	dijkstra1(s);
	dijkstra2(u);

	return 0;
}

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:106:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  106 |   freopen("test.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:107:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  107 |   freopen("test.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...