제출 #532018

#제출 시각아이디문제언어결과실행 시간메모리
532018amunduzbaevRobot (JOI21_ho_t4)C++17
34 / 100
316 ms48868 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];

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=1;i<=n;i++){
		map<int, int>& mm = uu[i];
		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});
		}
	}
	
	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();
		if(c < 0){ c = abs(c);
			int sum = uu[u][c], is = 0;
			for(auto x : ed[u][c]){
				if(x[0] == v) { is = 1; continue; }
				q.push({D + sum - x[1], x[0], c, x[1], u});
			}
			ed[u][c].clear();
			if(is) ed[u][c].push_back({v, p});
		} else if(!used[u]) {
			used[u] = 1;
			d[u] = D;
			for(auto x : ee[u]){
				q.push({D + x[1], x[0], abs(x[2]), x[1], u});
				if(x[2] < 0) {
					q.push({D, 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...