forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3080 - ChiBi.cpp
97 lines (72 loc) · 2.55 KB
/
3080 - ChiBi.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
int n,d[1000][1000],t[1000],C;
vector<int> comp[1000];
int Q[1000],head,tail;
bool visited[1000];
int main(){
while(scanf("%d",&n) == 1){
for(int i = 0;i < n;++i)
for(int j = 0;j < n;++j)
scanf("%d",&d[i][j]);
for(int i = 0;i < n;++i)
scanf("%d",&t[i]);
C = 0;
for(int i = 0;i < n;++i)
comp[i].clear();
memset(visited,false,sizeof(visited));
for(int i = 0;i < n;++i){
if(!visited[i]){
head = tail = 0;
visited[i] = true;
Q[tail] = i;
++tail;
while(head < tail){
int cur = Q[head];
comp[C].push_back(cur);
++head;
for(int j = 0;j < n;++j){
if(d[cur][j] != -1 && !visited[j]){
visited[j] = true;
Q[tail] = j;
++tail;
}
}
}
++C;
}
}
int ans = 0;
for(int c = 0;c < C;++c){
int sz = comp[c].size();
for(int k = 0;k < sz;++k){
int x = comp[c][k];
for(int i = 0;i < sz;++i){
int y = comp[c][i];
if(d[x][y] != -1){
for(int j = 0;j < sz;++j){
int z = comp[c][j];
if(d[x][z] != -1 && (d[y][z] == -1 || d[y][x]+d[x][z] < d[y][z]))
d[y][z] = d[y][x] + d[x][z];
}
}
}
}
int comp_ans = -1;
for(int i = 0;i < sz;++i){
int aux = 0;
for(int j = 0;j < sz;++j)
if(d[comp[c][i]][comp[c][j]] > aux)
aux = d[comp[c][i]][comp[c][j]];
aux += t[comp[c][i]];
if(comp_ans == -1 || aux < comp_ans) comp_ans = aux;
}
ans = max(ans,comp_ans);
}
printf("%d\n",ans);
}
return 0;
}