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;
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &pa) { is >> pa.first >> pa.second; return is; }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << "(" << pa.first << "," << pa.second << ")"; return os; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << "{"; for (auto v : vec) os << v << ","; os << "}"; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << "{"; for (auto v : mp) os << v.first << "=>" << v.second << ","; os << "}"; return os; }
template <typename T> void resize_array(vector<T> &vec, int len) { vec.resize(len); }
template <typename T, typename... Args> void resize_array(vector<T> &vec, int len, Args... args) { vec.resize(len); for (auto &v : vec) resize_array(v, args...); }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
// 函数名后面加 ll 就可以计算 long long类型对应的结果
// __builtin_ffs(x)
// 返回x中最后一个为1的位是从后向前的第几位
// __builtin_popcount(x)
// x中1的个数
// __builtin_ctz(x)
// x末尾0的个数。x=0时结果未定义。
// __builtin_clz(x)
// x前导0的个数。x=0时结果未定义。
// __builtin_parity(x)
// x中1的奇偶性。
#define highest_bit1_index(x) (31 - __builtin_clz(k))
#define highest_bit1_index_ll(x) (63 - __builtin_clzll(k))
#define rep(i, a, n) for (int i = a; i < (n); i++)
#define per(i, a, n) for (int i = (n)-1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef double db;
#if DEBUG
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;
#else
#define dbg(x)
#endif
template <typename T>
class Dijkstra {
public:
struct Edge {
int to;
int color;
T cost;
friend ostream &operator<<(ostream &os, const Edge &r) { return os << "<" << r.to << "," << r.cost << ">"; }
};
Dijkstra(vector<vector<Edge>> &edges) : edges_(edges) {}
T ShortestPath(int start) {
int n = sz(edges_);
distance_.resize(edges_.size());
fill(distance_.begin(), distance_.end(), numeric_limits<T>::max());
distance_[start] = 0;
prev_vertex_.resize(edges_.size());
fill(prev_vertex_.begin(), prev_vertex_.end(), -1);
// <min dis, vertex>
using P = pair<T, int>;
// make priority queue sort smaller integer first
priority_queue<P, vector<P>, greater<P>> que;
que.push(P(distance_[start], start));
vector<map<int,T>> min_cost2(n);
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (distance_[v] < p.first) continue;
for (int i = 0; i < edges_[v].size(); ++i) {
Edge e = edges_[v][i];
if (!min_cost2[e.to].count(e.color)) min_cost2[e.to][e.color] = distance_[v];
T dis_v2 = distance_[v];
if (min_cost2[v].count(e.color)) dis_v2 = min_cost2[v][e.color];
T min_dis = min(dis_v2 + psum_[v][e.color] - e.cost, distance_[v] + e.cost);
if (distance_[e.to] > min_dis) {
distance_[e.to] = min_dis;
prev_vertex_[e.to] = v;
que.push(P(distance_[e.to], e.to));
}
}
}
if (distance_[n-1] != numeric_limits<T>::max()) return distance_[n-1];
else return -1;
}
// get min path to t
vector<int> GetPath(int t) {
vector<int> path;
for (; t != -1; t = prev_vertex_[t]) path.push_back(t);
reverse(path.begin(), path.end());
return path;
}
// private:
vector<vector<Edge>> &edges_;
vector<T> distance_;
vector<map<int,T>> psum_; // node -> color -> p
vector<int> prev_vertex_; // for path restore
};
using DijkstraT = Dijkstra<long long>;
using Edge = DijkstraT::Edge;
class Solution {
public:
void Solve() {
int n,m;
while(cin>>n>>m) {
vector<vector<Edge>> edges(n);
DijkstraT dj(edges);
dj.psum_.resize(n);
int a,b,c,p;
rep(i,0,m) {
cin>>a>>b>>c>>p;
a--; b--;
edges[a].emplace_back(Edge{b,c,p});
edges[b].emplace_back(Edge{a,c,p});
dj.psum_[a][c] += p;
dj.psum_[b][c] += p;
}
cout << dj.ShortestPath(0) << "\n";
// break;
}
cout.flush();
}
private:
};
// # define FILE_IO 1
void set_io(const string &name = "") {
ios::sync_with_stdio(false);
cin.tie(nullptr);
#if FILE_IO
if (!name.empty()) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
#endif
}
int main() {
set_io("input");
Solution().Solve();
return 0;
}
Compilation message (stderr)
Main.cpp: In instantiation of 'T Dijkstra<T>::ShortestPath(int) [with T = long long int]':
Main.cpp:141:38: required from here
Main.cpp:87:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Dijkstra<long long int>::Edge, std::allocator<Dijkstra<long long int>::Edge> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < edges_[v].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |