Submission #676968

#TimeUsernameProblemLanguageResultExecution timeMemory
676968baneRobot (JOI21_ho_t4)C++17
34 / 100
1539 ms2097152 KiB
#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 Robot(int N, int M, vector<tuple<int,int,int,int>>roads){
	vector<vector<vector<tuple<int,int,int>>>>edges(N + 1, vector<vector<tuple<int,int,int>>>(M + 1));
	unordered_map<int, ll>dp2[N + 1];
	unordered_map<int,ll>sum[N + 1];
	for (int i = 0; i<M; i++){
		int a,b,c,d;
		tie(a,b,c,d) = roads[i];
		edges[a][c].pb({b,c,d});
		edges[b][c].pb({a,c,d});
		sum[a][c]+=d;
		sum[b][c]+=d;
	}
	ll dp[N + 1];
	for (int i = 0; i<=N; i++){
		dp[i] = 1e18;
	}
	dp[1] = 0;
	priority_queue<tuple<ll, int, int>>q;
	q.push(make_tuple(0,1,0));
	while(!q.empty()){
		ll dist;
		int node, state;
		tie(dist, node, state) = q.top();
		dist = -dist;
		q.pop();
		if (state){
			for (auto edge : edges[node][state]){
				ll new_cost = dist + sum[node][state] - get<2>(edge);
				if (new_cost < dp[get<0>(edge)]){
					dp[get<0>(edge)] = new_cost;
					q.push(make_tuple(-new_cost, get<0>(edge), 0));
				}
			}
		}else{
			for (auto color : edges[node]){
				for (auto edge : color){
					int next = get<0>(edge);
					int col = get<1>(edge);
					int p = get<2>(edge);
					ll new_cost = dist + p;
					if (new_cost < dp[next]){
						dp[next] = new_cost;
						q.push(make_tuple(-new_cost, next, 0));
					}
					new_cost = dist +  sum[node][col] - p;
					if (new_cost < dp[next]){
						dp[next] = new_cost;
						q.push(make_tuple(-new_cost, next, 0));
					}
					if (!dp2[next].count(col) || dp2[next][col] > dist){
						dp2[next][col] = dist;
						q.push(make_tuple(-dist, next, col));
					}
				}
			}
		}
	}
	return (dp[N] == 1e18 ? -1 : dp[N]);
}
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
	int N,M;
	cin >> N >> M;
	vector<tuple<int,int,int,int>>roads;
	for (int i = 0; i<M; i++){
		int a,b,c,p;
		cin >> a >> b >> c >> p;
		roads.pb({a,b,c,p});
	}
	cout<<Robot(N,M,roads);
	return 0;   
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...