题目大意:给出求每个区间{l,r}的异或和的花费cost[i][j],求知道每一个元素的最小花费
分析:考试的时候想成了线性基(明明考图论了),结果就GG了.正解是MST,把每个询问当做边,前缀和(似乎可以这么说)当做点,做MST
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e3+10;
struct Edge{
int from,to,val;
bool operator < (const Edge &rhs) const {
return rhs.val>val;
}
Edge(int from=0,int to=0,int val=0):from(from),to(to),val(val){}
}e[N*N];
int len=0;
void add(int from,int to,int val) {
e[++len]=Edge(from,to,val);
}
int n,map[N][N],
fa[N];
int Find(int x){return fa[x]==x?fa[x]:fa[x]=Find(fa[x]);}
int Kruskal() {
fo(i,0,n) fa[i]=i;
int ans=0;
sort(e+1,e+1+len);
for(int i=1;i<=len;i++) {
int fu=Find(e[i].from),fv=Find(e[i].to);
if(fu==fv) continue;
else {
ans+=e[i].val;
fa[fu]=fv;
}
}
return ans;
}
int main() {
scanf("%d",&n);
fo(i,1,n) {
fo(j,1,n) {
scanf("%d",&map[i][j]);
if(i<=j) add(i-1,j,map[i][j]);
}
}
printf("%d\n",Kruskal());
return 0;
}