跳到正文

个人记录

4.7总结

Wonderful_Anser约 14 分钟
原文
文章信息与版本

文章信息

发布
2026年4月8日
收录
2026年4月8日
更新
2026年11月14日
篇幅
约 14 分钟

版本

  1. v2当前
  2. v1

4.7总结

考试情况

Cow Hurdles S最小花费灾后重建[USACO10FEB] Chocolate Giving Stree[SEERC 2018]总分
10pts WA\footnotesize\color{red}\textsf{\textbf{10pts WA}}AC\footnotesize\color{green}\textsf{\textbf{AC}}20pts TLE\footnotesize\color{red}\textsf{\textbf{20pts TLE}}TLE\footnotesize\color{red}\textsf{\textbf{TLE}}WA\footnotesize\color{red}\textsf{\textbf{WA}}130pts\footnotesize\color{orange}\textsf{\textbf{130pts}}

题目

Cow Hurdles S

错误原因

没有初始化,判重边

正确代码

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 305;
int dp[N][N], n, m, t;
void floyd()
{
    for (int k = n; k >= 1; k--)
        for (int i = n; i >= 1; i--)
            for (int j = n; j >= 1; j--)
                dp[i][j] = min(dp[i][j], max(dp[i][k], dp[k][j]));
}
int main()
{
    cin >> n >> m >> t;
    memset(dp, 0x3f, sizeof dp); // 初始化
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        dp[u][v] = min(dp[u][v], w); // 判重边
    }
    for (int i = 1; i <= n; i++) // 初始化
        dp[i][i] = 0;
    floyd();
    while (t--)
    {
        int a, b;
        cin >> a >> b;
        if (dp[a][b] < 1e9)
            cout << dp[a][b] << "\n";
        else
            cout << -1 << "\n";
    }
    return 0;
}

灾后重建

错误原因

方法错误导致TLE,使用Dijkstra处理每个查询,但应使用Floyd进行预处理后再处理每个询问

正确解法

普通Floyd是“一次性把所有路都算死”,而这道题是“兵来将挡,水来土掩”,跟着时间走,每来一个新询问,才把当前能用的新村子加进去接着算,主打一个增量更新。

C++
#include <bits/stdc++.h>
using namespace std;
int n, m, q, a[205], dp[205][205];
void f(int k)
{
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
    return ;
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> a[i];
    memset(dp, 0x3f, sizeof dp);
    for(int i = 1; i <= m; i++)
    {
        int x, y, t;
        cin >> x >> y >> t;
        x++, y++;
        dp[x][y] = dp[y][x] = min(dp[x][y], t);
    }
    for(int i = 1; i <= n; i++)
        dp[i][i] = 0;
    cin >> q;
    int c = 1;
    while (q--)
    {
        int x, y, t;
        cin >> x >> y >> t;
        x++, y++;
        while (a[c] <= t && c <= n) // 枚举中转点,但不能都算进去
            f(c++);
        if (a[x] > t || a[y] > t) // 如果x、y中有村子在时刻t时还没修好,直接无解
        {
            cout << "-1" << "\n";
            continue;
        }
        if (dp[x][y] > 1e9) // 如果走不通业也无解
        {
            cout << "-1" << "\n";
            continue;
        }
        cout << dp[x][y] << "\n"; // 有解则直接输出
    }
    return 0;
}

[USACO10FEB] Chocolate Giving S

错误原因

没有想到优化方法

C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int y, w;
    bool operator < (const node & tmp) const
    {
        return w > tmp.w;
    }
} ;
int dis[50005], n, m;
vector<node> nbr[50005];
bool vis[50005];
void dij(int s)
{
    priority_queue<node> q;
    memset(vis, false, sizeof vis);
    memset(dis, 0x3f, sizeof dis);
    dis[s] = 0;
    q.push({s, dis[s]});
    while (!q.empty())
    {
        int x = q.top().y;
        q.pop();
        if (vis[x])
            continue;
        vis[x] = true;
        for (auto a : nbr[x])
        {
            int y = a.y, w = a.w;
            if (dis[x] + w < dis[y])
            {
                dis[y] = dis[x] + w;
                q.push({y, dis[y]});
            }
        }
    }
}
int main()
{
    int q;
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        nbr[u].push_back({v, w});
        nbr[v].push_back({u, w});
    }
    while (q-
-)
    {
        int x, y;
        cin >> x >> y;
        // 此处可以优化
        dij(x);
        int ans = dis[1];
        dij(1);
        ans += dis[y];
        cout << ans << "\n";
    }
    return 0;
}

正解

因为无向边的特性,我们可以只对点 11 进行Dijkstra然后输出 dis[x] + dis[y] 即可

C++
#include <bits/stdc++.h>
using namespace std;
struct node
{
    int y, w;
    bool operator < (const node & tmp) const
    {
        return w > tmp.w;
    }
} ;
int dis[50005], n, m;
vector<node> nbr[50005];
bool vis[50005];
void dij(int s)
{
    priority_queue<node> q;
    memset(vis, false, sizeof vis);
    memset(dis, 0x3f, sizeof dis);
    dis[s] = 0;
    q.push({s, dis[s]});
    while (!q.empty())
    {
        int x = q.top().y;
        q.pop();
        if (vis[x])
            continue;
        vis[x] = true;
        for (auto a : nbr[x])
        {
            int y = a.y, w = a.w;
            if (dis[x] + w < dis[y])
            {
                dis[y] = dis[x] + w;
                q.push({y, dis[y]});
            }
        }
    }
}
int main()
{
    int q;
    cin >> n >> m >> q;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        nbr[u].push_back({v, w});
        nbr[v].push_back({u, w});
    }
    dij(1); // 只需对点1进行Dijkstra
    while (q--)
    {
        int x, y;
        cin >> x >> y;
        cout << dis[x] + dis[y] << "\n";
    }
    return 0;
}

tree[SEERC 2018]

正解思路

  1. 先跑Floyd求出任意两点间的最短路 dpi,jdp_{i, j}

  2. 枚举最大黑点点对距离 dpi,jdp_{i, j}, 确定最大距离后再枚举其余的黑点 kk,判断 kk 到 ii 和 kk 到 jj 的距离是否在 dpi,jdp_{i, j} 以内,是的话就 cnt+1cnt + 1

  3. 如果 cnt≥mcnt \ge m 将 ansans 设为 min⁡ans,dpi,j\min{ans, dp_{i, j}},最后答案为 ansans

  4. 时间复杂度:O(n3)O(n^3)

C++
#include <bits/stdc++.h>
using namespace std;
int n, m, p[105], dp[105][105];
void floyd()
{
    for (int k = 1; k <= n; k++)
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> p[i];
    memset(dp, 0x3f, sizeof dp);
    for (int i = 1; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        dp[u][v] = dp[v][u] = 1;
    }
    for (int i = 1; i <= n; i++)
        dp[i][i] = 0;
    floyd();
    int ans = n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
        {
            if (p[i] != 1 || p[j] != 1)
                continue;
            int cnt = 0;
            for (int k = 1; k <= n; k++)
                if (p[k] == 1 && dp[i][k] <= dp[i][j] && dp[j][k] <= dp[i][j])
                    cnt++;
            if (cnt >= m)
                ans = min(ans, dp[i][j]);
        }
    cout << ans;
    return 0;
}
原文