T1
C++#include <algorithm>
#include <limits>
#include <vector>
#include <queue>
constexpr int MAXM = 1 << 5;
constexpr int64_t Infinity = 1e18;
int N, M, C[MAXM];
inline int cast(int pos, int hnd) noexcept {
return pos*N + hnd;
}
inline std::pair<int, int64_t> next(int pos, int hnd, int tgt) {
const int hup = std::max(hnd, tgt), hdw = std::min(hnd, tgt);
const int dpos = C[tgt];
const int cost = 2 * abs(dpos) + hup - hdw;
const int npos = pos + dpos;
const int ewt = cost;
if(npos <= 0 || pos > N) return {0, Infinity};
return {cast(npos, tgt), ewt};
}
#ifndef ONLINE_JUDGE
#define logf(...) (::qio::clog << ::qio::formatted(__VA_ARGS__))
#else
#define logf(...) (0)
#endif
int main() {
qio::cin >> N >> M;
int zpos = 0;
for(int i = 1; i <= M; ++i) {
qio::cin >> C[i];
if(!C[i])
zpos = i;
}
const int GN = 4e5;
std::vector<std::vector<std::pair<int, int64_t>>> E(GN + 1);
for(int i = 1; i <= N; ++i) // 第 i 层
for(int j = 1; j <= M; ++j) { // 从 槽位 j
const int u = cast(i, j);
for(int k = 1; k <= M; ++k) { // 到 槽位 k
auto const [v, w] = next(i, j, k);
if(w == Infinity) continue;
E[u].emplace_back(v, w),
logf("%d %d %lld\n", u, v, w);
}
}
std::vector<bool> V(GN + 1);
std::vector<int64_t> D(GN + 1, Infinity);
std::priority_queue<
std::pair<int, int>,
std::vector<std::pair<int, int>>,
std::greater<std::pair<int, int>>> Q;
const int S = cast(1, zpos);
D[S] = 0, Q.emplace(0, S);
while(!Q.empty()) {
auto const [_, u] = Q.top(); Q.pop();
if(V[u]) [[unlikely]] continue;
else [[likely]] V[u] = true;
for(auto const &[v, w]: E[u])
if(D[v] > D[u] + w)
Q.emplace(D[v] = D[u] + w, v);
}
int64_t ans = Infinity;
for(int i = 1; i <= M; ++i)
ans = std::min(ans, D[cast(N, i)]);
qio::cout << (ans != Infinity? ans: -1);
}
#pragma endregion
T2
C++#pragma region Main
#include <algorithm>
#include <vector>
#include <queue>
int main() {
int N, M, T;
qio::cin >> N >> M >> T;
std::vector<int> C(N + 1);
for(int i = 1; i <= N; ++i)
qio::cin >> C[i];
std::vector<std::vector<std::pair<int, int>>> E(N + 1);
for(int i = 0, u, v, w; i < M; ++i)
qio::cin >> u >> v >> w,
E[u].emplace_back(v, w),
E[v].emplace_back(u, w);
std::vector<bool> V(N + 1);
std::vector<std::pair<int, int>> P(N + 1, {-1, -1});
std::vector<std::vector<std::pair<int, int>>> B(N + 1);
std::vector<int64_t> D(N + 1, std::numeric_limits<int64_t>::max());
std::priority_queue<
std::pair<int, int>,
std::vector<std::pair<int, int>>,
std::greater<std::pair<int, int>>> Q;
const int S = 1;
D[S] = 0, Q.emplace(0, S);
while(!Q.empty()) {
auto const [_, u] = Q.top(); Q.pop();
if(V[u]) [[unlikely]] continue;
else [[likely]] V[u] = true;
if(P[u].first ^ -1)
B[P[u].first].emplace_back(u, P[u].second);
for(auto const &[v, w]: E[u])
if(D[v] > D[u] + w || (D[v] == D[u] + w && P[v].first > u))
P[v] = {u, w},
Q.emplace(D[v] = D[u] + w, v);
}
std::vector<int64_t> W(N + 1);
auto dfs = [&](auto &&self, int u) -> int64_t {
for(auto const &[v, w]: B[u])
W[u] += self(self, v);
return W[u] += C[u];
};
dfs(dfs, S);
int64_t ans = 0;
for(int i = 1; i <= N; ++i) {
const int64_t Wt = W[i] * (D[i] - T);
ans = std::max(ans, Wt);
qio::clog << qio::formatted("Wt #%d = %lld ans = %lld\n", i, Wt, ans);
}
qio::cout << ans;
}
#pragma endregion
T3
C++#include <vector>
std::vector<bool> C;
std::vector<int> D, P;
std::vector<std::vector<int>> E;
int N, K;
int p;
void dfs(int u, int fa) {
P[u] = fa;
for (int const &v: E[u]) {
if (v == fa) continue;
D[v] = D[u] + 1;
if (D[v] > D[p]) p = v;
dfs(v, u);
}
}
int d2 = 0;
void dp(int u, int f) {
for(int const &v: E[u]) {
if(v == f) continue;
const int wt = (C[u] && C[v]? -1: 1);
dp(v, u);
d2 = std::max(d2, D[v] + D[u] + wt);
D[u] = std::max(D[u], D[v] + wt);
}
}
int main() {
qio::cin >> N >> K;
E.resize(N + 1);
for(int i = 0, u, v; i < N - 1; ++i)
qio::cin >> u >> v,
E[u].emplace_back(v),
E[v].emplace_back(u);
D.resize(N + 1, 1e9), P.resize(N + 1);
C.resize(N + 1);
dfs(1, 0);
const int ll = p; D[p] = 0;
dfs(ll, 0);
const int rr = p, dl = D[p];
if(K == 1) {
qio::cout << int64_t(N * 2 - 2) - dl + 1;
return 0;
}
for(int i = rr; i; i = P[i])
C[i] = true;
std::fill(D.begin(), D.end(), 0);
dp(1, 0);
qio::cout << N * 2 - dl - d2;
}
#pragma endregion
Quick IO Library
C++#include <type_traits>
#include <utility>
#include <cstdlib>
#include <cstdint>
#include <cstdio>
#include <tuple>
#pragma region Quick IO
#ifndef ONLINE_JUDGE
#define SIO_DEBUG
#endif
#define BUFSIZE (1 << 20)
#define __isdigit(x) (x >= '0' && x <= '9')
bool static __isblank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
class __qreader {
private:
FILE *fptr;
bool eof_flag;
#ifdef SIO_DEBUG
char __gc() {
int ch = fgetc(fptr);
if (ch == EOF) {
eof_flag = true;
return ' ';
}
return (char)ch;
}
#else
char buf[BUFSIZE], *p1, *p2;
char __gc() {
if (p1 == p2) {
p2 = (p1 = buf) + fread(buf, 1, BUFSIZE, fptr);
if (p1 == p2) {
eof_flag = true;
return ' ';
}
}
return *p1++;
}
#endif
public:
#ifdef SIO_DEBUG
__qreader(FILE *file): fptr(file), eof_flag(false) {}
#else
__qreader(FILE *file): fptr(file), eof_flag(false), p1(buf), p2(buf) {}
#endif
template <class T,
std::enable_if_t<std::is_arithmetic<T>::value, bool> = true>
void read(T &x) {
double tmp = 1;
bool sign = 0;
x = 0;
char ch = __gc();
for (; !__isdigit(ch) && !eof_flag; ch = __gc())
if (ch == '-') sign = 1;
for (; __isdigit(ch) && !eof_flag; ch = __gc()) x = x * 10 + (ch - '0');
if (ch == '.')
for (ch = __gc(); __isdigit(ch) && !eof_flag; ch = __gc())
tmp /= 10.0, x += tmp * (ch - '0');
if (sign) x = -x;
}
char*reads(char *s) {
char ch = __gc();
for (; __isblank(ch) && !eof_flag; ch = __gc());
for (; !__isblank(ch) && !eof_flag; ch = __gc()) *s++ = ch;
*s = 0;
return s;
}
void readc(char &c) { for (c = __gc(); __isblank(c) && !eof_flag; c = __gc()); }
char* readsln(char *s) {
while (true) {
char ch = __gc();
if (eof_flag || ch == '\n') break;
if (ch != '\r') *s++ = ch;
}
*s = '\0';
return s;
}
bool eof() const { return eof_flag; }
};
class __qwriter {
private:
FILE *fptr;
#ifdef SIO_DEBUG
void __pc(const char &c) { fputc(c, fptr); }
#else
char buf[BUFSIZE], *pp;
void __pc(const char &c) {
if (pp - buf == BUFSIZE) fwrite(buf, 1, BUFSIZE, fptr), pp = buf;
*pp++ = c;
}
#endif
public:
#ifdef SIO_DEBUG
__qwriter(FILE *file): fptr(file) {}
#else
__qwriter(FILE *file): fptr(file), pp(buf) {}
~__qwriter() { fwrite(buf, 1, pp - buf, fptr); }
#endif
template <class T,
std::enable_if_t<std::is_integral<T>::value, bool> = true>
void write(T x) {
if (x < 0) x = -x, __pc('-');
static T sta[35];
T top = 0;
do {
sta[top++] = x % 10, x /= 10;
} while (x);
while (top) __pc(sta[--top] + '0');
}
template <class T,
std::enable_if_t<std::is_integral<T>::value, bool> = true>
void write(T x, char lastChar) { write(x), __pc(lastChar); }
void writec(char const& ch) { __pc(ch); }
void writes(char const* str, char lastChar = '\0') {
for(; *str; ++str) __pc(*str);
if(lastChar) __pc(lastChar);
}
template <class ...Targs>
void writef(char const* format, Targs ...args) {
static char fmt_buf[BUFSIZ];
sprintf(fmt_buf, format, args...);
writes(fmt_buf);
}
};
#pragma endregion
#pragma region Quick IO Streaming
namespace qio {
namespace file_operators {
__qreader __stdin = stdin;
__qwriter __stdout = stdout;
__qwriter __stderr = stderr;
};
class qistream;
class qostream;
namespace type_trait {
template<typename...> struct voider { using type = void; };
template<typename... Ts> using void_t = typename voider<Ts...>::type;
#define DEF_MODIFIER_TRAIT(trait_name, stream_type) \
template <typename T, typename = void> \
struct trait_name : std::false_type {}; \
template <typename T> \
struct trait_name<T, \
typename std::enable_if< \
std::is_same< \
decltype(std::declval<T>()(std::declval<stream_type&>())), \
stream_type& \
>::value \
>::type \
> : std::true_type {};
DEF_MODIFIER_TRAIT(is_qistream_modifier, qistream)
DEF_MODIFIER_TRAIT(is_qostream_modifier, qostream)
#undef DEF_MODIFIER_TRAIT
template <typename T, typename = void>
struct is_readable : std::false_type {};
template <typename T>
struct is_readable<T,
void_t<decltype(std::declval<T&>().read(std::declval<__qreader&>()))>>
: std::true_type {};
template <typename T, typename = void>
struct is_writable : std::false_type {};
template <typename T>
struct is_writable<T,
void_t<decltype(std::declval<const T&>().write(std::declval<__qwriter&>()))>>
: std::true_type {};
}
class qistream {
__qreader& reader;
public:
qistream(__qreader& reader): reader(reader) { }
template <typename T,
typename std::enable_if<std::is_arithmetic<T>::value, bool>::type = true>
qistream& operator>>(T& numeric) { this->reader.read(numeric); return *this; }
qistream& operator>>(char& character) { this->reader.readc(character); return *this; }
qistream& operator>>(char* string) { this->reader.reads(string); return *this; }
template <typename T,
typename std::enable_if<type_trait::is_readable<T>::value, bool>::type = true>
qistream& operator>>(T&& readable) {
std::forward<T>(readable).read(this->reader);
return *this;
}
template <typename T,
typename std::enable_if<type_trait::is_qistream_modifier<T>::value, bool>::type = true>
qistream& operator>>(T modifier) {
return modifier(*this);
}
bool eof() const { return reader.eof(); }
explicit operator bool() const { return !eof(); }
};
class qostream {
__qwriter& writer;
public:
qostream(__qwriter& writer): writer(writer) { }
template <typename T,
typename std::enable_if<std::is_integral<T>::value, bool>::type = true>
qostream& operator<<(T const& integeral) { this->writer.write(integeral); return *this; }
qostream& operator<<(char const& character) { this->writer.writec(character); return *this; }
qostream& operator<<(const char *string) { this->writer.writes(string); return *this; }
template <typename T,
typename std::enable_if<type_trait::is_writable<T>::value, bool>::type = true>
qostream& operator<<(T const& writable) {
writable.write(this->writer);
return *this;
}
template <typename T,
typename std::enable_if<type_trait::is_qostream_modifier<T>::value, bool>::type = true>
qostream& operator<<(T modifier) {
return modifier(*this);
}
};
qistream cin = file_operators::__stdin;
qostream cout = file_operators::__stdout;
qostream clog = file_operators::__stderr;
qostream cerr = file_operators::__stderr;
namespace functional {
template <class ...Targs>
class formatted_output {
const char *format;
std::tuple<typename std::decay<Targs>::type...> args;
public:
formatted_output(const char *format, Targs&& ...args):
format(format), args(std::forward<Targs>(args)...) { }
void write(__qwriter& writer) const {
apply_impl(writer, std::index_sequence_for<Targs...>{});
}
private:
template <std::size_t... Is>
void apply_impl(__qwriter& writer, std::index_sequence<Is...>) const {
writer.writef(format, std::get<Is>(args)...);
}
};
class getline_request {
char *const target;
public:
getline_request(char *string): target(string) { }
void read(__qreader &reader) {
reader.readsln(target);
}
};
}
template <class ...Targs>
functional::formatted_output<Targs...> formatted(char const* format, Targs &&...args) {
return functional::formatted_output<Targs...>(format, std::forward<Targs>(args)...);
}
functional::getline_request readline(char *string) {
return functional::getline_request(string);
}
qostream& endl(qostream& stream) { return stream << '\n'; }
}
#pragma endregion