Skip to content

Commit 4ea7f78

Browse files
committed
srm 584 div 1 250 sol
1 parent 520b347 commit 4ea7f78

File tree

1 file changed

+22
-33
lines changed

1 file changed

+22
-33
lines changed

Diff for: Egalitarianism.cpp

+22-33
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,35 @@
22
#include <string>
33
#include <numeric>
44
#include <queue>
5+
#define inf 10000000
56
using namespace std;
6-
vector<string> M;
7-
int visited[55], n;
7+
int D[55][55];
88

9-
class Egalitarianism {
10-
11-
int bfs(int s){
129

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 {
3011

3112
public:
32-
int maxDifference(vector<string> const &isFriend,
13+
int maxDifference(vector<string> const M,
3314
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+
3627
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;
4534
return maxv*d;
4635
}
4736
};

0 commit comments

Comments
 (0)