Algoogle

Algorithm for Programming Contest

PKU 2457 Part Acquisition

Category: PKU Tag: bfs

Part Acquisition

問題概要


解法


BFSで各objectまでの最短経路を出す.
そのときに各objectについてどのobjectから来たのかを保存しておく.

コード


(2457.cpp) download
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
#include <cstdio>
#include <vector>
#include <queue>
#include <map>

using namespace std;

typedef pair<int,int> pii;

int N, K;
vector<int> edge[1024];
int dist[1024];
int from[1024];

int main(){
    scanf("%d%d", &N, &K);
    for(int i = 0; i < N; i++){
        int a, b;
        scanf("%d%d", &a, &b);
        edge[a].push_back(b);
    }
    queue<pii> bfs;
    bfs.push(pii(1,0));
    while(!bfs.empty()){
        int pos = bfs.front().first;
        int ppos = bfs.front().second;
        bfs.pop();
        if(dist[pos] > 0) continue;
        dist[pos] = dist[ppos] + 1;
        from[pos] = ppos;
        for(int i = 0; i < edge[pos].size(); i++)
            bfs.push(pii(edge[pos][i], pos));
    }
    if(dist[K] == 0){
        puts("-1");
        return 0;
    }
    vector<int> ans;
    int cur = K;
    while(cur != 0){
        ans.push_back(cur);
        cur = from[cur];
    }
    printf("%d\n", (int)ans.size());
    for(int i = ans.size()-1; i >= 0; i--)
        printf("%d\n", ans[i]);
    return 0;
}

Comments