본문 바로가기

Algorithm/BAEKJOON

[BOJ] 14938번 서강그라운드 (C++)

728x90
반응형

https://www.acmicpc.net/problem/14938

 

14938번: 서강그라운드

예은이는 요즘 가장 인기가 있는 게임 서강그라운드를 즐기고 있다. 서강그라운드는 여러 지역중 하나의 지역에 낙하산을 타고 낙하하여, 그 지역에 떨어져 있는 아이템들을 이용해 서바이벌을

www.acmicpc.net

 

#include <iostream>
#include <algorithm>
#define INF 987654321
using namespace std;

int n, m, r;
int item[101];
int graph[101][101];
int answer;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> n >> m >> r;
	for(int i = 1; i <= n; i++) cin >> item[i];
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++){
			if(i == j) graph[i][j] = 0;
			else graph[i][j] = INF;
		}
	}
	
	// 간선 입력
	for(int i = 0; i < r; i++){
		int from, to, dis;
		cin >> from >> to >> dis;
		graph[from][to] = dis;
		graph[to][from] = dis;
	}
	
	// 플로이드 와샬
	for(int k = 1; k <= n; k++){
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				graph[i][j] = min(graph[i][k] + graph[k][j], graph[i][j]);
			}
		}
	}
	
	// 정점별로 아이템 수색
	for(int i = 1; i <= n; i++){
		int cnt = 0;
		for(int j = 1; j <= n; j++){
			if(graph[i][j] <= m){
				cnt += item[j];
			}
		}
		answer = max(answer, cnt);
	}
	cout << answer << endl;
	return 0;
}
728x90
반응형