이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 2e5 + 7;
const ll oo = 1e18 + 69;
int n, m, u, v, c, p;
struct edge {
int v, c, p;
};
map<int, vector<edge>> g[maxn];
ll f[maxn];
map<int, ll> f2[maxn], cnt[maxn];
struct state {
ll cost;
int u, c;
friend bool operator < (const state &a, const state &b) {
return a.cost > b.cost;
}
};
void dijkstra() {
priority_queue<state> q;
fill_n(f, n + 1, oo);
f[1] = 0;
q.push({0, 1, 0});
while(!q.empty()) {
int u = q.top().u, c = q.top().c;
ll fu = q.top().cost;
q.pop();
if(c > 0) {
if(f2[u][c] != fu) continue;
for(auto e:g[u][c]) {
int v = e.v;
if(mini(f[v], fu + cnt[u][c] - e.p)) {
q.push({f[v], v, 0});
}
}
} else {
if(f[u] != fu) continue;
for(auto tmp:g[u]) {
for(auto e:tmp.se) {
int v = e.v;
if(mini(f[v], fu + cnt[u][e.c] - e.p)) {
q.push({f[v], v, 0});
}
if(mini(f[v], fu + e.p)) {
q.push({f[v], v, 0});
}
if(!f2[v].count(e.c) || f2[v][e.c] > fu) {
f2[v][e.c] = fu;
q.push({fu, v, e.c});
}
}
}
}
}
cout<<(f[n] == oo ? -1:f[n]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>m;
while(m--) {
cin>>u>>v>>c>>p;
g[u][c].pb({v, c, p});
g[v][c].pb({u, c, p});
cnt[u][c] += p;
cnt[v][c] += p;
}
dijkstra();
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... |