Bzoj3714: [PA2014]Kuglarz

题面

传送门

Sol

假如我们知道所有的前缀和数组,差分以下就得到了答案

对于一组区间\([l, r]\)
我们就知道了\(S[r]-S[l-1]\)的值,\(S\)即前缀和
那么如果把这看成一条边\((l-1,r)\),那么最后只需要所有的点联通就可以求出所有点对之间的关系
那不就是最小生成树了
直接\(n^2\)\(prim\)

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2005);
typedef int Arr[_];
 
IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}
 
Arr dis, c[_], vis;
int n;
ll ans;
 
int main(RG int argc, RG char *argv[]){
    Fill(c, 63), n = Input();
    for(RG int i = 1; i <= n; ++i)
        for(RG int j = i; j <= n; ++j)
            c[j][i - 1] = c[i - 1][j] = Input();
    Fill(dis, 63), dis[0] = 0;
    for(RG int i = 0; i <= n; ++i){
        RG int p = n + 1;
        for(RG int j = 0; j <= n; ++j)
            if(!vis[j] && dis[j] < dis[p]) p = j;
        vis[p] = 1, ans += dis[p];
        for(RG int j = 0; j <= n; ++j)
            if(!vis[j]) dis[j] = min(dis[j], c[p][j]);
    }
    printf("%lld\n", ans);
    return 0;
}

posted @ 2018-03-28 13:26  Cyhlnj  阅读(129)  评论(0编辑  收藏  举报