西南交通大学第十三届ACM决赛 E.Shortest Path【思维+Dfs】
来源:互联网 发布:锐捷冒充mac地址上网 编辑:程序博客网 时间:2024/04/27 14:01
题目描述
Today HH becomes a designer, and he faces a problem so he asks you for help.
Treeisland is a country with n cities and n−1 two-way road and from any city you can go to any other cities.
HH the designer is going to design a plan to divide n city into n/2 pairs so that the sum of the length between the n/2 pairs city is minimum.
Now HH has finished it but he doesn't know whether it's true so he ask you to calculate it together.
It's guaranteed that n is even.
输入描述:
The first line contains an positive integer T(1≤T≤100), represents there are T test cases.For each test case: The first line contains an positive integer n(1≤n≤104), represents the number of cities in Treeisland, it's guarantee that n is even.Then n−1 lines followed. Each line contains three positive integer u, v and len, (u≠v,1≤u≤n,1≤v≤n,1≤len≤109)indicating there is a road of length len between u and v.It's guarantee you can get to any city from any city.
输出描述:
For each test case, output in one line an integer, represent the minimum sum of length.
示例1
输入
241 2 52 3 83 4 661 3 53 2 34 5 44 3 94 6 10
输出
1131
说明
In the first example, you can divide them into (1,2), and (3,4), then the minimum sum of length is 5+6=11In the second example, you can divide them into (1,3),(2,4),(5,6), hen the minimum sum of length is 5+(3+9)+(10+4)=31
给出N个点,让我们将其分成n/2对,每对点的贡献值为两点距离,求最小距离和。
思路:
①我们可以YY一下,对于很多种分配方案,我们可以很容易找到一种情况,就是一条边要么对答案有贡献,要么就是没贡献(也就是求两点距离的时候不会走过这样一条边)。
②题目又是在一棵树上做文章,那么我们肯定要想O(n)的复杂度去Dfs怎么做一做能够得到解。
对于一个点u来讲,如果以这个点u作为根的子树的点的个数为奇数的话,我们知道其子树中肯定有一个点,要和外边一个点相配对才行,那么对应从父亲到当前节点u这条边就一定会有贡献。
③那么过程维护一下,对应到一个点的时候,去判定子树的size是否为奇数即可。
Ac代码:
#include<stdio.h>#include<string.h>using namespace std;#define ll long long intstruct node{ int from; int to; int w; int next;}e[350000];int cont;int head[150000];int size[150000];ll output;void add(int from,int to,int w){ e[cont].to=to; e[cont].w=w; e[cont].next=head[from]; head[from]=cont++;}void Dfs(int u,int from,int prew){ size[u]=1; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; int w=e[i].w; if(v==from)continue; Dfs(v,u,w); size[u]+=size[v]; } if(size[u]%2==1)output+=prew;}int main(){ int n; int t;scanf("%d",&t); while(t--) { output=0,cont=0; scanf("%d",&n); memset(head,-1,sizeof(head)); for(int i=1;i<=n-1;i++) { int x,y,w;scanf("%d%d%d",&x,&y,&w); add(x,y,w);add(y,x,w); } Dfs(1,-1,0); printf("%lld\n",output); }}
阅读全文
0 0
- 西南交通大学第十三届ACM决赛 E.Shortest Path【思维+Dfs】
- 西南交通大学第十三届ACM决赛-重现赛-E(DFS)
- 西南交通大学第十三届ACM决赛 F 题 【思维 + 前缀】
- 西南交通大学第十三届ACM决赛【7/11】
- 西南交通大学第十三届ACM决赛-重现赛
- 西南交通大学第十三届ACM决赛 D.Music Problem【Bitset+背包/思维+背包】
- 西南交通大学第十三届ACM决赛 F.Maximize The Beautiful Value【思维】
- 【西南交通大学第十三届ACM决赛-重现赛】 A B E F H K
- 西南交通大学第十三届ACM决赛 D.Music Problem 背包DP
- 西南交通大学第十三届ACM决赛【solved:9 / 11】
- 西南交通大学第十三届ACM决赛-重现赛-H(BFS+DP)
- 西南交通大学第十三届ACM决赛-重现赛-C(组合数+容斥+分块打表)
- 西南交通大学第十三届校赛 HMagic Maze DFS
- SWJTU OJ 西安交通大学第十三届ACM程序设计决赛-题解
- hdoj--5636--Shortest Path(dfs)
- hdu 5636 Shortest Path【floyd+思维】
- CodeForces 59E Shortest Path 用边跑最短路
- HDOJ 5636 Shortest Path(dfs求最短路径)
- CentOS 7 下不同主机间 ssh 免密码登录
- 朴素贝叶斯分类器简介及C++实现(性别分类)
- c#程序如何获取管理员权限
- svn_client API 参考
- Canvas学习之绘制时钟
- 西南交通大学第十三届ACM决赛 E.Shortest Path【思维+Dfs】
- 反向传导算法
- LNK1104:无法打开文件kernel32.lib的解决方法解决方法
- android保存固定ip
- IE眼中的rem
- C#学习笔记——读写ini文件
- 浅谈大数据时代web数据可视化探析
- gym 101512 BAPC 2014 B Button Bashing
- linux服务器安全优化