一道神题,一开始还想了很多,什么博弈论之类的,233,还是太年轻啊。
看了看别人的题解后,茅塞顿开,可以说是很可以的。
我们要知道每个杯子下的情况,就要知道每一个(废话),所以就是最小代价知道所有,一个区间[i,j],知道[1,i-1],[1,j],就知道了,而且三个是知二推一,所以我们把0~n连起来就好了(感性的认识一下),想到了什么?
对,最小生成树!连i-1到j,再跑一遍就是答案了。
#include<bits/stdc++.h>
#define N 2000
using namespace std;
int n,m,f[N+1];
long long ans;
struct edge{
int x;
int y;
int len;
bool operator < (const edge &A) const
{
return len<A.len;
}
};edge A[N*N+1];
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
int x=0,b=1;
char c=nc();
for(;!(c<='9'&&c>='0');c=nc())if(c=='-')b=-1;
for(;c<='9'&&c>='0';c=nc())x=x*10+c-'0';
return x*b;
}
inline int find(int x)
{
return x==f[x]?f[x]:f[x]=find(f[x]);
}
int main()
{
//freopen("in.txt","r",stdin);
n=read();
for(int i=1;i<=n;i++)
for(int j=i;j<=n;j++)
A[++m].x=i-1,A[m].y=j,A[m].len=read();
for(int i=0;i<=n;i++)f[i]=i;
sort(A+1,A+m+1);
for(int i=1;i<=m;i++)
{
int fx=find(A[i].x),fy=find(A[i].y);
if(fx==fy)continue;
f[fx]=fy;
ans+=A[i].len;
}
cout<<ans;
return 0;
}
话说fread真是好东西,怎么写怎么快。