P14362 题解

· · 题解

此题解原创 @LiQuiditY(思路 & 代码),作者 @zajasi。由于原创不会 LaTeX,作者发过一篇题解了,于是用了小号。

不保证正确性,但是时间复杂度加上卡常绝对是比现在最优解优的。此做法的复杂度为 \Theta(k^2\times n\log n),比现在的常规题解 \Theta(2^k\times n\log n) 在极限数据下明显是更优的。

但是再重申一遍,不保证正确性。但在目前的民间数据下是对的。如果有反例欢迎私信 @zajasi 进行沟通。

思路

首先可以证明:如果说在某一个状态下,不管买哪个乡村,都不可以使答案更优,那在当前状态下肯定没有更优的做法能够改变答案了。证明也很简单,如果买一个乡村不能够优化答案,那买两个肯定也不行。直接返回答案。

否则,如果有乡村可以优化答案,每一次取可以改变答案最大的村庄,将其加入边,把它作为图的一部分。这里的加和的正确性有待补充,欢迎各路大神证明 / 证伪。

于是你会发现,最多加 k 个村庄,每一次扫 k 个村庄看是否有优化空间,于是前面的 \Theta(2^k) 便优化成了 \Theta(k^2)。

通过了民间数据。

code

#include <bits/stdc++.h>
using namespace std;
#define int long long

struct node
{
    int u, v, w;
} e[2000010], e1[2000010], e2[2000010];

int v[15][1000010];
int f[1000010];
int cnt;
int n, m, k;
int t;
int l = 1;
bool h[1000010];

void init()
{
    for (int i = 1; i <= n + 100; i++)
    {
        f[i] = i;
    }
}

int F(int x)
{
    return f[x] == x ? x : f[x] = F(f[x]);
}

bool cmp(node x, node y)
{
    if (x.w < y.w)
    {
        return true;
    }
    return false;
}

int work(int cnt)
{
    init();
    int c = 0;
    int ans = 0;
    sort(e + 1, e + cnt, cmp);
    for (int i = 1; i < cnt; i++)
    {
        int xx = F(e[i].u), yy = F(e[i].v);
        if (xx == yy)
        {
            continue;
        }
        f[xx] = yy;
        c++;
        ans += e[i].w;
        e2[l].u = e[i].u, e2[l].v = e[i].v, e2[l].w = e[i].w;
        l++;
    }
    return ans;
}

main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
    {
        cin >> e[i].u >> e[i].v >> e[i].w;
    }
    cnt = m + 1;
    for (int i = 1; i <= k; i++)
    {
        for (int j = 0; j <= n; j++)
        {
            cin >> v[i][j];
        }
    }
    int z = work(cnt);
    for (int i = 1; i < l; i++)
    {
        e[i] = e2[i];
    }
    // cout << z << "\n";
    // return 0;
    cnt = l;
    int sum = 0;
    int lt = 0;
    for (int i = 1; i <= k; i++)
    {// 因为一共 k 个村庄,所以扫 k 遍一定能出。
        int mn = z, id = 0;
        // 下面的 j 循环就是在看有没有买乡村能带来的优化空间。
        for (int j = 1; j <= k; j++)
        {
            init();
            if (h[j] == 1)
            {
                continue;
            }
            lt = sum + v[j][0];
            for (int p = 1; p < cnt; p++)
            {
                e1[p] = e[p];
            }
            t++;
            for (int p = 1; p <= n; p++)
            {
                e[cnt].u = j + n, e[cnt].v = p, e[cnt].w = v[j][p];
                cnt++;
            }
            lt += work(cnt);
            if (lt < mn)
            {
                mn = lt;
                id = j;
            }
            cnt -= n;
            for (int p = 1; p < cnt; p++)
            {
                e[p] = e1[p];
            }
            t--;
        }
        if (mn == z)
        {
            cout << z;
            return 0;
        }
        // 如果发现更优,直接把村庄加图里。
        z = mn;
        t++;
        sum += v[id][0];
        h[id] = 1;
        for (int p = 1; p <= n; p++)
        {
            e[cnt].u = id + n, e[cnt].v = p, e[cnt].w = v[id][p];
            cnt++;
        }
    }
    cout << z;
    return 0;
}

再次感谢 @LiQuiditY 提供的目前好像还没假的思路。

如发现有问题,欢迎评论 / 私信 @zajasi。