记录编号 |
350554 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
树和机器人 |
最终得分 |
100 |
用户昵称 |
coolkid |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.326 s |
提交时间 |
2016-11-15 20:18:21 |
内存使用 |
7.37 MiB |
显示代码纯文本
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #define C(x) cout<<#x<<":"<<x<<endl
- using namespace std;
-
- const int MAXN=50000+20;
- int n,k,r;
-
- struct Edge{
- int to,dist,nxt;
- Edge(int t=0,int d=0,int n=0){
- to=t;dist=d;nxt=n;
- }
- }edges[MAXN<<1];
- int head[MAXN],cnt=0;
- int dp[MAXN][30];
-
- void addedge(int f,int t,int d){
- edges[cnt]=Edge(t,d,head[f]);head[f]=cnt++;
- edges[cnt]=Edge(f,d,head[t]);head[t]=cnt++;
- }
-
-
-
- void init(){
- memset(head,-1,sizeof(head));
- scanf("%d%d%d",&n,&r,&k);
- for(int i=1;i<n;i++){
- int x,y,z;
- scanf("%d%d%d",&x,&y,&z);
- addedge(x,y,z);
- }
- }
-
- void dfs(int u,int fa){
- for(int i=head[u];~i;i=edges[i].nxt){
- int v=edges[i].to;
- if(v!=fa){
- dfs(v,u);
- for(int j=k;j>=0;j--){
- dp[u][j]+=dp[v][0]+edges[i].dist*2;
- for(int l=1;l<=j;l++) dp[u][j]=min(dp[u][j],dp[v][l]+dp[u][j-l]+edges[i].dist*l);
- }
- }
- }
- }
-
- int main(){
- freopen("trobot.in","r",stdin);
- freopen("trobot.out","w",stdout);
- init();
- dfs(r,0);
- printf("%d\n",dp[r][k]);
- return 0;
- }