#include <bits/stdc++.h>
#define int long long
struct Vert
{
int stop;
int cost;
int color;
Vert() = default;
Vert(int stop, int cost, int color) : stop(stop), cost(cost), color(color) {};
};
signed
main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n, m;
std::cin >> n >> m;
std::vector<std::vector<Vert>> g(n);
for (int i = 0; i < m; i++)
{
int a, b;
int c, p;
std::cin >> a >> b >> c >> p;
a--, b--;
g[a].push_back(Vert(b, p, c));
g[b].push_back(Vert(a, p, c));
}
std::vector<int> dist(n, 1e18);
dist[0] = 0;
std::set<std::pair<int, int>> s;
s.insert({dist[0], 0});
while (s.size() > 0)
{
int v = s.begin()->second;
s.erase(s.begin());
std::map<int, int> all;
for (auto u : g[v])
{
all[u.color] += u.cost;
}
for (auto u : g[v])
{
int now = std::min(u.cost, all[u.color] - u.cost);
if (dist[u.stop] > dist[v] + now)
{
s.erase({dist[u.stop], u.stop});
dist[u.stop] = dist[v] + now;
s.insert({dist[u.stop], u.stop});
}
}
}
if (dist[n - 1] >= 1e18)
{
std::cout << -1;
return 0;
}
std::cout << dist[n - 1];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |