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<int> pos_u(m), pos_v(m);
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> dp(n, INF);
map<int, int> dpp[n];
int ans = INF;
dp[0]=0;
while(pq.size()){
auto [cost, ve, z] = pq.top();
pq.pop();
cost*=-1;
if(ve==n-1){
ans=min(ans, cost);
break;
}
int h=1;
if(z==-1){
if(dp[ve]!=cost) continue;
int k=1;
for(auto [i, j]: g[ve]){
if(!dpp[i].count(c[j])||dpp[i][c[j]]>cost+p[j]){
dpp[i][c[j]]=cost+p[j];
pq.push({-(cost+p[j]), i, j});
}
if(dp[i]>cost+p[j]){
dp[i]=cost+p[j];
pq.push({-dp[i], i, -1});
}
}
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(dp[g[ve][j][0]] > min(tot-p[g[ve][j][1]], p[g[ve][j][1]])){
dp[g[ve][j][0]] =cost+tot-p[g[ve][j][1]];
pq.push({-(cost+min(tot-p[g[ve][j][1]], 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(dp[g[ve][j][0]] > cost+tot-p[g[ve][j][1]]){
dp[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});
}
}
}
}
else{
if(dpp[ve][c[z]]!=cost) continue;
int pos=0;
if(v[z]==ve) pos=pos_v[z];
else pos = pos_u[z];
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(dp[g[ve][l][0]]>(cost+tot-p[g[ve][l][1]])){
dp[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:32: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]
32 | for(int l=0; l<g[i].size(); l++){
| ~^~~~~~~~~~~~
Main.cpp:75:31: 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]
75 | for(int l=1; l<g[ve].size(); l++){
| ~^~~~~~~~~~~~~
Main.cpp:92:48: 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]
92 | for(int j=g[ve].size()-k; j<g[ve].size(); j++){
| ~^~~~~~~~~~~~~
Main.cpp:117:31: 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:17: warning: unused variable 'count' [-Wunused-variable]
110 | int count=0, id=-1;
| ^~~~~
Main.cpp:110:26: warning: unused variable 'id' [-Wunused-variable]
110 | int count=0, id=-1;
| ^~
Main.cpp:55:13: warning: unused variable 'h' [-Wunused-variable]
55 | int h=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... |