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;
#define int long long
const int N=5005, INF=1e18;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<vector<array<int, 2>>> g(n);
vector<int> c(m), p(m), id(m), u(m), v(m);
vector<vector<int>> diss(n);
vector<int> pos_u(m), pos_v(m), dis(n, INF);
for(int i=0; i<m; i++){
cin >> u[i] >> v[i] >> c[i] >> p[i];
u[i]--; v[i]--;
if(u[i] > v[i]) swap(u[i], v[i]);
g[u[i]].push_back({v[i], i});
g[v[i]].push_back({u[i], i});
}
for(int i=0; i<n; i++){
sort(g[i].begin(), g[i].end(), [&](auto x, auto y){
return c[x[1]]<c[y[1]];
});
for(int l=0; l<g[i].size(); l++){
auto [u, j]=g[i][l];
if(i<u) pos_u[j] = l;
else pos_v[j] = l;
}
}
priority_queue<array<int, 3>> pq;
pq.push({0, 0, -1});
vector<int> vis(n);
map<int, int> viss[n];
int ans = INF;
while(pq.size()){
auto [cost, ve, z] = pq.top();
pq.pop();
cost*=-1;
if(z==-1 && dis[ve] < cost) continue;
if(ve==n-1){
ans=min(ans, cost);
break;
}
int h=1;
if(vis[ve]==0&&z==-1){
vis[ve]++;
h=0;
int k=1;
for(auto [i, j]: g[ve]){
pq.push({-(cost+p[j]), i, j});
}
if(g[ve].size()){
int tot=p[g[ve][0][1]];
for(int l=1; l<g[ve].size(); l++){
if(c[g[ve][l][1]]==c[g[ve][l-1][1]]){
k++;
tot+=p[g[ve][l][1]];
}
else{
for(int j=l-k; j<l; j++){
if(dis[g[ve][j][0]] > cost+tot-p[g[ve][j][1]]){
dis[g[ve][j][0]]=cost+tot-p[g[ve][j][1]];
pq.push({-(cost+tot-p[g[ve][j][1]]), g[ve][j][0], -1});
}
}
tot=p[g[ve][l][1]];
k=1;
}
}
for(int j=g[ve].size()-k; j<g[ve].size(); j++){
if(dis[g[ve][j][0]] > cost+tot-p[g[ve][j][1]]){
dis[g[ve][j][0]]=cost+tot-p[g[ve][j][1]];
pq.push({-(cost+tot-p[g[ve][j][1]]), g[ve][j][0], -1});
}
}
}
}
if(z==-1) continue;
int pos=0;
if(v[z]==ve) pos=pos_v[z];
else pos = pos_u[z];
if(viss[ve].count(c[z])) continue;
if(h)
viss[ve][c[z]]=h;
int count=0, id=-1;
vector<int> aa;
int tot=0;
for(int i=pos-1; i>=0; i--){
if(c[g[ve][i][1]]!=c[z]) break;
tot+=p[g[ve][i][1]], aa.push_back(i);
}
for(int i=pos+1; i<g[ve].size(); i++){
if(c[g[ve][i][1]]!=c[z]) break;
tot+=p[g[ve][i][1]], aa.push_back(i);
}
if(aa.size()){
for(auto l: aa){
if(dis[g[ve][l][0]]>(cost+tot-p[g[ve][l][1]])){
dis[g[ve][l][0]] =(cost+tot-p[g[ve][l][1]]);
pq.push({-(cost+tot-p[g[ve][l][1]]), g[ve][l][0], -1});
}
}
}
};
if(ans==INF) cout<<-1;
else cout<<ans;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:33:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int l=0; l<g[i].size(); l++){
| ~^~~~~~~~~~~~
Main.cpp:70:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int l=1; l<g[ve].size(); l++){
| ~^~~~~~~~~~~~~
Main.cpp:87:44: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for(int j=g[ve].size()-k; j<g[ve].size(); j++){
| ~^~~~~~~~~~~~~
Main.cpp:117:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int i=pos+1; i<g[ve].size(); i++){
| ~^~~~~~~~~~~~~
Main.cpp:110:13: warning: unused variable 'count' [-Wunused-variable]
110 | int count=0, id=-1;
| ^~~~~
Main.cpp:110:22: warning: unused variable 'id' [-Wunused-variable]
110 | int count=0, id=-1;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |