This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: swagchicken1
PROG:
LANG: C++11
*/
#include <iostream>
#include <tuple>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <algorithm>
#include <vector>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <cctype>
#include <climits>
#include <chrono>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector< vector <int> > vvi;
typedef pair<int, int> pii;
typedef pair < pair < int, int >, int > piii;
typedef pair < pair <int, int > , pair <int, int> > piiii;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
#define FOR(i,a,b) for(int i = a; i < b; i ++)
#define RFOR(i,a,b) for(int i = a-1; i >= b; i --)
#define all(a) a.begin(), a.end()
#define endl '\n';
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
template <typename T>
void pr(vector<T> &v) {
FOR(i, 0, sz(v)) cout << v[i] << " ";
cout << endl;
}
template <typename T>
void pr(vector<vector<T> > &v) {
FOR(i, 0, sz(v)) { pr(v[i]); }
}
template <typename T>
void re(T &x) {
cin >> x;
}
template <typename T>
void re(vector<T> &a) {
FOR(i, 0, sz(a)) re(a[i]);
}
template <class Arg, class... Args>
void re(Arg &first, Args &... rest) {
re(first);
re(rest...);
}
template <typename T>
void pr(T x) {
cout << x << endl;
}
template <class Arg, class... Args>
void pr(const Arg &first, const Args &... rest) {
cout << first << " ";
pr(rest...);
cout << endl;
}
void ps() { cout << endl; }
template<class T, class... Ts>
void ps(const T& t, const Ts&... ts) {
cout << t; if (sizeof...(ts)) cout << " "; ps(ts...);
}
const ll MOD = 1000000007;
#define inf 1e18;
#define INF INT_MAX
//#define DEBUG
long double PI = 4*atan(1);
long double eps = 1e-9;
struct edge {
int nxt;
int col;
ll w;
};
vector<map<int, vector<edge>>> adj;
map<int, ll> sumw[100010] = {};
ll dp[100010] = {};
map<int, ll> dp2[100010] = {};
int main() {
//auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);cin.tie(0);
//ofstream cout("output.txt");
//ifstream cin("input.txt");
#ifdef DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// freopen("cowmbat.in", "r", stdin);
// freopen("cowmbat.out", "w", stdout);
#endif
int n,m; cin >> n >> m;
adj.resize(n);
FOR(i,0,m) {
int u,v; cin >> u >> v; u--; v--;
int c; ll p; cin >> c >> p;
adj[u][c].pb({v, c, p});
adj[v][c].pb({u, c, p});
sumw[u][c] += p;
sumw[v][c] += p;
}
FOR(i,0,n) {
dp[i] = 1e18;
}
priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> pq;
dp[0] = 0;
pq.push({0, {0, 0}});
while(!pq.empty()) {
pair<ll, pii> curr = pq.top(); pq.pop();
ll d = curr.ff;
int node = curr.ss.ff;
int c = curr.ss.ss;
if(c > 0) {
if(d != dp2[node][c]) continue;
for(edge ed : adj[node][c]) {
ll nw = d + sumw[node][c] - ed.w;
if(nw < dp[ed.nxt]) {
dp[ed.nxt] = nw;
pq.push({dp[ed.nxt], {ed.nxt, 0}});
}
}
} else {
if(d != dp[node]) continue;
for (auto x : adj[node]) {
for(edge ed : x.ss) {
int nxt = ed.nxt;
int col = ed.col;
ll w = ed.w;
ll nw1 = d + w;
if(nw1 < dp[nxt]) {
dp[nxt] = nw1;
pq.push({dp[nxt], {nxt, 0}});
}
ll nw2 = d + sumw[node][col] - w;
if(nw2 < dp[nxt]) {
dp[nxt] = nw2;
pq.push({dp[nxt], {nxt, 0}});
}
ll nw3 = d;
if(dp2[nxt].count(col) == 0 || nw3 < dp2[nxt][col]) {
dp2[nxt][col] = nw3;
pq.push({dp2[nxt][col], {nxt, col}});
}
}
}
}
}
cout << (dp[n - 1] == 1e18 ? -1 : dp[n-1]) << endl;
//auto stop = chrono::high_resolution_clock::now();
//auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
//cout << duration.count() << endl;
//cin.close();
//cout.close();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |