이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
unordered_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))));
}
///
} 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))));
}
/// Doi mau (u,v) va cac canh con lai
if (dp[v][newcol] > D + cost[u][newcol] - (newcol == oldcol)*used) {
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;
}
/*
6 4
1 2 1 1000000000
2 3 1 1000000000
3 4 1 1000000000
4 5 1 1000000000
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |