|
2 | 2 | #include <string> |
3 | 3 | #include <numeric> |
4 | 4 | #include <queue> |
| 5 | +#define inf 10000000 |
5 | 6 | using namespace std; |
6 | | -vector<string> M; |
7 | | -int visited[55], n; |
| 7 | +int D[55][55]; |
8 | 8 |
|
9 | | -class Egalitarianism { |
10 | | - |
11 | | - int bfs(int s){ |
12 | 9 |
|
13 | | - queue<pair<int, int> > Q; |
14 | | - Q.push(make_pair(s, 0)); |
15 | | - int maxl = 0; |
16 | | - |
17 | | - while(!Q.empty()){ |
18 | | - pair<int, int> p = Q.front(); |
19 | | - Q.pop(); |
20 | | - int i = p.first, l = p.second; |
21 | | - maxl = max(l, maxl); |
22 | | - for(int j = 0; j < n; j++) |
23 | | - if(!visited[j] && M[i][j] == 'Y'){ |
24 | | - visited[j] = 1; |
25 | | - Q.push(make_pair(j, l+1)); |
26 | | - } |
27 | | - } |
28 | | - return maxl; |
29 | | - } |
| 10 | +class Egalitarianism { |
30 | 11 |
|
31 | 12 | public: |
32 | | - int maxDifference(vector<string> const &isFriend, |
| 13 | + int maxDifference(vector<string> const M, |
33 | 14 | int d) { |
34 | | - M = isFriend; |
35 | | - n = M.size(); |
| 15 | + int n = M.size(); |
| 16 | + // memset(D, 0, sizeof(D)); |
| 17 | + for(int i = 0; i < n; i++) |
| 18 | + for(int j = 0; j < n; j++) |
| 19 | + if(i == j) D[i][j] = 0; |
| 20 | + else D[i][j] = (M[i][j] == 'Y' ? 1 : inf); |
| 21 | + |
| 22 | + for(int k = 0; k < n; k++) |
| 23 | + for(int i = 0; i < n; i++) |
| 24 | + for(int j = 0; j < n; j++) |
| 25 | + D[i][j] = min(D[i][j], D[i][k] + D[k][j]); |
| 26 | + |
36 | 27 | int maxv = 0; |
37 | | - for(int i = 0; i < n; i++){ |
38 | | - fill(visited, visited+50, 0); |
39 | | - visited[i] = 1; |
40 | | - maxv = max(maxv, bfs(i)); |
41 | | - int count = accumulate(visited, visited+n, 0); |
42 | | - if(count < n) return -1; |
43 | | - } |
44 | | - if(maxv == 0) return -1; |
| 28 | + for(int i = 0; i < n; i++) |
| 29 | + for(int j = 0; j < n; j++) |
| 30 | + maxv = max(maxv, D[i][j]); |
| 31 | + |
| 32 | + cout<<"maxv = "<<maxv<<endl; |
| 33 | + if(maxv == inf) return -1; |
45 | 34 | return maxv*d; |
46 | 35 | } |
47 | 36 | }; |
0 commit comments