Skip to content

Commit cdac032

Browse files
committed
tccc 2003 semi 4 275
1 parent 13800b6 commit cdac032

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

Diff for: Circuits.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
using namespace std;
11+
typedef pair<int,int> pi;
12+
typedef vector<int> vi;
13+
typedef vector<vi> vvi;
14+
typedef vector<string> vs;
15+
typedef vector<vs> vvs;
16+
int M[55][55];
17+
int v[55];
18+
int n;
19+
20+
class Circuits {
21+
public:
22+
int dfs(int s, int c = 0){
23+
v[s] = 1;
24+
int rc = 0;
25+
for(int i = 0; i < n; i++){
26+
if((v[i] == 0 || v[i] == 2) && M[s][i]){
27+
v[i] = 1;
28+
rc = max(rc, dfs(i)+M[s][i]);
29+
}
30+
}
31+
v[s] = 2;
32+
return rc+c;
33+
}
34+
35+
int howLong(vector <string> connects, vector <string> costs)
36+
{
37+
n = costs.size();
38+
for(int i = 0; i < n; i++)
39+
for(int j = 0; j < n; j++)
40+
M[i][j] = 0;
41+
42+
for(int i = 0; i < n; i++){
43+
string s = connects[i], t = costs[i];
44+
int j, c;
45+
stringstream ss(s), st(t);
46+
while(ss>>j){
47+
st>>c;
48+
M[i][j] = c;
49+
}
50+
}
51+
fill(v, v+55, 0);
52+
int c = 0;
53+
for(int i = 0; i < n; i++)
54+
if(v[i] == 0) c = max(c, dfs(i));
55+
return c;
56+
}
57+
58+
// BEGIN CUT HERE
59+
public:
60+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
61+
private:
62+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
63+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
64+
void test_case_0() { string Arr0[] = {"1 2",
65+
"2",
66+
""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"5 3",
67+
"7",
68+
""}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 12; verify_case(0, Arg2, howLong(Arg0, Arg1)); }
69+
void test_case_1() { string Arr0[] = {"1 2 3 4 5","2 3 4 5","3 4 5","4 5","5",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"2 2 2 2 2","2 2 2 2","2 2 2","2 2","2",""}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 10; verify_case(1, Arg2, howLong(Arg0, Arg1)); }
70+
void test_case_2() { string Arr0[] = {"1","2","3","","5","6","7",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"2","2","2","","3","3","3",""}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 9; verify_case(2, Arg2, howLong(Arg0, Arg1)); }
71+
void test_case_3() { string Arr0[] = {"","2 3 5","4 5","5 6","7","7 8","8 9","10",
72+
"10 11 12","11","12","12",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"","3 2 9","2 4","6 9","3","1 2","1 2","5",
73+
"5 6 9","2","5","3",""}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 22; verify_case(3, Arg2, howLong(Arg0, Arg1)); }
74+
void test_case_4() { string Arr0[] = {"","2 3","3 4 5","4 6","5 6","7","5 7",""}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); string Arr1[] = {"","30 50","19 6 40","12 10","35 23","8","11 20",""}; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); int Arg2 = 105; verify_case(4, Arg2, howLong(Arg0, Arg1)); }
75+
76+
// END CUT HERE
77+
78+
};
79+
80+
// BEGIN CUT HERE
81+
int main(){
82+
83+
Circuits ___test;
84+
___test.run_test(-1);
85+
86+
}
87+
// END CUT HERE

Diff for: Circuits.txt

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
PROBLEM STATEMENT
2+
An essential part of circuit design and general system optimization is critical path analysis. On a chip, the critical path represents the longest path any signal would have to travel during execution. In this problem we will be analyzing chip designs to determine their critical path length. The chips in this problem will not contain any cycles, i.e. there exists no path from one component of a chip back to itself.
3+
4+
Given a vector <string> connects representing the wiring scheme, and a vector <string> costs representing the cost of each connection, your method will return the size of the most costly path between any 2 components on the chip. In other words, you are to find the longest path in a directed, acyclic graph. Element j of connects will list the components of the chip that can be reached directly from the jth component (0-based). Element j of costs will list the costs of each connection mentioned in the jth element of connects. As mentioned above, the chip will not contain any cyclic paths. For example:
5+
connects = {"1 2",
6+
"2",
7+
""}
8+
costs = {"5 3",
9+
"7",
10+
""}
11+
12+
In this example, component 0 connects to components 1 and 2 with costs 5 and 3 respectively. Component 1 connects to component 2 with a cost of 7. All connections mentioned are directed. This means a connection from component i to component j does not imply a connection from component j to component i. Since we are looking for the longest path between any 2 components, your method would return 12.
13+
14+
15+
16+
DEFINITION
17+
Class:Circuits
18+
Method:howLong
19+
Parameters:vector <string>, vector <string>
20+
Returns:int
21+
Method signature:int howLong(vector <string> connects, vector <string> costs)
22+
23+
24+
CONSTRAINTS
25+
-connects must contain between 2 and 50 elements inclusive
26+
-connects must contain the same number of elements as costs
27+
-Each element of connects must contain between 0 and 50 characters inclusive
28+
-Each element of costs must contain between 0 and 50 characters inclusive
29+
-Element i of connects must contain the same number of integers as element i of costs
30+
-Each integer in each element of connects must be between 0 and the size of connects-1 inclusive
31+
-Each integer in each element of costs must be between 1 and 1000 inclusive
32+
-Each element of connects may not contain repeated integers
33+
-Each element of connects must be a single space delimited list of integers, each of which has no extra leading zeros. There will be no leading or trailing whitespace.
34+
-Each element of costs must be a single space delimited list of integers, each of which has no extra leading zeros. There will be no leading or trailing whitespace.
35+
-The circuit may not contain any cycles
36+
-There will be at least 1 connection.
37+
38+
39+
EXAMPLES
40+
41+
0)
42+
{"1 2",
43+
"2",
44+
""}
45+
{"5 3",
46+
"7",
47+
""}
48+
49+
Returns: 12
50+
51+
From above
52+
53+
1)
54+
{"1 2 3 4 5","2 3 4 5","3 4 5","4 5","5",""}
55+
{"2 2 2 2 2","2 2 2 2","2 2 2","2 2","2",""}
56+
57+
Returns: 10
58+
59+
The longest path goes from 0-1-2-3-4-5 for a cost of 10.
60+
61+
2)
62+
{"1","2","3","","5","6","7",""}
63+
{"2","2","2","","3","3","3",""}
64+
65+
Returns: 9
66+
67+
The 0-1-2-3 path costs 6 whereas the 4-5-6-7 path costs 9
68+
69+
3)
70+
{"","2 3 5","4 5","5 6","7","7 8","8 9","10",
71+
"10 11 12","11","12","12",""}
72+
{"","3 2 9","2 4","6 9","3","1 2","1 2","5",
73+
"5 6 9","2","5","3",""}
74+
75+
Returns: 22
76+
77+
4)
78+
{"","2 3","3 4 5","4 6","5 6","7","5 7",""}
79+
{"","30 50","19 6 40","12 10","35 23","8","11 20",""}
80+
81+
Returns: 105

0 commit comments

Comments
 (0)