#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 ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) "[" #x " = " << (x) << "]"
#define int long long // forget to 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 = 2e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
void solve()
{
int n,m; cin >> n >> m;
vector<map<int, vector<pair<int,int>>>> adj(n + 1);
vector<map<int,int>> total_cost(n + 1);
FOR(i, 1, m){
int u,v,C,P; cin >> u >> v >> C >> P;
adj[u][C].emplace_back(v, P);
adj[v][C].emplace_back(u, P);
total_cost[u][C] += P;
total_cost[v][C] += P;
}
vector<map<int, int>> dist(n + 1);
const int Mx = m * 2;
struct Node{
int u,color,dist;
Node(int u = 0, int color = 0, int dist = 0): u(u), color(color), dist(dist) {}
bool operator < (const Node& q) const{
return dist > q.dist;
};
};
priority_queue<Node> pq;
dist[1][Mx] = 0;
pq.push(Node(1, Mx, 0));
/*
the only reason of changing only color of edge i is to reduce the cost of total - edge at the next v
*/
while(!pq.empty()){
Node eq = pq.top(); pq.pop();
int x = eq.u, cur_color = eq.color;
if(eq.dist > dist[x][cur_color]) continue;
if(cur_color == Mx){
for(auto [ncolor, Edge] : adj[x]){
for(auto [v, cost] : Edge){
int total = total_cost[x][ncolor];
int ncost = dist[x][Mx] + min(total - cost, cost);
if(!dist[v].count(Mx) || ckmn(dist[v][Mx], ncost)){
dist[v][Mx] = ncost;
pq.push(Node(v, Mx, dist[v][Mx]));
}
if(!dist[v].count(ncolor) || ckmn(dist[v][ncolor], dist[x][Mx])){
dist[v][ncolor] = dist[x][Mx];
pq.push(Node(v, ncolor, dist[v][ncolor]));
}
}
}
}
else{
int total = total_cost[x][cur_color];
for(auto [v, cost] : adj[x][cur_color]){
int ncost = dist[x][cur_color] + total - cost;
if(!dist[v].count(Mx) || ckmn(dist[v][Mx], ncost)){
dist[v][Mx] = ncost;
pq.push(Node(v, Mx, dist[v][Mx]));
}
}
}
}
cout << (dist[n].count(Mx) ? dist[n][Mx] : -1) << "\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 'int main()':
Main.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | 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... |