Submission #1055235

#TimeUsernameProblemLanguageResultExecution timeMemory
1055235Alihan_8Olympic Bus (JOI20_ho_t4)C++17
0 / 100
520 ms4044 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//~ #define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const i64 inf = 1e12;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
	
	int n, m; cin >> n >> m;
	
	// subtask #2
	
	vector <int> U(m), V(m), C(m), D(m);
	
	for ( int i = 0; i < m; i++ ){
		cin >> U[i] >> V[i] >> C[i] >> D[i];
		
		--U[i], --V[i];
	}
	
	vector <int> is(m);
	
	auto get = [&](auto U, auto V, auto C, int sx, int rev = -1){
		vector <vector<ar<int,3>>> adj(n);
		
		for ( int i = 0; i < (int)U.size(); i++ ){
			int u = U[i], v = V[i];
			
			if ( i == rev ) swap(u, v);
			
			adj[u].pb({v, C[i], i});
		}
		
		vector <i64> dp(n, inf);
		vector <int> fa(n, -1);
		
		priority_queue <pair<i64,int>> q;
		
		q.push({0, sx}); dp[sx] = 0;
		
		while ( !q.empty() ){
			auto [val, u] = q.top();
			q.pop();
			
			if ( -val != dp[u] ) continue;
			
			for ( auto &[v, w, j]: adj[u] ){
				if ( chmin(dp[v], dp[u] + w) ){
					q.push({-dp[v], v});
					fa[v] = j;
				}
			}
		}
		
		for ( auto &x: fa ){
			if ( x != -1 ) is[x] = 1;
		}
		
		return dp;
	};
	
	auto f = [&](int j){
		return get(U, V, C, 0, j)[n - 1] + get(U, V, C, n - 1, j)[0] + D[j];
	};
	
	int s = 0, e = n - 1;
	
	auto fs = get(U, V, C, s), fe = get(U, V, C, e);
	auto IS = is;
	auto ts = get(V, U, C, s), te = get(V, U, C, e);
	
	i64 opt = fs[e] + fe[s];
	
	for ( int i = 0; i < m; i++ ){
		int x = min(fs[e], fs[V[i]] + C[i] + te[U[i]]);
		int y = min(fe[s], fe[V[i]] + C[i] + ts[U[i]]);
		
		if ( IS[i] ){
			chmin(opt, f(i));
		} else{
			chmin(opt, x + y + D[i]);
		}
	}
	
	if ( opt >= inf ){
		opt = -1;
	}
	
	cout << opt;
	
    cout << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...