제출 #484602

#제출 시각아이디문제언어결과실행 시간메모리
484602BackNoobCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
415 ms35296 KiB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1) 
#define TASK "task"
#define sz(s) (int) (s).size()

using namespace std;
const int mxN = 3e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
 
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) {a = b; return true;} return false;
}

struct item{
	ll d;
	int spec , u;
};

struct cmp{
	bool operator()(const item &a , const item &b) {
		return a.d > b.d;
	}
};

int n , m , S , T , U , V;
vector<pair<int , int>> adj[mxN];
ll dp[mxN][3];
ll dist1[mxN] , dist2[mxN];

void pre()
{	
	memset(dist1 , 0x3f , sizeof(dist1));
	priority_queue <pair<ll , int>, vector <pair<ll , int>> , greater<pair<ll , int>>> pq;
	pq.push({dist1[S] = 0 , S});
	while(!pq.empty()) {
		int u = pq.top().se;
		ll d = pq.top().fi;
		pq.pop();
		if(d != dist1[u]) continue;
		for(auto it : adj[u]) {
			int v = it.fi;
			int c = it.se;
			if(minimize(dist1[v] , dist1[u] + c) == true) {
				pq.push({dist1[v] , v});
			}
		}
	}

	memset(dist2 , 0x3f , sizeof(dist2));
	pq.push({dist2[T] = 0 , T});
	while(!pq.empty()) {
		int u = pq.top().se;
		ll d = pq.top().fi;
		pq.pop();
		if(d != dist2[u]) continue;
		for(auto it : adj[u]) {
			int v = it.fi;
			int c = it.se;
			if(minimize(dist2[v] , dist2[u] + c) == true) {
				pq.push({dist2[v] , v});
			}
		}
	}
}

void solve()
{
	cin >> n >> m >> S >> T >> U >> V;

	for(int i = 1 ; i <= m ; i++) {
		int u , v , c;
		cin >> u >> v >> c;
		adj[u].pb({v , c});
		adj[v].pb({u , c});
	}

	pre();

	// for(int u = 1 ; u <= n ; u++) {
	// 	for(auto it : adj[u]) {
	// 		int v = it.fi;
	// 		int c = it.se;
	// 		if(dist2[u] + dist1[v] + c == dist1[T]) {
	// 			cout << u << ' ' << v << endl;
	// 		}
	// 	}
	// }

	memset(dp , 0x3f , sizeof(dp));
	priority_queue<item , vector<item> , cmp> pq;
	pq.push({0 , 0 , U});
	dp[U][0] = 0;
	while(!pq.empty()) {
		int u = pq.top().u;
		ll d = pq.top().d;
		int spec = pq.top().spec;
		pq.pop();

		if(d != dp[u][spec]) continue;

		for(auto it : adj[u]) {
			int v = it.fi;
			int c = it.se;

			if(spec == 0) {
				if(dist1[u] + dist2[v] + c == dist1[T]) {
					if(minimize(dp[v][1] , d) == true) {
						pq.push({d , 1 , v});
					}
				}

				if(minimize(dp[v][0] , d + c) == true) {
					pq.push({d + c , 0 , v});
				}
			}
			if(spec == 1) {
				if(dist1[u] + dist2[v] + c == dist1[T]) {
					if(minimize(dp[v][1] , d) == true) {
						pq.push({d , 1 , v});
					}
				}

				if(minimize(dp[v][2] , d + c) == true) {
					pq.push({d + c , 2 , v});
				}
			}
			if(spec == 2) {
				if(minimize(dp[v][2] , d + c) == true) {
					pq.push({d + c , 2 , v});
				}
			}
		}
	}	

	ll res = min({dp[V][0] , dp[V][1] , dp[V][2]});

	memset(dp , 0x3f , sizeof(dp));
	pq.push({0 , 0 , U});
	dp[U][0] = 0;
	while(!pq.empty()) {
		int u = pq.top().u;
		ll d = pq.top().d;
		int spec = pq.top().spec;
		pq.pop();

		if(d != dp[u][spec]) continue;

		for(auto it : adj[u]) {
			int v = it.fi;
			int c = it.se;

			if(spec == 0) {
				if(dist2[u] + dist1[v] + c == dist1[T]) {
					if(minimize(dp[v][1] , d) == true) {
						pq.push({d , 1 , v});
					}
				}

				if(minimize(dp[v][0] , d + c) == true) {
					pq.push({d + c , 0 , v});
				}
			}
			if(spec == 1) {
				if(dist2[u] + dist1[v] + c == dist1[T]) {
					if(minimize(dp[v][1] , d) == true) {
						pq.push({d , 1 , v});
					}
				}

				if(minimize(dp[v][2] , d + c) == true) {
					pq.push({d + c , 2 , v});
				}
			}
			if(spec == 2) {
				if(minimize(dp[v][2] , d + c) == true) {
					pq.push({d + c , 2 , v});
				}
			}
		}
	}	
	minimize(res , min({dp[V][0] , dp[V][1] , dp[V][2]}));
	cout << res;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    //freopen(TASK".inp" , "r" , stdin);
    //freopen(TASK".out" , "w" , stdout);
    
    int tc = 1;
    //cin >> tc;
    while(tc--) {
    	solve();
    }
    return 0;
} 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...