-
Notifications
You must be signed in to change notification settings - Fork 24
/
Circuits.cpp
66 lines (60 loc) · 1.7 KB
/
Circuits.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<iostream>
#include<cstdio>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<string>
#include<list>
#include<deque>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<utility>
#include<sstream>
#include<cstring>
using namespace std;
const int MAX_N = 55;
class Circuits {
public:
int map[MAX_N][MAX_N];
bool visited[MAX_N];
int memo[MAX_N];
int dfs(int v, int n) {
visited[v] = true;
for (int i = 0; i < n; ++i) {
if (map[v][i]) {
if (!visited[i]) {
memo[v] = map[v][i] + dfs(i, n);
}
}
}
return memo[v];
}
int howLong(vector <string> connects, vector <string> costs) {
int result = 0;
memset(map, 0, sizeof(int) * MAX_N * MAX_N);
memset(visited, 0, sizeof(int) * MAX_N);
memset(memo, 0, sizeof(int) * MAX_N);
for (int i = 0; i < connects.size(); ++i) {
string s;
stringstream vstream(connects[i]);
stringstream cstream(costs[i]);
while (getline(vstream, s, ' ')) {
int j = atoi(s.c_str());
getline(cstream, s, ' ');
int cost = atoi(s.c_str());
map[i][j] = cost;
}
}
for (int i = 0; i < connects.size(); ++i) {
if (!visited[i]) {
int tmp = dfs(i, connects.size());
result = max(result, tmp);
}
}
return result;
}
};