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;
int n,m;
long long dp[100005];
vector<pair<int,long long>>g[100005];
map<int,long long>color[100005];
vector<array<int,4>>save;
priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>>pq;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1; i<=m; i++) {
int u,v,c,p;
cin>>u>>v>>c>>p;
color[u][c]+=p;
color[v][c]+=p;
save.push_back({u,v,c,p});
}
for(auto [u,v,c,p]:save) {
long long minn=1e18;
minn=min(minn,color[u][c]-p);
for(int i=1; i<=m; i++)minn=min(minn,color[u][i]+p);
g[u].push_back({v,minn});
minn=1e18;
minn=min(minn,color[v][c]-p);
for(int i=1; i<=m; i++)minn=min(minn,color[v][i]+p);
g[v].push_back({u,minn});
}
for(int i=1; i<=n; i++)dp[i]=1e18;
dp[1]=0;
pq.push({0,1});
while(pq.size()) {
auto[_,u]=pq.top();
pq.pop();
if(dp[u]!=_)continue;
for(auto v:g[u])
if(dp[v.first]>dp[u]+v.second) {
dp[v.first]=dp[u]+v.second;
pq.push({dp[v.first],v.second});
}
}
if(dp[n]==1e18)cout<<-1;
else cout<<dp[n];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |