这两道题都是用简单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)的更多相关文章

  1. UVALive - 6436(DFS)

    题目链接:https://vjudge.net/contest/241341#problem/C 题目大意:给你从1到n总共n个数字,同时给你n-1个连接,同时保证任意两个点之间都可以连接.现在假设任 ...

  2. UVALive - 6436 —(DFS+思维)

    题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...

  3. ALGO-125_蓝桥杯_算法训练_王、后传说(DFS)

    问题描述 地球人都知道,在国际象棋中,后如同太阳,光芒四射,威风八面,它能控制横.坚.斜线位置. 看过清宫戏的中国人都知道,后宫乃步步惊心的险恶之地.各皇后都有自己的势力范围,但也总能找到相安无事的办 ...

  4. 广度优先遍历-BFS、深度优先遍历-DFS

    广度优先遍历-BFS 广度优先遍历类似与二叉树的层序遍历算法,它的基本思想是:首先访问起始顶点v,接着由v出发,依次访问v的各个未访问的顶点w1 w2 w3....wn,然后再依次访问w1 w2 w3 ...

  5. bzoj 2435 dfs处理

    Description 在 W 星球上有 n 个国家.为了各自国家的经济发展,他们决定在各个国家 之间建设双向道路使得国家之间连通.但是每个国家的国王都很吝啬,他们只愿 意修建恰好 n – 1条双向道 ...

  6. 齿轮 HYSBZ - 4602 (DFS实现)

    齿轮 HYSBZ - 4602 题意:很好理解就不啰嗦了. 致谢:感谢队友小明. 题解:嗯,一开始想到的是并查集,后来,就先看了另一道题,xj写dfs和暴力,就卡死了.于是来补这题了,前向星建图 题解 ...

  7. JS002. map( ) 和 filter( ) 的区别和实际应用场景(递归函数、深度优先搜索DFS)

    在开发过程中难免会碰到省市区级联的操作,一般后端人员是不愿意将中文储存在数据库的. 由于应用页面较多,我们在通过区域Code写查字典函数时应该注意函数的 时间复杂度 / 空间复杂度. 如果用三层for ...

  8. UVALive 6884 GREAT + SWERC = PORTO dfs模拟

    题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  9. UVALive 7334 Kernel Knights (dfs)

    Kernel Knights 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/K Description Jousting is ...

随机推荐

  1. Ubuntu的SWAP设置

    1. 在Ubuntu中配置使用新创建的Swap分区 Command list: 查找Swap分区的UUID sudo blkid 在/ect/fstab中加入新的Swap分区 sudo gedit / ...

  2. mybatis思维导图(二)

    写在前面 上一篇文章写了mybatis的基本原理和配置文件的基本使用,这一篇写mybatis的使用,主要包括与sping集成.动态sql.还有mapper的xml文件一下复杂配置等.值得注意的是,导图 ...

  3. smarty内置函数、自定义函数

    1.把字符串里的d字母替换成h格式:{'d'|str_replace:'h':$str}; d要查找的字符 h要替换的字符 $str字符串 2.function test($param){$p1=$p ...

  4. Lucene.Net(转)

    出处:http://www.cnblogs.com/piziyimao/archive/2013/01/31/2887072.html 做过站内搜索的朋友应该对Lucene.Net不陌生,没做过的也许 ...

  5. HttpAnalyzerStdV7使用教程

    相关链接:HttpAnalyzerStdV7安装教程 1.1.使用HttpAnalyzerStdV7监控服务器返回消息 1.运行前关闭要监控的浏览器. 2.运行软件: 3.点击"Start& ...

  6. ubuntu eclipse 集成pyDev

    Eclipse help 选择安装新软件 添加一个pydev 名字随意.地址是 http://pydev.org/updates. 下面的列表会出现很多PyDev For Eclipse 选择版本最高 ...

  7. Unity3D 之PC客户端的分辨率自定义

    在Player Setting中可以自定义分辨率,但在PC版本中如果使用Display Resolution Dialog选项,会发现在分辨率选项中只有预定义的那些,而并没有在Player Setti ...

  8. TDE--相关Demo

    SQL Server 2008引入透明数据加密(Transparent Data Encryption),它允许你完全无需修改应用程序代码而对整个数据库加密.当一个用户数据库可用且已启用TDE时,在写 ...

  9. 菜鸟的Xamarin.Forms前行之路——共享组件

    出自:博客园-半路独行 本文出自于http://www.cnblogs.com/banluduxing 转载请注明出处. Url Description Xamarin.Social The Xama ...

  10. Python在Android系统上运行

    下载 Scripting Layer for Android (SL4A) https://github.com/damonkohler/sl4a https://www.tutorialspoint ...