UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201420#3489. travelJosephcheng100584ms15452kbC++111.1kb2024-01-28 10:11:082024-01-28 12:04:05

answer

#include<bits/stdc++.h>
#define MAXN 200005
#define LL long long
using namespace std;

bool OPT;
bool vis[MAXN];
LL n,m,u,v,w,pos,ans;
LL head[MAXN];

struct Edge
{
	LL u,v,w,nxt;
}e[2*MAXN];

void addEdge(LL u,LL v,LL w)
{
	e[++pos]={u,v,w,head[u]};
	head[u]=pos;
}

LL DFS(LL st)
{
	if(head[st]==0) return 0;
	LL res=0;
	for(LL i=head[st];i;i=e[i].nxt)
		if(!vis[e[i].v]) vis[e[i].v]=true,res=max(res,(DFS(e[i].v)+e[i].w));
	return res;
}

void solve1()
{
	vis[1]=true;
	printf("%lld",m-DFS(1));
}

LL DP(LL st)
{
	if(head[st]==0) return 0;
	LL max1=0,max2=0;
	for(LL i=head[st];i;i=e[i].nxt){
		LL v=e[i].v,w=e[i].w;
		if(!vis[v]){
			vis[v]=true;
			LL res=DP(v);
			if(res+w>=max1) max2=max1,max1=res+w;
			else if(res+w>=max2) max2=res+w;
		}
	}
	ans=max(ans,max1+max2);
	return max1;
}

void solve2()
{
	vis[1]=true;
	DP(1);
	printf("%lld",m-ans);
}

int main()
{
	scanf("%d",&OPT);
	scanf("%lld",&n);
	for(int i=1;i<n;i++){
		scanf("%lld%lld%lld",&u,&v,&w);
		addEdge(u,v,w);
		addEdge(v,u,w);
		m+=2*w;
	}
	if(OPT==0) solve1();
	else solve2();
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

1
10
6 8 324690381
4 8 291661961
1 4 538084786
5 8 262366795
2 6 20323127
9 6 880215455
10 1 3908517...

output:

3516195924

result:

ok single line: '3516195924'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

1
10
9 3 151382372
7 9 935363696
1 3 678044530
5 3 699947578
4 1 399913162
6 1 58345716
8 6 78141905...

output:

7168022919

result:

ok single line: '7168022919'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1204kb

input:

1
10
7 2 948694788
1 2 10812389
4 2 454838489
8 4 236189669
5 1 441969681
9 4 445079149
6 8 63339094...

output:

3854338100

result:

ok single line: '3854338100'

Test #4:

score: 10
Accepted
time: 83ms
memory: 15452kb

input:

0
200000
187944 142836 224018023
187354 142836 534963736
195257 142836 436012071
81915 195257 136482...

output:

187546430624904

result:

ok single line: '187546430624904'

Test #5:

score: 10
Accepted
time: 85ms
memory: 15448kb

input:

0
200000
155676 189323 36693112
199769 189323 689732519
192749 155676 108455267
195053 199769 198489...

output:

187168473717222

result:

ok single line: '187168473717222'

Test #6:

score: 10
Accepted
time: 82ms
memory: 15452kb

input:

0
200000
83646 89797 298708645
177168 89797 973438255
179360 89797 517212339
169090 83646 63964573
1...

output:

187710879263941

result:

ok single line: '187710879263941'

Test #7:

score: 10
Accepted
time: 85ms
memory: 15444kb

input:

1
200000
147788 141394 450312861
183625 147788 22838150
190669 141394 42465082
196807 183625 5896058...

output:

187236123085527

result:

ok single line: '187236123085527'

Test #8:

score: 10
Accepted
time: 85ms
memory: 15444kb

input:

1
200000
192018 194091 4887055
197689 194091 546442613
166515 197689 975680386
147147 192018 7235273...

output:

187537538600571

result:

ok single line: '187537538600571'

Test #9:

score: 10
Accepted
time: 81ms
memory: 15448kb

input:

1
200000
177467 157577 923047282
150516 177467 258673231
136316 177467 658942112
142900 177467 81857...

output:

187187792305731

result:

ok single line: '187187792305731'

Test #10:

score: 10
Accepted
time: 83ms
memory: 15448kb

input:

1
200000
126048 198297 85770481
186042 126048 271470966
192904 126048 689381029
177445 126048 787080...

output:

186790088216710

result:

ok single line: '186790088216710'