# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1228125 | jfioashfn333 | Cyberland (APIO23_cyberland) | C++20 | 0 ms | 0 KiB |
#include "cyberland.h"
#include <vector>
#include <set>
#include <utility>
#include <limits>
#include <cmath>
#define int long long
#define pb push_back
#define eb emplace_back
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
using namespace std;
const int I = 1e18;
bool u[100005];
vector<pii> g[100005];
int t;
void f(int x) {
u[x] = 1;
if (x == t) return;
for (auto p : g[x]) {
if (!u[p.first]) f(p.first);
}
}
double solve(int n, int m, int k, int h, vector<int> x, vector<int> y, vector<int> c, vector<int> a) {
t = h;
for (int i = 0; i < n; i++) g[i].clear(), u[i] = 0;
for (int i = 0; i < m; i++) {
g[x[i]].eb(y[i], c[i]);
g[y[i]].eb(x[i], c[i]);
}
a[0] = 0;
f(0);
set<pair<int, int>> q;
vector<int> d(n, I);
d[0] = 0;
q.insert({0, 0});
for (int i = 0; i < n; i++) {
if (i && a[i] == 0 && u[i]) {
d[i] = 0;
q.insert({0, i});
}
}
while (!q.empty()) {
int x = q.begin()->second;
q.erase(q.begin());
for (auto p : g[x]) {
int y = p.first, w = p.second;
if (d[y] > d[x] + w) {
q.erase({d[y], y});
d[y] = d[x] + w;
q.insert({d[y], y});
}
}
}
if (d[h] == I) return -1;
return d[h];
}