답안 #9645

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
9645 2014-09-28T07:48:31 Z ksmail12 Your life (kriii2_Y) C++
4 / 4
120 ms 8040 KB
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>

using namespace std;


vector<vector<int> > g;
int find(int n){
    vector<int> dist(n+1, 0x7fffffff);
    dist[1] = 0;
    priority_queue<pair<int,int> >pq;
    pq.push(make_pair(0, 1));

    while(!pq.empty() ) {
        int cost = -pq.top().first;
        int here = pq.top().second;
        pq.pop();

        if(cost > dist[here]) {
            continue;
        }

        for(int i=0;i<g[here].size();i++) {
            int there = g[here][i];
            int ndist = cost+1;

            if(dist[there] > ndist) {
                dist[there] = ndist;
                pq.push(make_pair(-ndist, there));
            }
        }
    }

    return dist[n];
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    if(n==1) {
        printf("0\n");
        return 0;
    }
    g.resize(n+1);
    // make graph
    for (int i=m;i!=0;i--) {
        int x,y;
        scanf("%d %d", &x, &y);
        g[x].push_back(y);
    }
    // find solution
    int result = find(n);
    if(result == 0x7fffffff){
        printf("-1\n");
    }
    else {
        printf("%d\n",result);
    }
    
    return 0;   
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1680 KB Output is correct
2 Correct 0 ms 1680 KB Output is correct
3 Correct 0 ms 1680 KB Output is correct
4 Correct 0 ms 1680 KB Output is correct
5 Correct 0 ms 1680 KB Output is correct
6 Correct 0 ms 1680 KB Output is correct
7 Correct 0 ms 1680 KB Output is correct
8 Correct 0 ms 1680 KB Output is correct
9 Correct 0 ms 1680 KB Output is correct
10 Correct 20 ms 2208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 4412 KB Output is correct
2 Correct 40 ms 7448 KB Output is correct
3 Correct 64 ms 7448 KB Output is correct
4 Correct 48 ms 7448 KB Output is correct
5 Correct 52 ms 7448 KB Output is correct
6 Correct 48 ms 7448 KB Output is correct
7 Correct 44 ms 7448 KB Output is correct
8 Correct 120 ms 8040 KB Output is correct
9 Correct 56 ms 7448 KB Output is correct
10 Correct 72 ms 7184 KB Output is correct