# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
919937 | XXBabaProBerkay | Cyberland (APIO23_cyberland) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
#define F first
#define S second
using ll = long long;
const int INF = 1e9 + 7;
double solve(int N, int M, int K, int H, vector<int> x, vector<int> y, vector<int> c, vector<int> arr)
{
vector<vector<pair<int, double>>> adj(N);
for (int i = 0; i < M; i++)
{
adj[x[i]].emplace_back(y[i], (double)c[i]);
adj[y[i]].emplace_back(x[i], (double)c[i]);
}
queue<int> Q;
vector<bool> vis(N);
Q.push(0);
vis[0] = 1;
while (!Q.empty())
{
int u = Q.front();
Q.pop();
for (pair<int, int> i : adj[u])
if (!vis[i.F] && i.F != H)
{
vis[i.F] = 1;
Q.push(i.F);
}
}
priority_queue<pair<double, int>, vector<pair<double, int>>, greater<pair<double, int>>> PQ;
vector<double> dist(N, 1e18);
for (int i = 1; i < N; i++)
if (vis[i] && !arr[i])
{
dist[i] = 0;
PQ.emplace(0, i);
}
dist[0] = 0;
PQ.emplace(0, 0);
while (!PQ.empty())
{
auto [d, u] = PQ.top();
PQ.pop();
if (d != dist[u])
continue;
for (pair<int, double> i : adj[u])
{
auto [v, w] = i;
if (dist[v] > dist[u] + w)
PQ.emplace(dist[v] = dist[u] + w, v);
}
}
vector<double> dist2(N, 1e18);
for (int i = 1; i < N; i++)
if (vis[i] && !arr[i])
{
dist2[i] = 0;
PQ.emplace(0, i);
}
dist2[0] = 0;
PQ.emplace(0, 0);
while (!PQ.empty())
{
auto [d, u] = PQ.top();
PQ.pop();
if (d != dist2[u])
continue;
for (pair<int, double> i : adj[u])
{
auto [v, w] = i;
if (arr[v] == 2 && dist2[v] > min(dist2[u] + w, dist[u] + w / 2.0))
PQ.emplace(dist2[v] = min(dist2[u] + w, dist[u] + w / 2.0), v);
else if (arr[v] != 2 && dist2[v] > dist2[u] + w)
PQ.emplace(dist2[v] = dist2[u] + w, v);
}
}
return (dist[H] == 1e18 ? -1 : (double)dist[H]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M, K;
cin >> N >> M >> K;
int H;
cin >> H;
vector<int> arr(N), x(M), y(M), z(M);
for (int i = 0; i < N; i++)
cin >> arr[i];
for (int i = 0; i < M; i++)
cin >> x[i] >> y[i] >> z[i];
cout << solve(N, M, K, H, x, y, z, arr);
}