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
#define print(a) for(auto x : a) cout << x << ' '; cout << endl;
#define str string
signed main(){
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
int n, m;
cin >> n >> m;
vector<array<int, 3>> graph[n + 1];
map<int, int> p[n + 1];
for(int i = 0; i < m; i ++){
int a, b, c, P;
cin >> a >> b >> c >> P;
graph[a].push_back({b, c, P});
graph[b].push_back({a, c, P});
p[a][c] += P;
p[b][c] += P;
}
vector<vector<int>> mins(n + 1, vector<int> (2, 1e18)), color(n + 1, vector<int> (2)), price(n + 1, vector<int> (2));
set<array<int, 5>> s;
s.insert({0, 1, 0, 0, 0});
s.insert({0, 1, 1, 0, 0});
mins[1][0] = 0;
mins[1][1] = 0;
while(!s.empty()){
array<int, 5> x = *s.begin();
s.erase(s.begin());
str st;
cin >> st;
if(x[2] == 0){
for(auto y : graph[x[1]]){
if(mins[y[0]][0] > x[0] + y[2]){
s.erase({mins[y[0]][0], y[0], 0, color[y[0]][0], price[y[0]][0]});
mins[y[0]][0] = x[0] + y[2];
color[y[0]][0] = y[1];
price[y[0]][0] = y[2];
s.insert({mins[y[0]][0], y[0], 0, color[y[0]][0], price[y[0]][0]});
}
if(mins[y[0]][1] > x[0] + p[x[1]][y[1]] - y[2] - (x[3] == y[2] ? x[4] : 0)){
s.erase({mins[y[0]][1], y[0], 1, color[y[0]][1], price[y[0]][1]});
mins[y[0]][1] = x[0] + p[x[1]][y[1]] - y[2] - (x[3] == y[2] ? x[4] : 0);
color[y[0]][1] = y[1];
price[y[0]][1] = y[2];
s.insert({mins[y[0]][1], y[0], 1, color[y[0]][1], price[y[0]][ 1]});
}
}
}
else{
for(auto y : graph[x[1]]){
if(mins[y[0]][0] > x[0] + y[2]){
s.erase({mins[y[0]][0], y[0], 0, color[y[0]][0], price[y[0]][0]});
mins[y[0]][0] = x[0] + y[2];
color[y[0]][0] = y[1];
price[y[0]][0] = y[2];
s.insert({mins[y[0]][0], y[0], 0, color[y[0]][0], price[y[0]][0]});
}
if(mins[y[0]][1] > x[0] + p[x[1]][y[1]] - y[2]){
s.erase({mins[y[0]][1], y[0], 1, color[y[0]][1], price[y[0]][1]});
mins[y[0]][1] = x[0] + p[x[1]][y[1]] - y[2];
color[y[0]][1] = y[1];
price[y[0]][1] = y[2];
s.insert({mins[y[0]][1], y[0], 1, color[y[0]][1], price[y[0]][1]});
}
}
}
}
if(min(mins[n][0], mins[n][1]) == 1e18)
cout << -1;
else
cout << min(mins[n][0], mins[n][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... |