회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
2
채점 현황
랭킹
게시판
그룹
더 보기
재채점 기록
블로그
강의
실험실
도움말
BOJ Stack
BOJ Book
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
제 코드에서 쓸데없이 시간을 많이 잡아먹는 부분이 있는지 궁금해요.
13308번 - 주유소
Horaegi
4년 전
0
2000ms안에 비빌 수 있을것 같은데 안비벼지네요 ㅠㅠ 혹시 제 코드에 시간을 많이 잡아먹는 요소가 있을까요?
#include <stdio.h> #include <algorithm> #include <vector> #include <queue> typedef long long ll; #define INF 9e18 using namespace std; struct pos { int end; ll edge; }; int oil[2505] = { 0, }; int newgph[2505][2505] = { 0, }; ll city[2505] = { 0, }; bool operator < (pos a, pos b) { return a.edge > b.edge; } vector <pos> gph[2505]; vector <pos> dgph[2505]; priority_queue<pos>pq; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &oil[i]); for (int i = 1; i <= n; i++) { city[i] = INF; for (int j = 1; j <= n; j++) { newgph[i][j] = INF; } } for (int i = 0; i < m; i++) { int start, end, dist; scanf("%d %d %d", &start, &end, &dist); gph[start].push_back({ end,dist }); gph[end].push_back({ start,dist }); } for (int start = 1; start <= n; start++) { pq.push({ start,0 }); newgph[start][start] = 0; while (!pq.empty()) { int from = pq.top().end; int herecost = pq.top().edge; pq.pop(); if (newgph[start][from] <herecost) continue; for (int i = 0; i < gph[from].size(); i++) { int to = gph[from][i].end; int therecost = herecost + gph[from][i].edge; if (newgph[start][to] > therecost) { newgph[start][to] = therecost; pq.push({ to,therecost }); } } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j || newgph[i][j] >= INF)continue; dgph[i].push_back({ j,(ll)(newgph[i][j]*oil[i]) }); } } pq.push({ 1,0 }); city[1] = 0; while (!pq.empty()) { int from = pq.top().end; ll herecost = pq.top().edge; pq.pop(); if (city[from] <herecost) continue; for (int i = 0; i <dgph[from].size(); i++) { int to = dgph[from][i].end; ll therecost = (ll) (herecost + dgph[from][i].edge); if (city[to] > therecost) { city[to] = therecost; pq.push({ to,therecost }); } } } printf("%lld", city[n]); }
댓글을 작성하려면
로그인
해야 합니다.
Horaegi 4년 전
2000ms안에 비빌 수 있을것 같은데 안비벼지네요 ㅠㅠ 혹시 제 코드에 시간을 많이 잡아먹는 요소가 있을까요?