Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c02253a

Browse files
Merge pull request #584 from kchung1995/weekly
환승
2 parents cb6d3e8 + 6a320ab commit c02253a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
using namespace std;
5+
#define INF 1e9
6+
7+
int n, k, m;
8+
int visited[101001];
9+
vector<vector <int > > graph(101001);
10+
11+
int main() {
12+
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
13+
cin >> n >> k >> m;
14+
15+
for (int i = 1; i <= m; i++) {
16+
for (int j = 0; j < k; j++) {
17+
int temp;
18+
cin >> temp;
19+
graph[temp].push_back(i + 100000);
20+
graph[i + 100000].push_back(temp);
21+
}
22+
}
23+
24+
for (int i = 2; i < 101001; i++) {
25+
visited[i] = INF;
26+
}
27+
visited[1] = 0;
28+
29+
queue<int> q;
30+
q.push(1);
31+
32+
while (!q.empty()) {
33+
int c = q.front();
34+
q.pop();
35+
36+
for (int i = 0; i < graph[c].size(); i++) {
37+
if (visited[graph[c][i]] > visited[c] + 1) {
38+
visited[graph[c][i]] = visited[c] + 1;
39+
q.push(graph[c][i]);
40+
}
41+
}
42+
}
43+
44+
cout << (visited[n] / 2) + 1;
45+
return 0;
46+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /