社区讨论
中样例过了,1-8全WA
P11324【MX-S7-T2】「SMOI-R2」Speaker参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @m3vbhuu4
- 此快照首次捕获于
- 2024/11/24 16:08 去年
- 此快照最后确认于
- 2025/11/04 14:01 4 个月前
超级裸的暴力,中样例也过了,就是不知道哪错了,求条
CPP#include <bits/stdc++.h>
constexpr auto maxn = 2050;
class edge
{
public:
int to;
int next;
int dis;
};
class node
{
public:
int idx;
long long dis;
bool operator>(node const& other) const
{
return other.dis > this->dis;
}
};
edge edges[maxn * 2];
int heads[maxn];
bool visited[maxn];
long long dis[maxn][maxn];
int n, q, c[maxn], u, v, w, x, y;
auto cnt = 0;
void add(int u, int v, int w)
{
edges[++cnt].to = v;
edges[cnt].next = heads[u];
edges[cnt].dis = w;
heads[u] = cnt;
}
void dijkstra(int start)
{
std::priority_queue<node, std::vector<node>, std::greater<node>> q;
dis[start][start] = 0;
q.push(node{start, 0});
while (!q.empty())
{
auto now = q.top().idx;
q.pop();
if (visited[now])
{
continue;
}
visited[now] = true;
for (auto i = heads[now]; i; i = edges[i].next)
{
if (dis[start][edges[i].to] > dis[start][now] + edges[i].dis)
{
dis[start][edges[i].to] = dis[start][now] + edges[i].dis;
if (!visited[edges[i].to])
{
q.push(node{edges[i].to, dis[start][edges[i].to]});
}
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
std::cin >> n >> q;
for (auto i = 1; i <= n; i++)
{
std::cin >> c[i];
}
for (auto i = 1; i < n; i++)
{
std::cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
for (auto i = 1; i <= n; i++)
{
for (auto j = 1; j <= n; j++)
{
visited[j] = false;
dis[i][j] = 1e18;
}
dijkstra(i);
}
while (q--)
{
std::cin >> x >> y;
long long ans = -1e18;
for (auto i = 1; i <= n; i++)
{
ans = std::max(ans, c[x] + c[i] + c[y] - dis[x][i] - dis[i][y]);
}
std::cout << ans << "\n";
}
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...