이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m;
struct edge{
int to, c, p;
};
struct nod{
unordered_map<int, vector<edge>> out;
int dp;
bool used;
unordered_map<int, int> dpc;
unordered_map<int, bool> usedc;
unordered_map<int, int> sum;
};
struct elem{
int cost, x, c;
bool operator<(const elem &ot) const{
return cost>ot.cost;
}
};
vector<nod> g;
int32_t main(){
cin>>n>>m;
g.resize(n+1);
for(int i=1;i<=m;++i){
int a, b, c, p;
cin>>a>>b>>c>>p;
g[a].out[c].push_back({b, c, p});
g[b].out[c].push_back({a, c, p});
g[a].sum[c]+=p;
g[b].sum[c]+=p;
}
priority_queue<elem> pq;
for(int i=1;i<=n;++i) g[i].dp=1e18;
g[1].dp=0;
pq.push({0, 1, 0});
while(!pq.empty()){
elem x=pq.top();
pq.pop();
if(x.c){
if(g[x.x].usedc.count(x.c)) continue;
g[x.x].usedc[x.c]=1;
for(auto it:g[x.x].out[x.c]){
int cost=g[x.x].sum[x.c]-it.p+x.cost;
if(cost<g[it.to].dp){
g[it.to].dp=cost;
pq.push({g[it.to].dp, it.to, 0});
}
}
}
else{
if(g[x.x].used) continue;
g[x.x].used=1;
for(auto out:g[x.x].out){
for(auto it:out.second){
int cost=g[x.x].sum[it.c]-it.p+x.cost;
if(cost<g[it.to].dp){
g[it.to].dp=cost;
pq.push({g[it.to].dp, it.to, 0});
}
cost=it.p+x.cost;
if(cost<g[it.to].dp){
g[it.to].dp=cost;
pq.push({g[it.to].dp, it.to, 0});
}
cost=x.cost;
if((!g[it.to].dpc.count(it.c))||(cost<g[it.to].dpc[it.c])){
g[it.to].dpc[it.c]=cost;
pq.push({g[it.to].dpc[it.c], it.to, it.c});
}
}
}
}
}
int sol=g[n].dp;
if(sol==1e18) sol=-1;
cout<<sol<<"\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |