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 fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 1e5+5;
const ll INF = 1e18;
const int Mx = 2e5+1;
struct Node{
// color < Mx: some affect in color cost of that color
int v, cur_dist, color, dist_edge;
Node(int v = 0, int cur_dist = 0, int color = Mx, int dist_edge = 0): v(v), cur_dist(cur_dist), color(color), dist_edge(dist_edge) {}
bool operator < (const Node& q) const{
return cur_dist > q.cur_dist;
}
};
int n, m;
map<int,int> color_cost[N], dist[N];
map<int, vector<pair<int,int>>> adj[N];
void solve()
{
cin >> n >> m;
FOR(i, 1, m){
int u,v,c,w;
cin >> u >> v >> c >> w;
adj[u][c].push_back(make_pair(v, w));
adj[v][c].push_back(make_pair(u, w));
color_cost[u][c] += w;
color_cost[v][c] += w;
}
// 2 case: (choose to change u->v and affect v) && (choose to change u->v with no affect to v || choose to change all except u->v and no affect to v)
priority_queue<Node> pq;
pq.push(Node(1));
dist[1][Mx] = 0;
while(!pq.empty()){
Node eq = pq.top(); pq.pop();
int color = eq.color, x = eq.v;
if(dist[x][color] < eq.cur_dist) continue;
if(color < Mx){
int total = color_cost[x][color] - eq.dist_edge;
for(auto [v, w] : adj[x][color]){
// no affect to v
if(!dist[v].count(Mx) || ckmn(dist[v][Mx], dist[x][color] + total - w)){
dist[v][Mx] = dist[x][color] + total - w;
pq.push(Node(v, dist[v][Mx], Mx, 0));
}
if(adj[v].count(color) && (int)adj[v][color].size() > 1){
// keep affect v
if(!dist[v].count(color) || ckmn(dist[v][color], dist[x][color] + w)){
dist[v][color] = dist[x][color] + w;
pq.push(Node(v, dist[v][color], color, w));
}
}
}
}
else{
for(auto [color, list_col] : adj[x]){
int total = color_cost[x][color];
for(auto [v, w] : list_col){
// don't affect v : change all except x->v or change only x->v
if(!dist[v].count(Mx) || ckmn(dist[v][Mx], dist[x][Mx] + total - w)){
dist[v][Mx] = dist[x][Mx] + total - w;
pq.push(Node(v, dist[v][Mx], Mx, 0));
}
if(!dist[v].count(Mx) || ckmn(dist[v][Mx], dist[x][Mx] + w)){
dist[v][Mx] = dist[x][Mx] + w;
pq.push(Node(v, dist[v][Mx], Mx, 0));
}
if(adj[v].count(color) && (int)adj[v][color].size() > 1){
// can affect
if(!dist[v].count(color) || ckmn(dist[v][color], dist[x][Mx] + w)){
dist[v][color] = dist[x][Mx] + w;
pq.push(Node(v, dist[v][color], color, w));
}
}
}
}
}
}
if(!dist[n].size()){
cout << "-1\n";
}
else{
int answer = INF;
for(auto [color, last_dist] : dist[n]){
if(color == Mx) ckmn(answer, last_dist);
}
cout << answer <<"\n";
}
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:72:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for(auto [v, w] : adj[x][color]){
| ^
Main.cpp:89:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
89 | for(auto [color, list_col] : adj[x]){
| ^
Main.cpp:92:26: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
92 | for(auto [v, w] : list_col){
| ^
Main.cpp:121:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
121 | for(auto [color, last_dist] : dist[n]){
| ^
Main.cpp: In function 'int main()':
Main.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:138:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |