이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "cyberland.h"
#include <bits/stdc++.h>
#define ll long long
#define pa pair<int, int>
#define fi first
#define se second
using namespace std;
const int maxN = 1e5;
const double oo = 1e18, eps = 1e-7;
int n, fin;
short abi[maxN];
vector<pa> adj[maxN];
ll dist[maxN];
template<class X>
bool mini(X &a, X b) {
if (a > b || a == -1) {
a = b;
return true;
}
return false;
}
void Dijkstra() {
for (int i = 0; i < n; i++) dist[i] = -1;
priority_queue<pa, vector<pa>, greater<pa> > pq;
dist[fin] = 0; pq.emplace(0, fin);
ll D; int node;
while (!pq.empty()) {
tie(D, node) = pq.top(); pq.pop();
if (D != dist[node]) continue;
for (pa o : adj[node])
if (mini(dist[o.se], D + o.fi)) pq.emplace(dist[o.se], o.se);
}
}
double rDist[2][maxN];
void Ford_Bellman(vector<int> &v) {
queue<pair<double, int> > q;
for (int x : v) {
rDist[0][x] = rDist[1][x];
q.emplace(rDist[0][x], x);
}
double D; int node;
while (!q.empty()) {
tie(D, node) = q.front(); q.pop();
if (D > rDist[0][node]) continue;
for (pa o : adj[node]) {
if (o.se == fin) continue;
if (abi[o.se] == 1) rDist[1][o.se] = min(rDist[1][o.se], (D + o.fi) / 2);
else if (rDist[0][o.se] > D + o.fi + eps) {
rDist[0][o.se] = D + o.fi;
q.emplace(rDist[0][o.se], o.se);
}
}
}
}
double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr) {
n = N; fin = H;
for (int i = 0; i < n; i++) {
adj[i].clear();
abi[i] = arr[i];
}
for (int i = 0; i < M; i++) {
adj[x[i]].emplace_back(c[i], y[i]);
adj[y[i]].emplace_back(c[i], x[i]);
}
Dijkstra();
if (dist[0] == -1) return -1;
double ans = dist[0];
vector<int> posGo;
fill(rDist[0], rDist[0] + n, oo);
fill(rDist[1], rDist[1] + n, oo);
for (int i = 0; i < n; i++)
if (!abi[i] && dist[i] != -1) {
ans = min(ans, (double)dist[i]);
rDist[1][i] = 0;
posGo.push_back(i);
}
// Travel 1st time
Ford_Bellman(posGo);
// Update posGo
posGo.clear();
for (int i = 0; i < n; i++)
if (abi[i] == 1 && rDist[1][i] < oo) posGo.push_back(i);
// Travel K-1 times
mini(K, 70); /// important;
for (int t = 2; t <= K; t++) Ford_Bellman(posGo);
for (int i : posGo) ans = min(ans, rDist[1][i] + dist[i]);
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |