比赛 |
20161115 |
评测结果 |
AWWWAAWWWWWWWWWWWWWW |
题目名称 |
树和机器人 |
最终得分 |
15 |
用户昵称 |
残星誓言 |
运行时间 |
0.126 s |
代码语言 |
C++ |
内存使用 |
1.25 MiB |
提交时间 |
2016-11-15 11:50:09 |
显示代码纯文本
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- using namespace std;
- const int maxn=50000+10;
- const int maxm=50000+10;
- int n,k,r;
- int ma=0;
- int head[maxn];
- bool vis[maxn];
- struct edges
- {
- int f;int t; int dist; int next;
- } e[maxm];
- int dfs(int x)
- {
- int re=0; vis[x]=1;
- for(int i=head[x];~i;i=e[i].next)
- {
- if(!vis[e[i].t])
- {
- int tp=dfs(e[i].t)+e[i].dist;
- if(x==r) ma=max(ma,tp);
- re+=tp;
- }
- }
- return re;
- }
- int tot=0;
- int ans=0;
- void ade(int f,int t,int d)
- {
- tot++;
- e[tot].f=f;
- e[tot].t=t;
- e[tot].dist=d;
- e[tot].next=head[f];
- head[f]=tot;
- }
- int main()
- {
- freopen("trobot.in","r",stdin);
- freopen("trobot.out","w",stdout);
- scanf("%d%d%d",&n,&r,&k);
- memset(vis,0,sizeof(vis));
- memset(head,-1,sizeof(head));
- for(int i=1;i<n;i++)
- {
- int a,b,c;
- scanf("%d%d%d",&a,&b,&c);
- ade(a,b,c);
- ade(b,a,c);
- ans+=c;
- }
- if(k==1)
- {
- int all=dfs(r);
- all=all*2-ma;
- printf("%d",all);
- }
- else
- {
- printf("%d",ans);
- }
- return 0;
- }