UVALive - 6436、HYSBZ - 2435 (dfs)
这两道题都是用简单dfs解的,主要是熟悉回溯过程就能做,据说用bfs也能做
道路修建(HYSBZ - 2435)
在 W 星球上有 n 个国家。为了各自国家的经济发展,他们决定在各个国家
之间建设双向道路使得国家之间连通。但是每个国家的国王都很吝啬,他们只愿意修建恰好n – 1条双向道路。 每条道路的修建都要付出一定的费用, 这个费用等于道路长度乘以道路两端的国家个数之差的绝对值。例如,在下图中,虚线所示道路两端分别有 2 个、4个国家,如果该道路长度为 1,则费用为1×|2 – 4|=2。图中圆圈里的数字表示国家的编号。
由于国家的数量十分庞大,道路的建造方案有很多种,同时每种方案的修建
费用难以用人工计算,国王们决定找人设计一个软件,对于给定的建造方案,计
算出所需要的费用。请你帮助国王们设计一个这样的软件。
Input
输入的第一行包含一个整数n,表示 W 星球上的国家的数量,国家从 1到n
编号。接下来 n – 1行描述道路建设情况,其中第 i 行包含三个整数ai、bi和ci,表
示第i 条双向道路修建在 ai与bi两个国家之间,长度为ci。
Output
输出一个整数,表示修建所有道路所需要的总费用。
Sample Input
6
1 2 1
1 3 1
1 4 2
6 3 1 5 2 1
Sample Output
20
Hint
n = 1,000,000 1≤ai, bi≤n
0 ≤ci≤ 10^6
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <iomanip>
#include <stack>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
typedef long long LL;
#define Fil(Array, len, num) fill(Array, Array + len, num)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const double PI = 3.1415926;
const double E = 2.1718281828;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Edge
{
int to, w, next;
}edge[MAXN << 1];
int head[MAXN << 1], cnt, vis[MAXN];
void Add(int a, int b, int c)
{
edge[++cnt].to = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt;
}
int n, num[MAXN];
LL ans;
void dfs(int s)
{
num[s] = 1;
for(int i = head[s];i != -1;i = edge[i].next)
{
if(!vis[edge[i].to])
{
vis[edge[i].to] = 1;
dfs(edge[i].to);
num[s] += num[edge[i].to];
ans += (LL)abs(n - 2 * num[edge[i].to]) * edge[i].w;//这里要注意是num[edge[i].to]
}
}
}
int main()
{
scanf("%d", &n);
Fil(head, MAXN, -1);
int a, b, c;
for(int i = 1;i < n;++i)
{
scanf("%d%d%d", &a, &b, &c);
Add(a, b, c);
Add(b, a, c);
}
vis[1] = 1;
dfs(1);
cout << ans << endl;
return 0;
}
UVALive - 6436
题目里面的图拉不下来,就自己看题吧
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <queue>
#include <iomanip>
#include <stack>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
typedef long long LL;
#define Fil(Array, len, num) fill(Array, Array + len, num)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
const double PI = 3.1415926;
const double E = 2.1718281828;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
vector<int> mp[MAXN];
int num[MAXN], n;
LL ans;
void dfs(int s, int fa)
{
num[s] = 1;
int len = mp[s].size();
LL t = 0;
for(int i = 0;i < len;++i)
{
if(mp[s][i] == fa)
continue;
dfs(mp[s][i], s);
num[s] += num[mp[s][i]];
// 这里是算该点之后的所有点,然后算忙碌度
t += (LL)((n - num[mp[s][i]] - 1) * num[mp[s][i]]);
// cout << s << " " << mp[s][i] << " " << t << " " << num[mp[s][i]] << endl;
}
// 这里还要算这个点前面的有几个点,加上忙碌度,算出来每两个点都算了两次
t += (LL)(n - num[s]) * (num[s] - 1);
ans = max(ans, t);
}
int main()
{
int T;
scanf("%d", &T);
for(int cas = 1;cas <= T;++cas)
{
for(int i = 0;i < MAXN;++i)
mp[i].clear();
ans = 0;
scanf("%d", &n);
for(int i = 1;i < n;++i)
{
int a, b;
scanf("%d%d", &a, &b);
mp[a].push_back(b);
mp[b].push_back(a);
}
dfs(1, -1);
printf("Case #%d: %lld\n", cas, ans / 2);
}
return 0;
}
UVALive - 6436、HYSBZ - 2435 (dfs)的更多相关文章
- UVALive - 6436(DFS)
题目链接:https://vjudge.net/contest/241341#problem/C 题目大意:给你从1到n总共n个数字,同时给你n-1个连接,同时保证任意两个点之间都可以连接.现在假设任 ...
- UVALive - 6436 —(DFS+思维)
题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...
- ALGO-125_蓝桥杯_算法训练_王、后传说(DFS)
问题描述 地球人都知道,在国际象棋中,后如同太阳,光芒四射,威风八面,它能控制横.坚.斜线位置. 看过清宫戏的中国人都知道,后宫乃步步惊心的险恶之地.各皇后都有自己的势力范围,但也总能找到相安无事的办 ...
- 广度优先遍历-BFS、深度优先遍历-DFS
广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...
- bzoj 2435 dfs处理
Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...
- 齿轮 HYSBZ - 4602 (DFS实现)
齿轮 HYSBZ - 4602 题意:很好理解就不啰嗦了. 致谢:感谢队友小明. 题解:嗯,一开始想到的是并查集,后来,就先看了另一道题,xj写dfs和暴力,就卡死了.于是来补这题了,前向星建图 题解 ...
- JS002. map( ) 和 filter( ) 的区别和实际应用场景(递归函数、深度优先搜索DFS)
在开发过程中难免会碰到省市区级联的操作,一般后端人员是不愿意将中文储存在数据库的. 由于应用页面较多,我们在通过区域Code写查字典函数时应该注意函数的 时间复杂度 / 空间复杂度. 如果用三层for ...
- UVALive 6884 GREAT + SWERC = PORTO dfs模拟
题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- UVALive 7334 Kernel Knights (dfs)
Kernel Knights 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/K Description Jousting is ...
随机推荐
- [SoapUI] 通过SoapUI发送POST请求,请求的body是JSON格式的数据
通过SoapUI发送POST请求,请求的body是JSON格式的数据: data={"currentDate":"2015-06-19","reset ...
- JS对象转URL参数(原生JS和jQuery两种方式)
转自:点击打开链接 现在的js框架将ajax请求封装得非常简单,例如下面: $.ajax({ type: "POST", url: "some.php", da ...
- 1256 Anagram
题目链接: http://poj.org/problem?id=1256 题意: 根据自定义的字典序: 'A'<'a'<'B'<'b'<...<'Z'<'z' 和输 ...
- Perl 学习笔记-标量数据
最近学习Perl, 准备看一遍入门指南,关键的东西还是记录下来,以便以后复习和查看参考. 笔记来自<<Perl语言入门第5版>> 1. 在Perl内部,不区分整数值和浮点数值, ...
- mysql问题,出现 Cant connect to mysql server on 'localhost'
莫名其妙的一个问题,这个问题出现在今天,然后查找下,发现需要重启服务器,但是重启也一样,于是关机重启,还是这个现象 ,然后看到 错误提示, 提示my.ini的第21行,产生错误,于是按照路径找到配置文 ...
- A Multi-Sensorial Simultaneous Localization and Mapping (SLAM) System for Low-Cost Micro Aerial Vehicles in GPS-Denied Environments
A Multi-Sensorial Simultaneous Localization and Mapping (SLAM) System for Low-Cost Micro Aerial Vehi ...
- JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序
大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...
- delphi Table切换控件顺序问题
delphi Table切换控件顺序问题 Tagorder的值就是确定Table键切换顺序的 以上做法只能解决同一类型的多个控件(如Edit1,edit2....)显示顺序问题 假如有不同类型的控件如 ...
- TCP中的seq
TCP连接中传送的字节流中的每个字节都按顺序编号,第一个字节的编号由本地随机产生 seq其实就是这个报文段中的第一个字节的数据编号. 例如,一段报文的序号字段值是 200 ,而携带的数据共有100字段 ...
- WebApi与Mvc的区别
ASP.NET Mvc是基于ASP.NET管道设计的框架,但是Mvc应用程序只能在iis中运行,而WebApi框架既可以寄宿在IIs上也可以在Self-Host上进行处理. Mvc和webapi都有相 ...