- 景点中心
- 2009-07-16 19:46:28 @
两遍dfs是用来建树的么?用链表怎么建?
2 条评论
-
Randle LV 9 @ 2017-07-09 08:43:50
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const long long root=1;
inline const void read(long long &a)
{
a=0;
char c=getchar();
while(c<'0'||c>'9')c=getchar();
while(c>='0'&&c<='9')
{
a=a*10+c-'0';
c=getchar();
}
}
long long n,father[100001],org_r[100001];
long long s[100001],r[100001],ex_s[100001],ex_r[100001],point[100001],ans[100001];
long long len[200001],nex[200001],to[200001],from[200001];
long long min_p=root;
void dp_1(long long p)
{
long long side=point[p];
r[p]=org_r[p];
while(side!=-1)
{
if(to[side]!=father[p])
{
father[to[side]]=p;
dp_1(to[side]);
r[p]+=r[to[side]];
s[p]+=s[to[side]]+r[to[side]]*len[side];
}
side=nex[side];
}
}
void dp_2(long long p)
{long long side=point[p];
while(side!=-1)
{
if(to[side]!=father[p])
{
ex_r[to[side]]=r[root]-r[to[side]]+org_r[to[side]];
ex_s[to[side]]=ans[p]+(r[root]-2*r[to[side]])*len[side]-s[to[side]];
ans[to[side]]=ex_s[to[side]]+s[to[side]];
if(ans[to[side]]<ans[min_p])min_p=to[side];
dp_2(to[side]);
}
side=nex[side];
}
}
int main()
{
//freopen("测试数据.txt","r",stdin);
read(n);
memset(s,0,sizeof(s));
memset(ex_s,0,sizeof(ex_s));
memset(r,0,sizeof(r));
memset(ex_r,0,sizeof(ex_r));
memset(ans,0,sizeof(ans));
for(long long i=1;i<=n;i++)
{
point[i]=-1;
read(org_r[i]);
}
for(long long i=0;i<=(n-1)*2-1;i+=2)
{
long long a,b,c;
read(a);read(b);read(c);
nex[i]=point[a];
nex[i^1]=point[b];
to[i]=from[i^1]=b;
to[i^1]=from[i]=a;
point[a]=i;
point[b]=i^1;
len[i]=len[i^1]=c;
}
father[root]=root;
dp_1(root);
ans[root]=s[root];
dp_2(root);
cout<<min_p<<endl<<ans[min_p];
return 0;
} -
2009-08-07 14:05:40@
这个么,你还是等大牛吧
这个么,你还是等大牛吧,我只知道这个是树形dp,我只知道我过了,仅此而已
- 1