Submission #531987

#TimeUsernameProblemLanguageResultExecution timeMemory
531987amunduzbaevRobot (JOI21_ho_t4)C++17
0 / 100
205 ms49044 KiB
#include "bits/stdc++.h"
using namespace std;

#define ar array
#define int long long

const int N = 1e5 + 5;
struct node{
	int a, b, c, p;
};
vector<int> edges[N];
vector<ar<int, 3>> ee[N];
map<int, vector<ar<int, 2>>> ed[N];
map<int, int> uu[N];
int d[N], used[N];
node e[N];

/*

5 5
1 2 2 0
2 3 1 2
3 4 1 2
4 5 3 0
2 4 1 3

4 3
1 2 1 2
2 3 1 1
3 4 1 2

*/

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	int n, m; cin>>n>>m;
	for(int i=0;i<m;i++){
		node& x = e[i]; cin>>x.a>>x.b>>x.c>>x.p;
		edges[x.a].push_back(i);
		edges[x.b].push_back(i);
	}
	
	for(int i=0;i<n;i++){
		map<int, int> mm;
		for(auto in : edges[i]){ auto x = e[in];
			mm[x.c] += x.p;
		}
		
		for(auto in : edges[i]){ auto x = e[in];
			int b = x.a + x.b - i;
			if(mm[x.c] > x.p){
				ee[i].push_back({b, x.p, -x.c});
				if(x.p > mm[x.c] - x.p){
					ee[i].push_back({b, mm[x.c] - x.p, x.c});
				}
			} else {
				ee[i].push_back({b, 0, x.c});
			}
			
			ed[i][x.c].push_back({b, x.p});
		}
		
		uu[i] = mm;
	}
	
	priority_queue<ar<int, 5>, vector<ar<int, 5>>, greater<ar<int, 5>>> q;
	q.push({0, 1, 0, 0, 0});
	memset(d, 127, sizeof d);
	while(!q.empty()){
		int D = q.top()[0], u = q.top()[1], c = q.top()[2], p = q.top()[3], v = q.top()[4]; q.pop();
		//~ cout<<D<<" "<<u<<" "<<c<<" "<<p<<" "<<v<<"\n";
		d[u] = min(d[u], D);
		if(c < 0){ c = -c;
			int sum = uu[u][c];
			bool is = 0;
			for(auto x : ed[u][c]){
				if(x[0] == v) { is = 1; continue; }
				q.push({D + sum - p - x[1], x[0], c, x[1], u});
			}
			ed[u][c].clear();
			if(is) ed[u][c].push_back({v, p});
		}
		if(!used[u]){
			used[u] = 1;
			for(auto x : ee[u]){
				//~ cout<<x[0]<<" "<<x[1]<<" "<<x[2]<<"\n";
				q.push({D + x[1], x[0], x[2], x[1], u});
			}
		}
	}
	
	if(d[n] > 1e18) cout<<-1<<"\n";
	else cout<<d[n]<<"\n";
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...