#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_set>
#include <vector>
#include <climits>
#include <list>
#include <stack>
using namespace std;
using ll = long long;
using db = long double; // or double, if TL is tight
using llu = unsigned long long;
using str = string; // yay python!
// pairs
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdb = pair<db,db>;
#define mp make_pair
#define fr first
#define sc second
#define tcT template<class T
#define tcTU tcT, class U
// ^ lol this makes everything look weird but I'll try it
tcT> using V = vector<T>;
tcT, size_t SZ> using AR = array<T,SZ>;
using vi = V<int>;
using vb = V<bool>;
using vl = V<ll>;
using vd = V<db>;
using vs = V<str>;
using vpi = V<pii>;
using vpl = V<pll>;
using vpd = V<pdb>;
#define ms0(x) memset(x , 0, sizeof(x))
// vectors
// oops size(x), rbegin(x), rend(x) need C++17
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define ft front()
#define bk back()
#define lb lower_bound
#define ub upper_bound
tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); }
tcT> int upb(V<T>& a, const T& b) { return int(ub(all(a),b)-bg(a)); }
// loops
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
const int MOD = (int)1e9+7; // 998244353;
const int MX = (int)2e5+5;
const ll BIG = 1e18; // not too close to LLONG_MAX
const db PI = acos((db)-1);
const int dx[4] = {0,1,0,-1};
const int dy[4] = {-1,0,1,0};
long long Commuter_Pass(int N, int M, int S, int T, int U, int V, vector<vector<pair<int,int>>>roads){
ll SPS[N + 1], SPU[N + 1], SPV[N + 1];
long long dp[N + 100][2];
for (int i = 0; i<=N; i++){
SPS[i] = SPU[i] = SPV[i] = 1e18;
dp[i][0] = dp[i][1] = 1e18;
}
SPS[S] = 0;
SPU[U] = 0;
SPV[V] = 0;
priority_queue<pair<ll,int>>q;
q.push(mp(0,U));
while(!q.empty()){
ll dst = -q.top().fr;
int node = q.top().sc;
q.pop();
if (SPU[node] != dst)continue;
for (auto edge : roads[node]){
int city = edge.fr;
int cost = edge.sc;
if (SPU[city] > dst + cost){
SPU[city] = dst + cost;
q.push(mp(-SPU[city], city));
}
}
}
q.push(mp(0,V));
while(!q.empty()){
ll dst = -q.top().fr;
int node = q.top().sc;
q.pop();
if (SPV[node] != dst)continue;
for (auto edge : roads[node]){
int city = edge.fr;
int cost = edge.sc;
if (SPV[city] > dst + cost){
SPV[city] = dst + cost;
q.push(mp(-SPV[city], city));
}
}
}
priority_queue<pair<ll,pair<int,int>>>pq;
pq.push(mp(0, mp(S,S)));
int visited[N + 1];
memset(visited, 0, sizeof(visited));
while(!pq.empty()){
ll dst = -pq.top().fr;
int node = pq.top().sc.fr;
int par = pq.top().sc.sc;
pq.pop();
if (!visited[node]){
visited[node] = 1;
SPS[node] = dst;
dp[node][1] = min(dp[par][1], SPV[node]);
dp[node][0] = min(dp[par][0], SPU[node]);
for (auto next : roads[node]){
pq.push(mp(-(dst + next.sc), mp(next.fr,node)));
}
}else if (dst == SPS[node]){
if (dp[node][1] + dp[node][0] >= min(dp[par][1], SPV[node]) + min(dp[par][0], SPU[node])){
dp[node][1] = min(dp[par][1], SPV[node]);
dp[node][0] = min(dp[par][0], SPU[node]);
}
}
}
long long ans = SPU[V];
ans = min(ans, dp[T][1] + dp[T][0]);
for (int i = 1; i<=N; i++){
SPS[i] = 1e18;
dp[i][1] = 1e18;
dp[i][0]= 1e18;
}
SPS[T] = 0;
pq.push(mp(0, mp(T,T)));
memset(visited, 0, sizeof(visited));
while(!pq.empty()){
ll dst = -pq.top().fr;
int node = pq.top().sc.fr;
int par = pq.top().sc.sc;
pq.pop();
if (!visited[node]){
visited[node] = 1;
SPS[node] = dst;
dp[node][1] = min(dp[par][1], SPV[node]);
dp[node][0] = min(dp[par][0], SPU[node]);
for (auto next : roads[node]){
pq.push(mp(-(dst + next.sc), mp(next.fr,node)));
}
}else if (dst == SPS[node]){
if (dp[node][1] + dp[node][0] >= min(dp[par][1], SPV[node]) + min(dp[par][0], SPU[node])){
dp[node][1] = min(dp[par][1], SPV[node]);
dp[node][0] = min(dp[par][0], SPU[node]);
}
}
}
return min(ans, dp[S][1] + dp[S][0]);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N, M, S, T, U, V;
cin >> N >> M >> S >> T >> U >> V;
vector<vector<pair<int,int>>>edges(N + 1);
for (int i = 0; i<M; i++){
int a,b,c;
cin >> a >> b >> c;
edges[a].pb(mp(b,c));
edges[b].pb(mp(a,c));
}
cout<<Commuter_Pass(N,M,S,T,U,V,edges);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
372 ms |
24772 KB |
Output is correct |
2 |
Correct |
337 ms |
24716 KB |
Output is correct |
3 |
Correct |
326 ms |
24440 KB |
Output is correct |
4 |
Correct |
362 ms |
24828 KB |
Output is correct |
5 |
Correct |
315 ms |
22512 KB |
Output is correct |
6 |
Correct |
347 ms |
25020 KB |
Output is correct |
7 |
Correct |
342 ms |
24792 KB |
Output is correct |
8 |
Correct |
327 ms |
24636 KB |
Output is correct |
9 |
Correct |
376 ms |
24472 KB |
Output is correct |
10 |
Correct |
334 ms |
24384 KB |
Output is correct |
11 |
Correct |
111 ms |
15672 KB |
Output is correct |
12 |
Correct |
375 ms |
24576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
354 ms |
24720 KB |
Output is correct |
2 |
Correct |
346 ms |
22484 KB |
Output is correct |
3 |
Correct |
335 ms |
24500 KB |
Output is correct |
4 |
Correct |
322 ms |
22396 KB |
Output is correct |
5 |
Correct |
331 ms |
22476 KB |
Output is correct |
6 |
Correct |
324 ms |
24416 KB |
Output is correct |
7 |
Correct |
328 ms |
22480 KB |
Output is correct |
8 |
Correct |
382 ms |
22472 KB |
Output is correct |
9 |
Correct |
350 ms |
22468 KB |
Output is correct |
10 |
Correct |
344 ms |
24528 KB |
Output is correct |
11 |
Correct |
112 ms |
15568 KB |
Output is correct |
12 |
Correct |
352 ms |
24252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
2012 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
38 ms |
3536 KB |
Output is correct |
5 |
Correct |
20 ms |
2764 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
4 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
19 ms |
2892 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
372 ms |
24772 KB |
Output is correct |
2 |
Correct |
337 ms |
24716 KB |
Output is correct |
3 |
Correct |
326 ms |
24440 KB |
Output is correct |
4 |
Correct |
362 ms |
24828 KB |
Output is correct |
5 |
Correct |
315 ms |
22512 KB |
Output is correct |
6 |
Correct |
347 ms |
25020 KB |
Output is correct |
7 |
Correct |
342 ms |
24792 KB |
Output is correct |
8 |
Correct |
327 ms |
24636 KB |
Output is correct |
9 |
Correct |
376 ms |
24472 KB |
Output is correct |
10 |
Correct |
334 ms |
24384 KB |
Output is correct |
11 |
Correct |
111 ms |
15672 KB |
Output is correct |
12 |
Correct |
375 ms |
24576 KB |
Output is correct |
13 |
Correct |
354 ms |
24720 KB |
Output is correct |
14 |
Correct |
346 ms |
22484 KB |
Output is correct |
15 |
Correct |
335 ms |
24500 KB |
Output is correct |
16 |
Correct |
322 ms |
22396 KB |
Output is correct |
17 |
Correct |
331 ms |
22476 KB |
Output is correct |
18 |
Correct |
324 ms |
24416 KB |
Output is correct |
19 |
Correct |
328 ms |
22480 KB |
Output is correct |
20 |
Correct |
382 ms |
22472 KB |
Output is correct |
21 |
Correct |
350 ms |
22468 KB |
Output is correct |
22 |
Correct |
344 ms |
24528 KB |
Output is correct |
23 |
Correct |
112 ms |
15568 KB |
Output is correct |
24 |
Correct |
352 ms |
24252 KB |
Output is correct |
25 |
Correct |
20 ms |
2012 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
38 ms |
3536 KB |
Output is correct |
29 |
Correct |
20 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
4 ms |
596 KB |
Output is correct |
33 |
Correct |
2 ms |
468 KB |
Output is correct |
34 |
Correct |
19 ms |
2892 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
468 KB |
Output is correct |
40 |
Correct |
367 ms |
29240 KB |
Output is correct |
41 |
Correct |
369 ms |
28688 KB |
Output is correct |
42 |
Correct |
362 ms |
28652 KB |
Output is correct |
43 |
Correct |
156 ms |
17628 KB |
Output is correct |
44 |
Correct |
131 ms |
17740 KB |
Output is correct |
45 |
Correct |
368 ms |
25536 KB |
Output is correct |
46 |
Correct |
316 ms |
25300 KB |
Output is correct |
47 |
Correct |
388 ms |
27352 KB |
Output is correct |
48 |
Correct |
151 ms |
17180 KB |
Output is correct |
49 |
Correct |
352 ms |
28844 KB |
Output is correct |
50 |
Correct |
334 ms |
25652 KB |
Output is correct |
51 |
Correct |
313 ms |
25480 KB |
Output is correct |