「PA2014」Kuglarz

传送门
memset0好评

解题思路

其实这是一道图论题
不难发现,如果知道了 \(\sum1...i\)\(\sum1...j\) 的奇偶性,那么就可以得知 \(\sum i+1...j\) 的奇偶性,我们的目的就是求出所有每一个位置也就是 \(\sum i...i\) 的奇偶性。
我们可以将 \(i-1\)\(j\) 连一条边权为 \(c[i][j]\) 的边,表示花费这些代价的值可以得到 \(i-1...j\) 的奇偶性。
很容易发现只有且只要所有点全部联通就可以得到任意一个位置的奇偶性。
在稠密图上用 \(\text{Prim}\) 求最小生成树即可。

细节注意事项

  • 咕咕咕

参考代码

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#define rg register
using namespace std;
template < typename T > inline void read(T& s) {
    s = 0; int f = 0; char c = getchar();
    while (!isdigit(c)) f |= (c == '-'), c = getchar();
    while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
    s = f ? -s : s;
}

typedef long long LL;
const int _ = 2010;

int n, g[_][_], dis[_], vis[_];

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
#endif
    read(n);
    memset(g, 0x3f, sizeof g);
    for (rg int i = 1; i <= n; ++i)
        for (rg int j = i; j <= n; ++j)
            read(g[i - 1][j]), g[j][i - 1] = g[i - 1][j];
    LL mst = 0;
    memset(dis, 0x3f, sizeof dis);
    dis[0] = 0;
    for (rg int x = 0; x <= n; ++x) {
        int u = 2001;
        for (rg int i = 0; i <= n; ++i)
            if (!vis[i] && dis[i] < dis[u]) u = i;
        vis[u] = 1, mst += dis[u];
        for (rg int i = 0; i <= n; ++i)
            if (!vis[i]) dis[i] = min(dis[i], g[u][i]);
    }
    printf("%lld\n", mst);
    return 0;
}

完结撒花 \(qwq\)

猜你喜欢

转载自www.cnblogs.com/zsbzsb/p/11746523.html
今日推荐