This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,M;
cin>>N>>M;
vector<pair<ll,ll> > v[N];
map<ll,ll> colourcnt[N];
for(ll i = 0;i < M;i++){
ll a,b,c,p;
cin>>a>>b>>c>>p;
a--, b--, c--;
v[a].push_back({b,c});
v[b].push_back({a,c});
colourcnt[a][c]++;
colourcnt[b][c]++;
}
priority_queue<pair<ll,ll>,vector<pair<ll,ll> >,greater<pair<ll,ll> > > pq;
ll dist[N];
memset(dist,-1,sizeof(dist));
dist[0] = 0;
pq.push({0,0});
while(!pq.empty()){
ll d = pq.top().first;
ll x = pq.top().second;
pq.pop();
if(d != dist[x]){
continue;
}
for(auto u : v[x]){
ll w = colourcnt[x][u.second] - 1;
if(dist[u.first] == -1 || dist[u.first] > dist[x] + w){
dist[u.first] = dist[x] + w;
pq.push({dist[u.first],u.first});
}
}
}
if(dist[N - 1] == -1){
cout<<-1<<'\n';
}else{
cout<<dist[N - 1]<<'\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... |