Submission #99576

#TimeUsernameProblemLanguageResultExecution timeMemory
99576psmaoCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
887 ms19204 KiB
#include <bits/stdc++.h>
using namespace std;

#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;

void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}

const int maxn = 100050;

ll dist[4][maxn]; //0-> starts from u, 1->v, 2-> s, 3-> t, 
ll f[2][maxn];
bool inq[maxn];
int S, T, U, V, n, m;
vector<pii> adj[maxn];
priority_queue<pair<ll,int> ,vector<pair<ll,int> >,greater<pair<ll,int> > > pq;

void dijkstra(int s, int k)
{
	fo(i,1,n) dist[k][i] = INF;
	pq.push(mp(0ll, s));
	dist[k][s] = 0;
	while(!pq.empty())
	{
		int h = pq.top().se; ll w = pq.top().fi; pq.pop();
		if(w < dist[k][h]) continue;
		for(auto p : adj[h]) if(dist[k][p.fi] > dist[k][h] + p.se)
		{
			dist[k][p.fi] = dist[k][h] + p.se;
			pq.push(mp(dist[k][p.fi], p.fi));
		}
	}
}
void DP(int s, int k1, int k, int x)
{
	fo(i,1,n) f[x][i] = INF;
	f[x][s] = dist[k][s];
	pq.push(mp(f[x][s], s));
	while(!pq.empty())
	{
		int h = pq.top().se; pq.pop(); inq[h] = false;
		for(auto p : adj[h]) if(dist[k1][h] + p.se == dist[k1][p.fi])
		{
			if(f[x][p.fi] > f[x][h]) 
			{
				f[x][p.fi] = min(f[x][h], dist[k][p.fi]);
				if(!inq[p.fi])
				{
					inq[p.fi] = true;
					pq.push(mp(f[x][p.fi], p.fi));
				}
			}
		}
	}
}
int main()
{
	#ifdef MPS	
		fp("1.in","r",stdin);
		fp("1.out","w",stdout);
	#endif
	sf("%d%d",&n,&m);
	sf("%d%d%d%d",&S,&T,&U,&V);
	fo(i,1,m)
	{
		int u, v, w;
		sf("%d%d%d",&u,&v,&w);
		adj[u].pb(mp(v, w));
		adj[v].pb(mp(u, w));
	}
	dijkstra(U, 0);
	dijkstra(V, 1);
	dijkstra(S, 2);
	dijkstra(T, 3);
	DP(S, 2, 1, 0);
	DP(T, 3, 1, 1);
	ll ans = INF;
	// D("%lld %lld %lld %lld\n",dist[2][S], dist[3][S], dist[2][T],f[1][3]);
	fo(i,1,n) //iterate all y
		if(dist[2][i] + dist[3][i] == dist[2][T]) // its on the shortest path from s to T
			ans = min(ans, dist[0][i] + min(f[0][i], f[1][i]));
	pf("%lld\n",min(ans,dist[0][V]));
	return 0;
}

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:86:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d%d",&n,&m);
    ^
commuter_pass.cpp:87:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d%d%d%d",&S,&T,&U,&V);
    ^
commuter_pass.cpp:91:5: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   sf("%d%d%d",&u,&v,&w);
     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...