회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
[C/C++] 집구하기 어디가 문제일까요?ㅠ
13911번 - 집 구하기
rlawnddnjs12
2년 전
0
다익스트라 2번돌려서 모든 조건에 만족하는 집을 찾고자 하는데 어디가 문제일까요 ?? ㅜㅜ 도와주세요
#include <iostream> #include <queue> #include <vector> #include <stack> #include <string> #include <cstring> #include <sstream> #include <set> #include <unordered_set> #include <map> #include <algorithm> #include <cmath> #include <ctime> #define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0) #define ENDL cout << endl #define ll long long #define ull unsigned long long #define INF 18549876543 #define Mod 1000000009 #define endl '\n' #define pil pair<int,int> using namespace std; typedef struct GraphNode { int dest; int cost; GraphNode* next; }GraphNode; typedef struct Graph { GraphNode* adj; }Graph; int V, E; priority_queue<pair<int, int>, vector<pair<int, int>>> pq; void Init_Graph(Graph* gph, int count) { gph->adj = (GraphNode*)malloc(sizeof(GraphNode) * (count + 1)); for (int i = 1; i <= count; i++) { gph->adj[i].next = NULL; } } void Reset(Graph* gph, int count) { for (int i = 1; i <= count; i++) { gph->adj[i].next = NULL; free(gph->adj[i].next); } } int AddDirectedLinked(Graph* gph, int src, int dst, int cost) { GraphNode* Temp = (GraphNode*)malloc(sizeof(GraphNode)); Temp->cost = cost; Temp->dest = dst; Temp->next = gph->adj[src].next; gph->adj[src].next = Temp; return 1; } int AddUnDirectedLinked(Graph* gph, int src, int dst, int cost) { return AddDirectedLinked(gph, src, dst, cost) && AddDirectedLinked(gph, dst, src, cost); } void Dijkstra(Graph* gph, vector<int>& dist) { while (!pq.empty()) { int cost = -pq.top().first; int node = pq.top().second; pq.pop(); GraphNode* head = gph->adj[node].next; while (head) { if (head->cost + cost < dist[head->dest]) { dist[head->dest] = head->cost + cost; pq.push({ -(head->cost + cost), head->dest }); } head = head->next; } } } int main() { fastio; Graph graph; cin >> V >> E; Init_Graph(&graph, V); int a, b, c; while (E--) { cin >> a >> b >> c; AddUnDirectedLinked(&graph, a, b, c); } int M, x, S, y; cin >> M >> x; vector<int> dist1(V+1, INF); for (int i = 0; i < M; i++) { int mac; cin >> mac; dist1[mac] = 0; pq.push({ 0, mac }); } Dijkstra(&graph, dist1); cin >> S >> y; vector<int> dist2(V+1, INF); for (int i = 0; i < S; i++) { int star; cin >> star; dist2[star] = 0; pq.push({ 0, star }); } Dijkstra(&graph, dist2); int answer_node=-1, answer_dist = INF; for (int node = 1; node <= V; node++) { if (dist1[node] && dist2[node] && dist1[node] <= x && dist2[node] <= y) answer_dist = answer_dist < dist1[node] + dist2[node] ? answer_dist : dist1[node] + dist2[node]; } if (answer_dist != INF) cout << answer_dist << endl; else cout << -1 << endl; return 0; }
댓글을 작성하려면
로그인
해야 합니다.
rlawnddnjs12 2년 전
다익스트라 2번돌려서 모든 조건에 만족하는 집을 찾고자 하는데 어디가 문제일까요 ?? ㅜㅜ 도와주세요