惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Heimdal Security Blog
A
Arctic Wolf
K
Kaspersky official blog
V
Vulnerabilities – Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Simon Willison's Weblog
Simon Willison's Weblog
L
LINUX DO - 热门话题
MongoDB | Blog
MongoDB | Blog
T
Threat Research - Cisco Blogs
D
Docker
爱范儿
爱范儿
T
Tenable Blog
C
Check Point Blog
B
Blog
C
Cisco Blogs
Vercel News
Vercel News
The Cloudflare Blog
T
Threatpost
NISL@THU
NISL@THU
T
Tor Project blog
V2EX - 技术
V2EX - 技术
P
Palo Alto Networks Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Tailwind CSS Blog
G
GRAHAM CLULEY
P
Privacy & Cybersecurity Law Blog
SecWiki News
SecWiki News
博客园 - 司徒正美
S
Security @ Cisco Blogs
GbyAI
GbyAI
S
Secure Thoughts
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
Recorded Future
Recorded Future
Cloudbric
Cloudbric
Webroot Blog
Webroot Blog
N
News and Events Feed by Topic
Y
Y Combinator Blog
博客园_首页
T
Troy Hunt's Blog
The Hacker News
The Hacker News
雷峰网
雷峰网
Google DeepMind News
Google DeepMind News
U
Unit 42
AWS News Blog
AWS News Blog
PCI Perspectives
PCI Perspectives
V
Visual Studio Blog
博客园 - 聂微东
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell

distjr_的博客

最新 | 画中少女 | distjr_的博客 天津行记 | distjr_的博客 最终选择去天津大学了 | distjr_的博客 上伊那牡丹,看完了 | distjr_的博客 上伊那牡丹,看完了 | distjr_的博客 帖子测试 | distjr_的博客 最新 | 关于我的高中三年 | distjr_的博客 最新 | KeepOpen:一款保持移动硬盘开启状态的小工具 | distjr_的博客 最新 | KeepOpen:一款保持移动硬盘开启状态的小工具 | distjr_的博客 最新 | 关于我的高中三年 | distjr_的博客 一款基于Python的随机学生点名器 | distjr_的博客 最新 | 一款基于Python的随机学生点名器 | distjr_的博客 U571839 千本桜 题解 | distjr_的博客 土地 | distjr_的博客 土地 | distjr_的博客 一九六八年 | distjr_的博客 一九六八年 | distjr_的博客 原野 | distjr_的博客 原野 | distjr_的博客 一种基于人工智能技术的系统发生树绘制与物种演化路径推断系统 | distjr_的博客 一种基于人工智能技术的系统发生树绘制与物种演化路径推断系统 | distjr_的博客 回来?Ⅱ | distjr_的博客 回来?Ⅱ | distjr_的博客 U314392 distjr_想买书 题解 | distjr_的博客 U314392 distjr_想买书 题解 | distjr_的博客 天涯边 | distjr_的博客 天涯边 | distjr_的博客 CF509D Restoring Numbers 题解 | distjr_的博客 CF509D Restoring Numbers 题解 | distjr_的博客 回来?Ⅰ | distjr_的博客 回来?Ⅰ | distjr_的博客 一场提前告知的自杀 | distjr_的博客 一场提前告知的自杀 | distjr_的博客 test | distjr_的博客 test | distjr_的博客 置顶 | 关于我自己 | distjr_的博客 置顶 | 关于我自己 | distjr_的博客
U571839 千本桜 题解 | distjr_的博客
文章作者: distjr_ · 2025-08-21 · via distjr_的博客

思路

本题是一个最小直径生成树(MDST)的模板题。

详细的思路等请参考OI-Wiki上的讲解。

std

/**
 * @file hatsune.cpp
 * @brief Solution of Luogu U571839
 * @date 2025-08-21
 */
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;

constexpr int MAXN = 510;
ll d[MAXN][MAXN], rk[MAXN][MAXN], val[MAXN];
constexpr ll INF = 1e17;
int n, m;

bool cmp(int a, int b) { return val[a] < val[b]; }

void floyd()
{
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

struct node
{
    ll u, v, w;
} a[MAXN * (MAXN - 1) / 2];

int main()
{
    for (int i = 1; i <= 501; ++i)
        for (int j = 1; j <= 501; ++j)
            d[i][j] = INF;
    for (int i = 1; i <= 501; ++i)
        d[i][i] = 0;
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= m; ++i)
    {
        ll u, v, w;
        scanf("%lld %lld %lld", &u, &v, &w);
        w *= 2;
        d[u][v] = w, d[v][u] = w;
        a[i].u = u, a[i].v = v, a[i].w = w;
    }
    floyd();
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            rk[i][j] = j;
            val[j] = d[i][j];
        }
        sort(rk[i] + 1, rk[i] + 1 + n, cmp);
    }
    ll P = 0, ansP = INF;
    for (int i = 1; i <= n; i++)
    {
        if (d[i][rk[i][n]] * 2 < ansP)
        {
            ansP = d[i][rk[i][n]] * 2;
            P = i;
        }
    }
    int f1 = 0, f2 = 0;
    ll disu = INT_MIN, disv = INT_MIN, ansL = INF;
    for (int i = 1; i <= m; i++)
    {
        ll u = a[i].u, v = a[i].v, w = a[i].w;
        for (int p = n, i = n - 1; i >= 1; i--)
        {
            if (d[v][rk[u][i]] > d[v][rk[u][p]])
            {
                if (d[u][rk[u][i]] + d[v][rk[u][p]] + w < ansL)
                {
                    ansL = d[u][rk[u][i]] + d[v][rk[u][p]] + w;
                    f1 = u, f2 = v;
                    disu = (d[u][rk[u][i]] + d[v][rk[u][p]] + w) / 2 - d[u][rk[u][i]];
                    disv = w - disu;
                }
                p = i;
            }
        }
    }
    printf("%lld\n", min(ansP, ansL) / 2);
    return 0;
}