dfs 遍历 codeforces 24A
2014-11-07 20:23
302 查看
//题目中明确说了,这是一个环,要充分利用性质
//答案一定是在从1出发的两条路径中的一条,如果当前边反向,则更新当前值,最后答案就是两条路径中小的
//dfs时,记录父亲节点,这样就可以避免在向下一步走的时候,又回到了父亲
//答案一定是在从1出发的两条路径中的一条,如果当前边反向,则更新当前值,最后答案就是两条路径中小的
//dfs时,记录父亲节点,这样就可以避免在向下一步走的时候,又回到了父亲
/************************************************************************* > File Name: 24A.cpp > Author: flyasdfvcxz > Mail: 1099431883@qq.com > Created Time: Fri 07 Nov 2014 08:10:08 PM CST ************************************************************************/ #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cstdlib> #include <vector> #include <queue> #include <map> #include <ctime> #include <cmath> #define ll long long using namespace std; const int N = 110; int n, cur, ans; int g ; void dfs(int x, int sum, int fa) { //cout << " x = " << x << " sum = " << sum << endl; if (x == 1) { ++cur; if (cur > 1) { ans = min(ans, sum); return; } } for (int i = 1; i <= n; ++i) { if (i == fa) continue; if (g[x][i]) { dfs(i, sum, x); } else if (g[i][x]) { dfs(i, sum + g[i][x], x); } } } int main() { //freopen("in.txt", "r", stdin); while (cin >> n) { memset(g, 0, sizeof(g)); for (int i = 1; i <= n; ++i) { int a, b, c; cin >> a >> b >> c; g[a][b] = c; } ans = 1e9; cur = 0; dfs(1, 0, -1); cout << ans << endl; } return 0; }
相关文章推荐
- codeforces 24A Ring road (dfs)
- Codeforces 29D Ant on the Tree 树的遍历 dfs序
- Codeforces 29D Ant on the Tree 树的遍历 dfs序
- 【codeforces 680 D】【数学+贪心+DFS】【求一个不超过 m 的最大体积 X, 每次选一个最大的 x 使得 x3 不超过当前剩余体积。问在能选最多个数的情况下,X 最大是多少】
- 二叉树的建立与DFS遍历
- Codeforces 405E DFS
- 1094. The Largest Generation (25)[dfs|bfs遍历]
- 图的遍历程序模板--DFS
- 数据结构-图-邻接表深度和广度遍历DFS_BFS
- DFS遍历
- leetcode 79. Word Search DFS 单词搜索 + 深度优先遍历
- leetcode 110. Balanced Binary Tree(平衡二叉树的判断) DFS深度优先遍历
- codeforces_686C. Robbers' watch(dfs)
- codeforces 550B Preparing Olympiad(DFS+回溯)
- 数据结构之图(邻接表存储,DFS和BFS遍历)
- codeforces 711D Directed Roads dfs
- CodeForces 598 D. Igor In the Museum(dfs)
- UESTC 2016 Summer Training #1 Div.2 J - Objects Panel (A) dfs、按条件遍历树、interesting
- CodeForces 734 E.Anton and Tree(dfs)
- CodeForces 570D(dfs序 or 离线dfs)