-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1167.cpp
83 lines (73 loc) · 2.75 KB
/
1167.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
#include<iostream>
#include<utility>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
int visited[100001] = {0, };
//<연결된 노드 번호, 거리>
vector<pair<int, int>>edge[100001];
int node[100001] = {0, };
int answer = 0;
queue<int>q;
void bfs();
int main (){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int v, tmp1, tmp2, tmp3, cnt;
cin >> v;
//input
for (int i = 1; i <= v; i++){
cnt = 0;
cin >> tmp1;
while(1){
cin >> tmp2;
if(tmp2 == -1) break;
cnt++;
cin >> tmp3;
edge[tmp1].push_back({tmp2, tmp3});
}
//제일 끝 노드들 큐에 push
if(cnt == 1) {
//visited[tmp1]=1;
q.push(tmp1);
}
}
bfs();
cout << answer;
return 0;
}
void bfs(){
while(!q.empty()){
visited[q.front()] ++;
//cout << q.front() << ' ';
for(int i = 0; i < edge[q.front()].size(); i++){
//cout << edge[edge[q.front()][i].first].size()<< ' ' << visited[edge[q.front()][i].first] << ' ' <<(int)(edge[edge[q.front()][i].first].size()) - (visited[edge[q.front()][i].first])<< " ";
if((int)edge[edge[q.front()][i].first].size()-visited[edge[q.front()][i].first] >= 1){
answer = max(node[edge[q.front()][i].first]+node[q.front()] + edge[q.front()][i].second, answer);
node[edge[q.front()][i].first] = max(node[edge[q.front()][i].first], node[q.front()] + edge[q.front()][i].second);
visited[edge[q.front()][i].first]++;
if((int)edge[edge[q.front()][i].first].size()-visited[edge[q.front()][i].first]==1) q.push(edge[q.front()][i].first);
}
/*if(edge[edge[q.front()][i].first].size()-visited[edge[q.front()][i].first] == 1){
answer = max(node[edge[q.front()][i].first]+node[q.front()] + edge[q.front()][i].second, answer);
node[edge[q.front()][i].first] = max(node[edge[q.front()][i].first], node[q.front()] + edge[q.front()][i].second);
//visited[edge[q.front()][i].first]++;
q.push(edge[q.front()][i].first);
}
*/}
/*for(int i = 1; i < 6; i++){
cout << node[i] << ' ';
}
cout << ' ';
cout << answer << '\n';*/
q.pop();
}
}
/*if(visited[edge[nodeNum][i].first] < edge[edge[nodeNum][i].first].size()){
visited[edge[nodeNum][i].first]++;
dfs(edge[nodeNum][i].first);
answer = max(node[edge[nodeNum][i].first]+node[nodeNum] + edge[nodeNum][i].second, answer);
node[edge[nodeNum][i].first] = max(node[edge[nodeNum][i].first], node[nodeNum] + edge[nodeNum][i].second);
}*/