이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <fstream>
using namespace std;
/* -------------------- Typedefs -------------------- */
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef float fl;
typedef double db;
typedef long double ld;
/* -------------------- Usings -------------------- */
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
/* -------------------- Defines -------------------- */
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
/* -------------------- Constants -------------------- */
const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18 + 5);
const ll MOD = ll(1e9 + 7);
const ll MOD2 = ll(998244353);
/* -------------------- Functions -------------------- */
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void precision(int x) {
cout.setf(ios::fixed | ios::showpoint);
cout.precision(x);
}
ll gcd(ll a, ll b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll range_sum(ll a, ll b) {
ll dif = a - 1, cnt = b - a + 1;
ll ans = ((b - a + 1) * (b - a + 2)) / 2;
ans += ((b - a + 1) * dif);
return ans;
}
string dec_to_bin(ll a) {
string s = "";
for (ll i = a; i > 0; ) {
ll k = i % 2;
i /= 2;
char c = k + 48;
s += c;
}
if (a == 0) {
s = "0";
}
reverse(all(s));
return s;
}
ll bin_to_dec(string s) {
ll num = 0;
for (int i = 0; i < s.size(); i++) {
num *= 2ll;
num += (s[i] - '0');
}
return num;
}
int isPrime(ll a) {
if (a == 1) return 0;
for (ll i = 2; i * i <= a; i++) {
if (a % i == 0) return 0;
}
return 1;
}
ll binpow(ll a, ll b) {
if (!a) return 0;
ll ans = 1;
while (b) {
if (b & 1) {
ans *= a;
}
b >>= 1;
a *= a;
}
return ans;
}
ll binpow_by_mod(ll a, ll b, ll mod) {
if (!a) return 0;
ll ans = 1;
while (b) {
if (b & 1) {
ans *= a;
ans %= mod;
}
b >>= 1;
a *= a;
a %= mod;
}
return ans;
}
/* -------------------- Solution -------------------- */
const int N = 100005, M = 50;
vector<pii> g[N], gr[N];
int used[N], p[N], sz[N], tin[N], tout[N], timer, l;
pii up[N][M];
bool f;
void make_set(int u) {
p[u] = u, sz[u] = u;
}
int find_set(int a) {
if (a == p[a]) return a;
return p[a] = find_set(p[a]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a == b) return;
f = true;
if (sz[a] > sz[b]) swap(a, b);
p[a] = b;
sz[b] += sz[a];
}
void dfs(int u, int par = 1, int len = 0) {
tin[u] = timer++, up[u][0] = { par,len };
for (int i = 1; i <= l; i++) {
int len1 = min(up[up[u][i - 1].ff][i - 1].ss, up[u][i - 1].ss);
up[u][i] = { up[up[u][i - 1].ff][i - 1].ff,len1 };
}
for (auto to : gr[u]) {
if (to.ff != par) dfs(to.ff, u, to.ss);
}
tout[u] = timer++;
}
bool upper(int a, int b) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
int lca(int a, int b) {
for (int i = l; i >= 0; i--) {
if (!upper(up[a][i].ff, b)) a = up[a][i].ff;
}
return up[a][0].ff;
}
int getmin(int a, int b) {
int ans = MAX;
for (int i = l; i >= 0; i--) {
if (!upper(up[a][i].ff, b)) {
ans = min(ans, up[a][i].ss);
a = up[a][i].ff;
}
}
ans = min(ans, up[a][0].ss);
return ans;
}
void slv() {
int n, m, k, q;
cin >> n >> m;
vector<pair<int, pii>> v(m);
for (int i = 0; i < m; i++) {
cin >> v[i].ss.ff >> v[i].ss.ss >> v[i].ff;
g[v[i].ss.ff].pub({ v[i].ss.ss,v[i].ff });
g[v[i].ss.ss].pub({ v[i].ss.ff,v[i].ff });
}
cin >> k;
vi d(n + 1, MAX);
priority_queue<pii> pq;
for (int i = 0; i < k; i++) {
int x;
cin >> x;
pq.push({ 0,x });
used[x] = 1, d[x] = 0;
}
while (!pq.empty()) {
int u = pq.top().ss, len = -pq.top().ff;
pq.pop();
if (len > d[u]) continue;
for (auto to : g[u]) {
len = to.ss;
if (len + d[u] < d[to.ff]) {
d[to.ff] = len + d[u];
pq.push({ -d[to.ff], to.ff });
}
}
}
for (auto& elem : v) {
elem.ff = -min(d[elem.ss.ff], d[elem.ss.ss]);
}
sort(all(v));
for (auto elem : v) {
int u1 = elem.ss.ff, u2 = elem.ss.ss;
if (p[u1] == 0) make_set(u1);
if (p[u2] == 0) make_set(u2);
f = false;
union_sets(u1, u2);
if (f) {
gr[u1].pub({ u2,-elem.ff });
gr[u2].pub({ u1,-elem.ff });
}
}
l = log2(n) + 1;
dfs(1);
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
if (upper(a, b)) {
cout << getmin(b, a) << '\n';
continue;
}
if (upper(b, a)) {
cout << getmin(a, b) << '\n';
continue;
}
int lc = lca(a, b);
cout << min(getmin(a, lc), getmin(b, lc)) << '\n';
}
}
void cs() {
int tstc = 1;
//cin >> tstc;
while (tstc--) {
slv();
}
}
void precalc() {
return;
}
int main() {
fastio();
precalc();
//precision(0);
cs();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
plan.cpp: In function 'll range_sum(ll, ll)':
plan.cpp:90:21: warning: unused variable 'cnt' [-Wunused-variable]
90 | ll dif = a - 1, cnt = b - a + 1;
| ^~~
plan.cpp: In function 'll bin_to_dec(std::string)':
plan.cpp:113:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
113 | for (int i = 0; i < s.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |