이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f(i, l, r) for (int i = l; i <= r; i++)
#define debug(x) cout << #x << '=' << (x) << endl
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define ff first
#define ss second
using PLL = pair<ll, ll>;
using VL = vector<ll>;
using VLL = vector<pair<ll, ll>>;
// -------------------------------------------------------------------------------
constexpr int mod = 1e9 + 7;
void setIO(const string &name = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (sz(name)) {
freopen((name + ".in").c_str(), "r", stdin);
freopen((name + ".out").c_str(), "w", stdout);
}
}
// -------------------------------------------------------------------------------
// ------------------------Template End ------------------------------------------
// -------------------------------------------------------------------------------
struct Edge {
ll color, changePrice, to;
Edge(ll t, ll c, ll p) : color(c), changePrice(p), to(t) {
}
};
constexpr ll inf = 1e18;
int main() {
setIO();
ll n, m;
cin >> n >> m;
vector<map<ll, vector<Edge>>> adj(n);
vector<map<ll, ll>> colorCount(n);
ll a, b, c, p;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c >> p;
a--, b--;
adj[a][c].push_back(Edge(b, c, p));
adj[b][c].push_back(Edge(a, c, p));
colorCount[a][c] += p;
colorCount[b][c] += p;
}
priority_queue<tuple<ll, ll, ll>> pq;
vector<ll> dp(n, inf);
vector<map<ll, ll>> dp2(n);
dp[0] = 0;
pq.push({0, 0, 0});
ll distNow, node;
while (!pq.empty()) {
tie(distNow, node, c) = pq.top();
pq.pop();
distNow = -distNow;
ll newDist;
if (c) {
if (dp2[node][c] != distNow) continue;
for (auto edge: adj[node][c]) {
// I can't change this one
newDist = dp2[node][c] + colorCount[node][c] - edge.changePrice;
if (newDist < dp[edge.to]) {
dp[edge.to] = newDist;
pq.push({-dp[edge.to], edge.to, 0});
}
}
} else {
if (dp[node] != distNow) continue;
for (auto &edges: adj[node]) {
for (auto edge : edges.second) {
// dont change this, change others
newDist = dp[node] + colorCount[node][edge.color] - edge.changePrice;
if (newDist < dp[edge.to]) {
dp[edge.to] = newDist;
pq.push({-dp[edge.to], edge.to, 0});
}
// change this one
newDist = dp[node] + edge.changePrice;
if (newDist < dp[edge.to]) {
dp[edge.to] = newDist;
pq.push({-dp[edge.to], edge.to, 0});
}
// change this one and others for ahead one
newDist = dp[node];
if (!dp2[edge.to].count(edge.color) || newDist < dp2[edge.to][edge.color]) {
dp2[edge.to][edge.color] = newDist;
pq.push({-dp2[edge.to][edge.color], edge.to, edge.color});
}
}
}
}
}
cout << (dp[n - 1] >= inf ? -1 : dp[n - 1]);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void setIO(const string&)':
Main.cpp:25:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | freopen((name + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen((name + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |