코드
~~~ c++
#include
#include
using namespace std;
int TEST_CASE;
int locationCount;
int edgeCount;
int fireCount;
int stationCount;
vector>vec[1001]; // first 목적지, second 거리
vectorStationLocation;
vectorfireLocation;
int dist[1001];
int check[1001];
int answer[1001];
int INF = 10000000;
int main() {
scanf("%d", &TEST_CASE);
for (int i = 0; i < TEST_CASE; i++) {
StationLocation.clear();
fireLocation.clear();
for (int i = 0; i < 1001; i++) {
vec[i].clear();
}
scanf("%d %d %d %d", &locationCount, &edgeCount, &fireCount, &stationCount);
for (int j = 0; j < edgeCount; j++) {
int from, to, length;
scanf("%d %d %d", &from, &to, &length);
vec[from].push_back(make_pair(to, length));
vec[to].push_back(make_pair(from, length));
}
for (int k = 0; k < fireCount; k++) {
int fire;
scanf("%d", &fire);
fireLocation.push_back(fire);
}
for (int l = 0; l < stationCount; l++) {
int station;
scanf("%d", &station);
vec[0].push_back(make_pair(station, 0));
}
// dijkstra
for (int t = 0; t <= locationCount; t++) {// dist 초기화
dist[t] = INF;
check[t] = false;
}
// 가상의 점 ( 소방서까지 0의 거리 )
dist[0] = 0; // 시작 위치를 0으로 초기화
for (int o = 0; o < locationCount - 1; o++) { // 도시 갯수 -1 번만큼 반복하기 위함
int min = INF;
int here = -1; // 현재위치
for (int n = 0; n <= locationCount; n++) { // 원래는 1부터 돌리는 것이나 0이라는 가상의 점을 돌린다
if (!check[n] && min > dist[n]) { //방문하지 않고 가장 작은 값 찾기
min = dist[n];
here = n;
}
}
check[here] = true;
for (int p = 0; p < vec[here].size(); p++) { // 인접 정점들을 모두 보면서 dist 갱신
if (dist[vec[here][p].first] > dist[here] + vec[here][p].second) //목적지의 현재 dist의 최단경로 값보다 지금 위치에서 가는 것이 작을 때 갱신
dist[vec[here][p].first] = dist[here] + vec[here][p].second;
}
}
int total = 0;
for (int y = 0; y < fireCount; y++) {
total += dist[fireLocation[y]];
}
printf("%d\n", total);
}
return 0;
}
~~~
문제를 풀다가 처음에는 모든 소방서에서 다익스트라를 돌렸는데 시간초과가 나와서, 가상의 점 0 번에서 소방서로 0의 거리로 갈 수 있다는 것을 만들어서 다시 작성해보았습니다.
우선 테스트케이스는 잘 나온는데 오답이 나오네요..
어떤것이 틀린걸까요?
9년 전
0개의 댓글이 있습니다.
정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면
온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야
합니다. 현재 문제를 푸셨습니다.
bgc8214
코드
>vec[1001]; // first 목적지, second 거리StationLocation;fireLocation;
~~~ c++
#include
#include
using namespace std;
int TEST_CASE;
int locationCount;
int edgeCount;
int fireCount;
int stationCount;
vector
vector
vector
int dist[1001];
int check[1001];
int answer[1001];
int INF = 10000000;
int main() {
scanf("%d", &TEST_CASE);
for (int i = 0; i < TEST_CASE; i++) {
}
~~~
문제를 풀다가 처음에는 모든 소방서에서 다익스트라를 돌렸는데 시간초과가 나와서, 가상의 점 0 번에서 소방서로 0의 거리로 갈 수 있다는 것을 만들어서 다시 작성해보았습니다.
우선 테스트케이스는 잘 나온는데 오답이 나오네요..
어떤것이 틀린걸까요?
9년 전