#include <bits/stdc++.h>
#pragma GCC optimize("trapv")
using namespace std;
using ll = long long;
const ll INF = 1e18;
const int MN = 100001;
int n, m;
struct Edge {
int cul, cost, dest;
};
vector<Edge> L[MN];
vector<pair<int, ll> > G[MN]; /// nod, cost
ll Cost[MN];
int main() {
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
int u, v, c, w;
cin >> u >> v >> c >> w;
L[u].push_back({c, w, v});
L[v].push_back({c, w, u});
}
for(int i = 1; i <= n; ++i) {
sort(L[i].begin(), L[i].end(), [&](auto a, auto b) {
return a.cul < b.cul;
});
for(int j = 0; j < L[i].size(); ++j) {
if((j == 0 || L[i][j - 1].cul != L[i][j].cul) &&
(j == (L[i].size() - 1) || L[i][j + 1].cul != L[i][j].cul)) {
G[i].push_back(make_pair(L[i][j].dest, 0));
} else {
G[i].push_back(make_pair(L[i][j].dest, L[i][j].cost));
}
}
}
priority_queue<pair<ll, int> > PQ;
PQ.push(make_pair(0ll, 1));
for(int i = 1; i <= n; ++i) Cost[i] = INF;
Cost[1] = 0;
while(!PQ.empty()) {
ll nod = PQ.top().second, cost = - PQ.top().first;
PQ.pop();
if(Cost[nod] != cost) continue;
for(auto [it, w] : G[nod]) {
if(Cost[it] > Cost[nod] + w) {
Cost[it] = Cost[nod] + w;
PQ.push(make_pair(-Cost[it], it));
}
}
}
if(Cost[n] == INF) {
cout << "-1\n";
} else {
cout << Cost[n] << "\n";
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:26:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for(int j = 0; j < L[i].size(); ++j) {
| ~~^~~~~~~~~~~~~
Main.cpp:28:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | (j == (L[i].size() - 1) || L[i][j + 1].cul != L[i][j].cul)) {
| ~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
13696 KB |
Output is correct |
2 |
Correct |
48 ms |
8908 KB |
Output is correct |
3 |
Correct |
161 ms |
19324 KB |
Output is correct |
4 |
Correct |
54 ms |
10956 KB |
Output is correct |
5 |
Incorrect |
219 ms |
24688 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5120 KB |
Output is correct |
2 |
Correct |
4 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |