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>
#define ll long long
#define fi first
#define se second
#define INF 1e18
using namespace std;
const int maxn = 1e5 + 1;
typedef pair<ll,ll> pii;
typedef pair<ll,pii> piii;
typedef pair<ll,piii> piiii;
map<int,ll> dp[maxn], cost[maxn];
vector<piii> adj[maxn];
int n,m;
ll dijsktra() {
for (int i = 1; i <= n; i++) {
dp[i][0] = INF;
for (piii j: adj[i]) {
int c = j.se.fi;
dp[i][c] = INF;
}
}
dp[1][0] = 0;
priority_queue<piiii, vector<piiii>, greater<piiii> > pq;
pq.push(piiii(0,piii(1,pii(0,0))));
while (!pq.empty()) {
int u = pq.top().se.fi, oldcol = pq.top().se.se.fi;
ll D = pq.top().fi, used = pq.top().se.se.se;
pq.pop();
if (D > dp[u][oldcol]) continue;
if (u == n) return D;
for (piii i: adj[u]) {
int v = i.fi, newcol = i.se.fi;
ll w = i.se.se;
if (oldcol == 0) {
/// Ko doi mau (u,v) -> doi mau cac canh con lai
if (dp[v][0] > D + cost[u][newcol] - w) {
dp[v][0] = D + cost[u][newcol] - w;
pq.push(piiii(dp[v][0],piii(v,pii(0,0))));
}
///
/// Doi mau (u,v)
if (dp[v][newcol] > D + w) {
dp[v][newcol] = D + w;
pq.push(piiii(dp[v][newcol],piii(v,pii(newcol,w))));
}
} else {
/// Ko doi mau (u,v) -> doi mau cac canh con lai
if (dp[v][0] > D + cost[u][newcol] - w - (newcol == oldcol)*used) {
dp[v][0] = D + cost[u][newcol] - w - (newcol == oldcol)*used;
pq.push(piiii(dp[v][0],piii(v,pii(0,0))));
}
///
/// Doi mau (u,v)
if (dp[v][newcol] > D + w) {
dp[v][newcol] = D + w;
pq.push(piiii(dp[v][newcol],piii(v,pii(newcol,w))));
}
}
}
}
return -1;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
ll u,v,c,w;
cin >> u >> v >> c >> w;
adj[u].push_back(piii(v,pii(c,w)));
adj[v].push_back(piii(u,pii(c,w)));
cost[u][c] += w;
cost[v][c] += w;
}
cout << dijsktra();
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... |