이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/*
ID: swagchicken1
PROG:
LANG: C++11
*/
#include <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <climits>
#include <chrono>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
template <typename T>
void pr(vector<T> &v) {
FOR(i, 0, sz(v)) cout << v[i] << " ";
cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
cout << first << " ";
pr(rest...);
cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
const ll MOD = 1000000007;
#define inf 1e18;
#define INF INT_MAX
//#define DEBUG
long double PI = 4*atan(1);
long double eps = 1e-9;
int n,m, S,T,U,V;
vector<vector<pair<int, ll>>> adj;
vector<ll> distS, distT, distU, distV;
ll ans;
void dijkstra(int start, vector<ll>& dist) {
FOR(i,0,n) dist[i] = 1e18;
dist[start] = 0;
set<pair<ll, int>> pq;
pq.insert({0, start});
while(!pq.empty()) {
pair<ll, int> curr = *pq.begin();
pq.erase(pq.find(curr));
int u = curr.ss;
ll d = curr.ff;
for(pair<int, ll> nxt : adj[u]) {
int v = nxt.ff; ll w = nxt.ss;
if(d + w < dist[v]) {
if(pq.find({dist[v], v}) != pq.end()) pq.erase(pq.find({dist[v], v}));
dist[v] = d+w;
pq.insert({d + w, v});
}
}
}
}
void search(int start) {
set<pair<ll, int>> pq;
vector<ll> minUA(n);
vector<ll> minVA(n);
vector<ll> dist(n);
FOR(i,0,n) {
minUA[i] = 1e18;
minVA[i] = 1e18;
dist[i] = 1e18;
}
minUA[start] = distU[start];
minVA[start] = distV[start];
dist[start] = 0;
pq.insert({dist[start], start});
while(!pq.empty()) {
pair<ll, int> curr = *pq.begin();
pq.erase(pq.find(curr));
int u = curr.ss;
ll d = curr.ff;
ans = min(ans, minUA[u] + distV[u]);
ans = min(ans, minVA[u] + distU[u]);
// cout << u << " " << minUA[u] << " " << distV[u] << " " << ans << endl;
for(pair<int, ll> nxt : adj[u]) {
int v = nxt.ff; ll w = nxt.ss;
if(d + w + distT[v] == distS[T]) {
minUA[v] = min(minUA[u], distU[v]);
minVA[v] = min(minVA[u], distV[v]);
}
if(d + w < dist[v]) {
if(pq.find({dist[v], v}) != pq.end()) pq.erase(pq.find({dist[v], v}));
dist[v] = d+w;
pq.insert({d + w, v});
}
}
}
}
int main() {
//auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);cin.tie(0);
//ofstream cout("output.txt");
//ifstream cin("input.txt");
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// freopen("cowmbat.in", "r", stdin);
// freopen("cowmbat.out", "w", stdout);
#endif
cin >> n >> m;
cin >> S >> T; S--; T--;
cin >> U >> V; U--; V--;
adj.resize(n);
distS.resize(n);
distT.resize(n);
distU.resize(n);
distV.resize(n);
FOR(i,0,m) {
int u,v,w; cin >> u >> v >> w;
u--; v--;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
dijkstra(S, distS);
dijkstra(T, distT);
dijkstra(U, distU);
dijkstra(V, distV);
ans = distU[V];
search(S);
ans = min(ans, distU[S] + distV[T]);
ans = min(ans, distU[T] + distV[S]);
cout << ans << endl;
//auto stop = chrono::high_resolution_clock::now();
//auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
//cout << duration.count() << endl;
//cin.close();
//cout.close();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |