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
signed main(){
int n, m; cin >> n >> m;
vector<vector<array<int, 3>>> adj(n);
vector<map<int, int>> col_cost(n);
for(int i = 0; i < m; i++){
int u, v, c, p; cin >> u >> v >> c >> p;
u--, v--;
adj[u].push_back({v, c, p});
adj[v].push_back({u, c, p});
col_cost[u][c] += p;
col_cost[v][c] += p;
}
/*for(int i = 0; i < n; i++){
cout << i+1 << ": ";
for(auto [c, p]: col_cost[i])
printf("(%d %d) ", c, p);
cout << endl;
}*/
vector<map<int, int>> dp(n);
priority_queue<array<int, 5>> q;
q.push({0, 0, 0, 0});
while(q.size()){
auto [d, u, c, p, pre] = q.top(); q.pop();
d = -d;
//printf("d: %d, u: %d, c: %d, p: %d\n", d, u+1, c, p);
if(dp[u].count(c)) continue;
dp[u][c] = d;
for(auto [v, c1, p1]: adj[u]){
// change everything else
if(v == pre) continue;
//cout << v << " " << c1 << " " << col_cost[u][c1]-p1-((c==c1)*p) << endl;
q.push({-(d+col_cost[u][c1]-((c==c1)*p)-p1), v, c1, p1, u});
// change just this edge
q.push({-(d+p1), v, 0, p1});
}
}
int ans = LLONG_MAX;
for(auto [x, res]: dp[n-1])
ans = min(ans, res);
cout << (ans == LLONG_MAX ? -1 : ans) << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |