SOJ 1024. Magic Island

题目大意:给定一个n个点n-1条的连通无向图,求从任意一点出发,在不重复经过同一点的情况下,所能走过的的边的最大权值。

解题思路:深度优先搜索。

 1 #include <iostream>
 2 #include <vector>
 3 using namespace std;
 4
 5 struct Edge {
 6     int to;
 7     int dis;
 8     Edge(int t = 0, int d = 0) {
 9         to = t;
10         dis = d;
11     }
12 };
13
14 const int maxn = 10005;
15
16 vector<Edge> cities[maxn];
17 bool visited[maxn];
18 int maxDis;
19 int dis;
20
21 void init(int n) {
22     for (int i = 1; i <= n; i++) {
23         visited[i] = false;
24         cities[i].clear();
25     }
26 }
27
28 void dfs(int from) {
29     for (int i = 0; i < cities[from].size(); i++) {
30         Edge & e = cities[from][i];
31         if (!visited[e.to]) {
32             visited[e.to] = true;
33             dis += e.dis;
34             maxDis = maxDis > dis ? maxDis : dis;
35             dfs(e.to);
36             visited[e.to] = false;
37             dis -= e.dis;
38         }
39     }
40 }
41
42 int main() {
43     int n, m;
44     int from, to, dis;
45     while (cin >> n >> m) {
46         init(n);
47
48         for (int i = 1; i < n; i++) {
49             cin >> from >> to >> dis;
50             cities[from].push_back(Edge(to, dis));
51             cities[to].push_back(Edge(from, dis));
52         }
53
54         dis = 0;
55         maxDis = 0;
56         visited[m] = true;
57         dfs(m);
58
59         cout << maxDis << endl;
60     }
61
62     return 0;
63 }
时间: 2024-08-25 00:49:31

SOJ 1024. Magic Island的相关文章

sicily 1024 Magic Island

深搜中与记录长度有关的题目,都差不多是这样的! 还有邻接表的写法,不够熟! 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 int ans; 6 7 bool vis[10005]; 8 9 struct edge { 10 int u; 11 int v; 12 int l; 13 }; 14 15 vector <edge> graph[10005]; 16 17 void dfs(int n, int x, in

sicily1024 Magic Island(图的遍历)

Description There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know

编程题目分类(剪辑)

1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. 模拟 12. 算术与代数 13. 组合问题 14. 数论 15. 网格,几何,计算几何 [编程入门] PC 110101, uva 100, The 3n+1 problem, 难度 1 PC 110102, uva 10189, Minesweeper, 难度 1 PC 110103, uva 10137, The T

(转)sicily题目分类

Sicily题目分类 ·         [数据结构/图论] 1310 Right-Heavy Tree   笛卡尔树相关,复杂度O(N)或O(NlogN). ·1426 Phone List         电话号码前缀检索,trie树相关. ·1443 Printer Queue      基本队列操作. ·1149 等价表达式         判断表达式是否等价(递归求解) ·1136 山海经             n长序列里求m次区间询问的最大连续子区间和.线段树/RMQ ·1252

Problem A CodeForces 560A

Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of mone

货币问题

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect an

货币体系

Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of mone

Codeforces Round #313 A. Currency System in Geraldion

Description A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of mone

【打CF,学算法——一星级】Codeforces Round #313 (Div. 2) A. Currency System in Geraldion

[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/A 题面: A. Currency System in Geraldion time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A magic island Geraldion, where Gerald lives,