이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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<ll, bool> visited[MN];
struct quadriple{
ll value; //当前的value (cost)
ll curr; // node
bool changed; //是否改变
ll roadcolor; //来的road的color
ll roadvalue; //来的road的value (cost)
quadriple(ll _value, ll _curr, bool _changed, ll _roadcolor, ll _roadvalue){
value = _value;
curr = _curr;
changed = _changed;
roadcolor = _roadcolor;
roadvalue = _roadvalue;
}
};
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));
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(visited[nPos][nColor]) continue;
ll num = colorsum[c.curr][nColor].second; //colorsum[c.curr].find(nColor)->second.second;
if(num ==1){ //相同颜色的路径数
pq.push(quadriple(c.value, nPos, false, nColor,0));
}else{
if(c.changed == true){ //来的时候改变了颜色
if(nColor == c.roadcolor){ //来的颜色和去的颜色一样
if(num == 2){
pq.push(quadriple(c.value, nPos, false, nColor,0));
}else{
pq.push(quadriple(c.value+ nValue, nPos, true, nColor, nValue));
ll currsum = colorsum[c.curr][nColor].first - c.roadvalue - nValue;
pq.push(quadriple(c.value + currsum, nPos, false,-1,0));
}
}else{ ////来的颜色和去的颜色不一样
pq.push(quadriple(c.value+ nValue, nPos, true, nColor,nValue));
ll currsum = colorsum[c.curr][nColor].first - nValue;
pq.push(quadriple(c.value+currsum, nPos, false,-1,0));
}
}else{ //来的时候未改变了颜色
pq.push(quadriple(c.value+nValue, nPos, true, nColor,nValue));
ll currsum = colorsum[c.curr][nColor].first - nValue;
pq.push(quadriple(c.value+currsum, nPos, false,-1,0));
}
}
}
}
}
}
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] != LLONG_MAX)?dp[N] :-1 )<< endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |