이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 110000;
long long const inf = 1e16;
int n, m;
long long D[N];
bool vst[N];
vector<pair<int, long long>> g[N];
vector<pair<int, pair<int, long long>>> v[N];
struct cmp{
bool operator() (pair<long long, int> a, pair<long long, int> b) {
return a.first > b.first;
}
};
struct cmp2{
bool operator() (pair<pair<long long, int>, int> a, pair<pair<long long, int>, int> b) {
return a.first.first > b.first.first;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
int a, b, c;
long long p;
for (int i = 1; i <= m; i ++) {
cin >> a >> b >> c >> p;
v[a].push_back({b, {c, p}});
v[b].push_back({a, {c, p}});
}
for (int i = 1; i <= n; i ++) {
map<int, long long> m;
for (auto e : v[i]) {
if (m.find(e.second.first) == m.end()) {
m[e.second.first] = 0;
}
m[e.second.first] += e.second.second;
}
for (auto e : v[i]) {
//cout << i << " " << e.first << " : " << m[e.second.first] - e.second.second << "\n";
g[i].push_back({e.first, min(e.second.second, m[e.second.first] - e.second.second)});
}
}
priority_queue<pair<long long, int>, vector<pair<long long, int>>, cmp> pq;
for (int i = 1; i <= n; i ++) {
D[i] = inf;
}
D[1] = 0;
pq.push({0, 1});
while (!pq.empty()) {
auto [d, x] = pq.top();
pq.pop();
if (vst[x]) continue;
vst[x] = true;
for (auto e : g[x]) {
if (D[e.first] > d + e.second) {
D[e.first] = d + e.second;
pq.push({D[e.first], e.first});
}
}
}
if (D[n] == inf) {
cout << -1;
return 0;
}
cout << D[n];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |