메뉴 건너뛰기

Programing

BAEKJOON

1005

관리자 2019.12.21 11:41 조회 수 : 8

C++

#include <cstdio>

#include <vector>

#include <queue>

#include <cstring>

using namespace std;

struct Q_info{

    int n;

    int dist;

};

int T,N,K;

int D[1010];

int result[1010];

int cnt[1010];

vector<int> vec[1010];

queue<Q_info> q;

int answer[1010];

int main()

{

    scanf("%d",&T);

    for(int i=0;i<T;i++){

        for(int j=0;j<1010;j++){

            vec[j].clear();

        }

        memset(result,0,sizeof(result));

        memset(cnt,0,sizeof(cnt));

        scanf("%d %d",&N,&K);

        for(int j=1;j<=N;j++){

            scanf("%d",&D[j]);

        }

        for(int j=0;j<K;j++){

                int a,b;

            scanf("%d %d",&a,&b);

            cnt[b]++;

            vec[a].push_back(b);

        }

        for(int i=1;i<=N;i++){

            if(cnt[i]==0){

                result[i]=D[i];

                q.push({i,result[i]});

            }

        }

        while(!q.empty()){

            Q_info info=q.front();

            q.pop();

            for(auto p: vec[info.n]){

                cnt[p]--;

                if(info.dist+D[p]>result[p]){

                    result[p]=info.dist+D[p];

                }

                if(cnt[p]==0)

                    q.push({p,result[p]});

            }

        }

        int w;

        scanf("%d",&w);

        answer[i]=result[w];

    }

    for(int i=0;i<T;i++){

        printf("%d\n",answer[i]);

    }

    return 0;

}

번호 제목 글쓴이 날짜 조회 수
공지 안내사항 관리자 2019.12.21 165
15 14430 관리자 2019.12.21 6
14 1182 관리자 2019.12.21 8
13 11660 관리자 2020.04.11 46
12 1149 관리자 2019.12.21 8
11 10999 관리자 2020.04.11 47
10 10937 관리자 2020.04.11 41
9 10919 관리자 2020.04.11 42
8 10800 관리자 2019.12.21 8
7 10799 관리자 2019.12.21 9
6 10534 관리자 2020.04.11 44
5 10164 관리자 2019.12.21 6
4 10159 관리자 2019.12.21 6
3 10157 관리자 2019.12.21 8
2 10070 관리자 2020.04.11 44
» 1005 관리자 2019.12.21 8
위로