Submission #59016

#TimeUsernameProblemLanguageResultExecution timeMemory
59016YaDon4ickCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
861 ms28388 KiB
//By Don4ick
//#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;

#define forn(i, n) for (int i = 1; i <= n; i++)
#define pb push_back
#define all(x) x.begin(), x.end()
#define y1 qwer1234

const double PI = acos(-1.0);
const int DIR = 4;
const int X[] = {1, 0, -1, 0};
const int Y[] = {0, 1, 0, -1};

const int N = (int)1e5 + 228;
const ll INF = (ll)1e18 + 228;

using namespace std;

int n, m;
vector < pair < int, int > > g[N]; 
vector < ll > d1, d2;
vector < int > p;
ll dist;

vector < ll > dijkstra(int start)
{
	vector < ll > d(n, INF);
	d[start] = 0;
	priority_queue < pair < ll, int > > q;
	for (int v = 0; v < n; v++)	
		q.push({-d[v], v});
	vector < bool > was(n);
	while(!q.empty())
	{
		pair < ll, int > p = q.top();
		q.pop();
		int v = p.second;
	    if (was[v])
	    	continue;
		was[v] = true;
		for (auto e : g[v])
		{
			int to = e.first, cost = e.second;
			if (d[to] > d[v] + cost)
			{
				d[to] = d[v] + cost;
				q.push({-d[to], to});
			}
		}
	}
	return d;	
}

ll sol(int s, int t, const vector < ll > & d3, const vector < ll > & d4)
{
	vector < ll > dp(n, INF);
	ll res = d3[t];
	for (int i = (int)n - 1; i >= 0; i--)
	{
		int v = p[i];
		if (d1[v] + d2[v] != dist)
			continue;
		dp[v] = d4[v];
		for (auto e : g[v])
		{
			int to = e.first, cost = e.second;
			if (d1[v] + cost + d2[to] != dist)
				continue;
			dp[v] = min(dp[v], dp[to]);			
		}	
		res = min(res, dp[v] + d3[v]);
	}	
	return res;
} 

bool cmp(int x, int y)
{
	return  d1[x] < d1[y];
}

int main()
{
	//ios_base::sync_with_stdio(false);
	//cin.tie(NULL);
	//cout.tie(NULL);

	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);

	//~read
	int s1, t1, s2, t2;
	scanf("%d%d\n%d%d\n%d%d", &n, &m, &s1, &t1, &s2, &t2);
	s1--, t1--, s2--, t2--;
	forn(i, m)
	{
		int v, u, c;
		scanf("%d%d%d", &v, &u, &c);
		v--, u--;
		g[v].pb({u, c});
		g[u].pb({v, c});
	}
	//~solve
	d1 = dijkstra(s1), d2 = dijkstra(t1);
	vector < ll > d3 = dijkstra(s2), d4 = dijkstra(t2);
	dist = d1[t1];
	p = vector < int > (n);
	for (int i = 0; i < n; i++)
		p[i] = i;
	sort(all(p), &cmp);
	cout << min(sol(s2, t2, d3, d4), sol(t2, s2, d4, d3)) << endl;

	return 0;
}

Compilation message (stderr)

commuter_pass.cpp: In function 'std::vector<long long int> dijkstra(int)':
commuter_pass.cpp:44:6: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
      if (was[v])
      ^~
commuter_pass.cpp:46:3: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   was[v] = true;
   ^~~
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d\n%d%d\n%d%d", &n, &m, &s1, &t1, &s2, &t2);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d", &v, &u, &c);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...