Submission #637181

#TimeUsernameProblemLanguageResultExecution timeMemory
637181chjiaoRobot (JOI21_ho_t4)C++17
0 / 100
3064 ms79464 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define newl '\n' const ll mod = 1e9+7; const ll MAX = 0x3f3f3f3f3f3f3f3f; const ll MN = 1e5+1; vector<map<ll, pair<ll,ll>>> colorsum(MN); vector<vector<pair<ll,pair<ll,ll>>>> neighbors(MN); vector<ll> dp(MN, LLONG_MAX); map<int, ll> dp2[MN]; map<ll, bool> visited[MN]; struct quadriple{ ll value; //当前的value (cost) ll curr; // node bool changed; //是否改变 ll roadcolor; //来的road的color ll roadvalue; //来的road的value (cost) ll preCurr; // quadriple(ll _value, ll _curr, bool _changed, ll _roadcolor, ll _roadvalue, ll _preCurr){ value = _value; curr = _curr; changed = _changed; roadcolor = _roadcolor; roadvalue = _roadvalue; preCurr = _preCurr; } }; struct cmp { bool operator ()(quadriple & a, quadriple& b){ // if(a.value == b.value) return a.curr > b.curr; return a.value > b.value; } }; void dijkstra(ll s) { priority_queue<quadriple, vector<quadriple>, cmp> pq; pq.push(quadriple(0LL,1,false,0,0LL, 0)); while (!pq.empty()) { quadriple c = pq.top(); pq.pop(); if(visited[c.curr][c.roadcolor] == false){ visited[c.curr][c.roadcolor] = true; // // dp[c.curr][c.changed] =c.value; dp[c.curr] = min(dp[c.curr], c.value); for(auto& [nPos, x]: neighbors[c.curr]){ //first = pos, second.first = color, second.second = num|| (was 'a') ll nColor; ;ll nValue; tie(nColor, nValue) = x; if(dp2[nPos].count(nColor) == 0) dp2[nPos][nColor] = LLONG_MAX; if(dp2[nPos].count(0) == 0) dp2[nPos][0] = LLONG_MAX; //other color //if(c.curr == nPos) continue; //if(visited[nPos][nColor]) continue; ll num = colorsum[c.curr][nColor].second; //colorsum[c.curr].find(nColor)->second.second; if(num ==1){ //相同颜色的路径数 if(c.value < dp2[nPos][nColor] ) pq.push(quadriple(c.value, nPos, false, nColor,0, c.curr)), dp2[nPos][nColor] = c.value; }else{ if(c.changed == true){ //来的时候改变了颜色 if(nColor == c.roadcolor){ //来的颜色和去的颜色一样 if(num == 2){ if(c.value < dp2[nPos][nColor] ) pq.push(quadriple(c.value, nPos, false, nColor,0, c.curr)), dp2[nPos][nColor] = c.value; }else{ if(c.value+ nValue < dp2[nPos][nColor] ) pq.push(quadriple(c.value+ nValue, nPos, true, nColor, nValue, c.curr)), dp2[nPos][nColor] = c.value+ nValue ; ll currsum = colorsum[c.curr][nColor].first - c.roadvalue - nValue; if(c.value + currsum < dp2[nPos][0] ) pq.push(quadriple(c.value + currsum, nPos, false,0,0, c.curr)), dp2[nPos][0] = c.value+ nValue ; } }else{ ////来的颜色和去的颜色不一样 if(c.value+ nValue < dp2[nPos][nColor] ) pq.push(quadriple(c.value+ nValue, nPos, true, nColor,nValue, c.curr)), dp2[nPos][nColor] = c.value+ nValue ; ll currsum = colorsum[c.curr][nColor].first - nValue; if(c.value + currsum < dp2[nPos][0] ) pq.push(quadriple(c.value+currsum, nPos, false,0,0, c.curr)), dp2[nPos][0] = c.value+ nValue ; } }else{ //来的时候未改变了颜色 if(c.value+ nValue < dp2[nPos][nColor] ) pq.push(quadriple(c.value+nValue, nPos, true, nColor,nValue, c.curr)), dp2[nPos][nColor] = c.value+ nValue ; ll currsum = colorsum[c.curr][nColor].first - nValue; if(c.value + currsum < dp2[nPos][0] ) pq.push(quadriple(c.value+currsum, nPos, false,0,0, c.curr)), dp2[nPos][0] = c.value+ nValue ; } } } } } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); //freopen("Robot.in", "r", stdin); // freopen("Robot.out", "w", stdout); ll N,M,a,b,c,d; cin >> N >> M; for(ll i=0; i<M; i++){ cin >> a >> b >> c >> d; // a--; // b--; neighbors[a].push_back(make_pair(b,make_pair(c,d))); neighbors[b].push_back(make_pair(a,make_pair(c,d))); colorsum[a][c].first +=d; //presum colorsum[a][c].second+=1; // num of same color routes colorsum[b][c].first+=d; colorsum[b][c].second+=1; } dijkstra(0); cout << ((dp[N] <= 1e18)?dp[N] :-1 )<< endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...