比赛 |
CSP2023-S模拟赛 |
评测结果 |
AAAAAAEEEEEEEEEEEEEE |
题目名称 |
坡伊踹 |
最终得分 |
30 |
用户昵称 |
心灵震荡 |
运行时间 |
2.624 s |
代码语言 |
C++ |
内存使用 |
4.77 MiB |
提交时间 |
2023-10-17 19:03:43 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1005, M = 200005, inf = 2e18;
int n, q, a[N], head[N], tot, u, v, w, dis[N], ans;
bool vis[N], flag, flag_flower = 1;
vector<int> g;
map<pair<int, int>, int> mp;
struct edge {int to, nxt, val;} e[N << 1];
void add_edge(int u, int v, int w)
{
e[++tot] = {v, head[u], w}, head[u] = tot;
}
void dfs(int x)
{
if(flag) return;
if(x == v)
{
flag = 1;
for(auto h : g) ans = min(ans, max(a[h], dis[h]));
return;
}
for(int i = head[x]; i; i = e[i].nxt)
{
if(!vis[e[i].to])
{
int tt = dis[e[i].to];
vis[e[i].to] = 1, dis[e[i].to] = dis[x] + e[i].val, g.push_back(e[i].to);
dfs(e[i].to);
vis[e[i].to] = 0, dis[e[i].to] = tt, g.pop_back();
}
}
return;
}
void solve_flower()
{
while(q--)
{
cin >> u >> v;
if(u == v) cout << max(a[u], 0ll) << '\n';
else if(u == 1) cout << min(a[u], max(a[v], mp[{u, v}])) << '\n';
else if(v == 1) cout << min(a[v], max(a[u], mp[{v, u}])) << '\n';
else cout << min(a[u], min(max(a[1], mp[{u, 1}]), max(a[v], mp[{u, 1}] + mp[{1, v}]))) << '\n';
}
return;
}
signed main()
{
freopen("poitry.in", "r", stdin);
freopen("poitry.out", "w", stdout);
ios :: sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i < n; i++)
{
cin >> u >> v >> w;
add_edge(u, v, w);
add_edge(v, u, w);
flag_flower &= (u == 1);
mp[{u, v}] = mp[{v, u}] = 1;
}
if(flag_flower) {solve_flower(); return 0;}
while(q--)
{
cin >> u >> v;
flag = 0, ans = inf, g.clear();
memset(vis, 0, sizeof vis);
memset(dis, 0x3f, sizeof dis);
g.push_back(u), vis[u] = 1, dis[u] = 0;
dfs(u);
cout << ans << '\n';
}
return 0;
}