제출 #696821

#제출 시각아이디문제언어결과실행 시간메모리
696821browntoadRobot (JOI21_ho_t4)C++14
0 / 100
127 ms25456 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define FOR(i, a, b) for (int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n+1)
#define pii pair<int, int>
#define ppi pair<int, pii>
#define f first
#define s second
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) (int)((x).size())

const ll maxn = 1e5+5;
const ll maxm = 2e5+5;

map<pii, bool> occ; // the special dp[i][j] state (if it is alr determined or not)
vector<int> dpv(maxn);
priority_queue<ppi, vector<ppi>, greater<ppi> > pq;
map<pii, int> vord;
map<pii, int> colcnt; 

struct edge{
	int to, c, p;
	int idc; // for that node, the rank of the color
	bool operator <(edge b){
		return c<b.c;
	}
};
vector<edge> e[maxn];
vector<vector<int> > ec;
int n, m;


signed main(){
	cin>>n>>m;
	REP(i, m){
		int u, v, c, p; cin>>u>>v>>c>>p;
		e[u].pb({v, c, p, 0});
		e[v].pb({u, c, p, 0});
	}
	REP1(i, n){
		sort(ALL(e[i]));
		vector<int> tmp;
		REP(j, SZ(e[i])){
			tmp.pb(j);
			if (j==SZ(e[i])-1 ||e[i][j+1].c!=e[i][j].c){
				vord[{i, e[i][j].c}]=SZ(ec);
				ec.pb(tmp);
				tmp.clear();
			}
			colcnt[{i, e[i][j].c}]+=e[i][j].p;
		}
	}
	// compute dp
	pq.push({0, {1, 0}});
	int ans = -1;
	while(pq.size()){
		ppi tmp = pq.top(); pq.pop();
		if (occ.count(tmp.s)) continue;
		occ[tmp.s]=1;
		if (tmp.s.f == n && tmp.s.s == 0){
			ans=tmp.f;
			break;
		}
		if (tmp.s.s == 0){
			REP(i, SZ(e[tmp.s.f])){
				int tx = e[tmp.s.f][i].to;
				if (!occ[{tx, e[tmp.s.f][i].c}]){
					pq.push({e[tmp.s.f][i].p+tmp.f, {tx, e[tmp.s.f][i].c}});
				}
				if (!occ[{tx, 0}]){
					pq.push({min(e[tmp.s.f][i].p+tmp.f, colcnt[{tmp.s.f, e[tmp.s.f][i].c}]-e[tmp.s.f][i].p), {tx, 0}});
				}

			}
		}
		else{
			int poop = vord[tmp.s];
			REP(i, SZ(ec[poop])){
				int tid = ec[poop][i];
				if (!occ[{e[tmp.s.f][tid].to, 0}]){
					pq.push({colcnt[{tmp.s.f, e[tmp.s.f][tid].c}]-e[tmp.s.f][tid].p+tmp.f, {e[tmp.s.f][tid].to, 0}});
				}
			}
		}
	}
	cout<<ans<<endl;


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