-
Notifications
You must be signed in to change notification settings - Fork 1
31-kokeunho #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
31-kokeunho #126
Conversation
wnsmir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ง๋ฐฉ๋ฌ ์ ์ฅ์์ ๊ต์ฅํ ๋ฐ์คํ ๋ฌธ์ ๋ค์,,
๋ฌธ์ ๋ฅผ ์ฝ์๋ง์ ๋ฐ๋ก ๋ค์ต์คํธ๋ผ๋ฅผ ๋ ์ฌ๋ ธ์ต๋๋ค.
๋ค๋ง ์๊ฐ์ด ํ๋ฌ๊ฐ๋๋๋ก ๋ค์ต์คํธ๋ผ๋ก ๊ฐ๋์์์ ๋ฉด์ ์ฅ๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ฒ๋๋ฉด
1%์ด์์ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ์ต๋๋ค. (์์์ํ์ง๋ง,,)
์๊ฐ์ ์ข ๋ ํด๋ณธ ๊ฒฐ๊ณผ ๋ฉด์ ์ฅ๋ค์์ ๊ฐ๋์์ ๊ฑฐ๋ฆฌ๋ฅผ ๋ชจ๋ ๊ตฌํด๋๊ณ , ์ด๋ฅผ ์ ์ฅํด๋ ๋ฐฐ์ด์์ ๋์๋ฒํธ์ ๊ฑฐ๋ฆฌ๋ฅผ ์ถ๋ ฅํ๋ฉด ๋ ๊ฒ ๊ฐ์์ต๋๋ค.
ํ๊ฐ์ง ์๊ฐํด์ผํ๋๊ฑด
๋ฌธ์ ๋ ๋จ๋ฐฉํฅ์ด๊ณ , ๋์์์ ๋ฉด์ ์ฅ๊น์ง์ ๊ธธ์ด ๋ณด์ฅ๋์ด์๋ค๋ ๊ฒ์
๋๋ค.
์ฆ ๋ฉด์ ์ฅ์์ ๋์๊น์ง์ ๊ธธ์ ๊ตฌํ๊ธฐ ์ํด์๋ ์ญ๋ฐฉํฅ์ผ๋ก ์ ์ฅํด์ฃผ์ด์ผ ํฉ๋๋ค.
๊ทธ ์ธ์๋ heapq๋ฅผ์ด ๋ด๋ฐฑํ ๋ค์ต์คํธ๋ผ๋ฌธ์ ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
N, M, K = map(int, input().split())
import heapq
def dijkstra(spots, graph, N):
INF = 10**18
dist = [INF] * (N+1)
heap = []
for start in spots:
dist[start] = 0
heapq.heappush(heap, (0, start))
while heap:
d, cur = heapq.heappop(heap)
if dist[cur] < d:
continue
# WTG == Where To Go
for WTG, cost in graph[cur]:
if dist[WTG] > d + cost:
dist[WTG] = d + cost
heapq.heappush(heap, (dist[WTG], WTG))
return dist
graph = [[] for _ in range(N+1)]
for _ in range(M):
u, v, c = map(int, input().split())
graph[v].append((u, c))
spots = list(map(int, input().split()))
farest_dist = -1
city = 0
dist = dijkstra(spots, graph, N)
for i in range(1, N+1):
if dist[i] > farest_dist:
farest_dist = dist[i]
city = i
if dist[i] == farest_dist and i < city:
city = i
print(city)
print(farest_dist)์ ์ฐธ๊ณ ๋ก ์ ๋์ฃผ๋ก INF์ 987654321์ ์ฐ๋๋ฐ
์ด๋ฌธ์ ์์ ๊ฐ์ ์ ์๊ฐ 100000, ๊ฐ์ค์น๋ 100000์ด๋ผ 10^10๊น์ง ์ปค์ง ์ ์์ด ํ๋ฒ ํ๋ ธ์ต๋๋ค..
์์ผ๋ก๋ 10*18์ ์จ์ผ๊ฒ ๋ค๋ ๊ตํ์ ์ป์ด๊ฐ๋๋ค~
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๊ฐ ๋ญ์ ํ๋ ธ์๊น์? ๋ฉด์ ์ฅ -> ๋์ ์ญ๋ฐฉํฅ ๊ทธ๋ํ๋ฅผ ๊ตฌ์ถํด์ ํ๋ฉด ๋์ง ํ๊ณ ์๊ฐ ๋ฐ๋ก ๋ ์ฌ๋ ธ๋๋ฐ ์ ์๊ฐ ์ด๊ณผ ๋๋ค๊ณ ์๊ฐํ์๊น์... ์๊ฐ ๊ฐ ๋ฉด์ ์ฅ ๋ ธ๋๋ง๋ค ๋ค์ต์คํธ๋ผ๋ฅผ ๋ค ๋๋ ค์ผํ๋ค๊ณ ์ฐฉ๊ฐํ ๊ฒ ๊ฐ์ต๋๋ค ํํ...
์ด๋ ๊ฒ ๋ฐฉํฅ ๊ทธ๋ํ๋ฅผ ๋ค์ง์ด์ ์ ์ฅํ ๋ค, ๋ชจ๋ ์ถ๋ฐ์ง -> ๋ชฉ์ ์ง๊ฐ ์๋ ๋ชฉ์ ์ง -> ๋ชจ๋ ์ถ๋ฐ์ง๋ก ๊ฑฐ๋ฆฌ ์ ๋ณด๋ฅผ ์ป๋ ๊ฑด ๊ตญ๋ฐฅ๊ฐ์ ์ ํ์ด์ฃ . ์ค์ ์์๋ ์ ๋ ์ฌ๋ฆฌ์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M, K; cin >> N >> M >> K;
vector<vector<pair<int, long long>>> graph(N + 1);
while(M--) {
int U, V, C; cin >> U >> V >> C;
graph[V].emplace_back(U, C); // reverse
}
using Node = pair<long long, int>;
priority_queue<Node, vector<Node>, greater<>> pq;
vector<long long> distances(N + 1, 1e10);
unordered_set<int> cities; cities.reserve(K);
while(K--) {
int node; cin >> node;
pq.emplace(0, node);
distances[node] = 0;
cities.emplace(node);
}
while(!pq.empty()) {
const auto [du, u] = pq.top();
pq.pop();
if(distances[u] < du) {
continue;
}
for(const auto& [v, dv] : graph[u]) {
if(du + dv < distances[v]) {
distances[v] = du + dv;
pq.emplace(distances[v], v);
}
}
}
long long maxDistance = 0;
long long minNode = 0;
for(int node = 1; node <= N; ++node) {
if(cities.count(node)) {
continue;
}
if(distances[node] > maxDistance) {
maxDistance = distances[node];
minNode = node;
}
}
cout << minNode << "\n" << maxDistance;
return 0;
}
kangrae-jo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๋ฏธ์๋ ๋ฌธ์ ๊ตฐ์.
๊ฒฝ๋ก๋ฅผ ๋ฐ๋๋ก ๋๋ ค์ ์ ์ฅํ๋ ๊ฒ๊ณผ ๋ฉด์ ์ฅ์ source๋ก ๋์ด์ ๋ค์ต์คํธ๋ผ๋ฅผ ๋๋ฆฌ๋ ๊ฒ์ ์๊ฐํด๋์ด์.
๊ทผ๋ฐ ์ ๋ฌธ์ ๊ฐ ์ํ๋ฆฌ๋ ๊ฑฐ์ง.... ๋ผ๊ณ ์๊ฐ์ ํ๊ณ gpt์ ๋ฃ์ด๋ดค์ด์...
์์ธ์ ์๊ณ ํ์จ๋ง ๋์ค๋๋ผ๊ตฌ์.
์ ๊ฐ ์ฒ์์ ์ง ์ฝ๋๋ ์๋์๊ฐ์์.
while (!pq.empty()) {
auto [dist, cur] = pq.top();
pq.pop();
if (dist > distances[cur]) continue;
for (auto [dist_, next] : graph[cur]) {
long long newDist = dist + dist_;
if (newDist < distances[next]) {
distances[next] = newDist;
pq.push({newDist, next});
}
}
}for๋ฌธ์์ ๊บผ๋ผ๋ ๋ฐ๋๋ก ๊บผ๋ด๊ณ ์์๋๋ผ๊ตฌ์...
ํ์๋ c++์ pq๋ฅผ ํ์ฉํ๊ธฐ ์ํด์ {๊ฑฐ๋ฆฌ, ๋
ธ๋} ๋ก ์ ์ฅํด๋จ๊ฑฐ๋ ์. ๊ทธ๋์ ์์ฃผ ํท๊ฐ๋ คํ๋ ๋ถ๋ถ์
๋๋ค.
๊ทธ๋ฆฌ๊ณ ์ดํ์๋ ๋ช๋ฒ ํ๋ ค์ ๋ฆฌ๋ฏธํธ๋ฅผ longlong์ผ๋ก ๋ฐ๊พธ์ด์ฃผ์๊ณ ํต๊ณผํ์ต๋๋ค ใ ใ
์๋ฃจ์ ์์ฒด๋ ์ฌ๋ฐ๋ฅด๊ฒ ์๊ฐํด๋๋ค๋ ๊ฒ์ ํฐ ์๋ฏธ๋ฅผ ๋ก๋๋ค์.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const long long INF = 1e18;
int N, M, K;
vector<vector<pair<int, int>>> graph;
vector<long long> distances;
void solution(vector<int>& places) {
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
distances.assign(N + 1, INF);
for (int src : places) {
pq.push({0, src});
distances[src] = 0;
}
while (!pq.empty()) {
auto [dist, cur] = pq.top();
pq.pop();
if (dist > distances[cur]) continue;
for (auto [next, dist_] : graph[cur]) {
long long newDist = dist + dist_;
if (newDist < distances[next]) {
distances[next] = newDist;
pq.push({newDist, next});
}
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> N >> M >> K;
graph = vector<vector<pair<int, int>>>(N + 1);
for (int i = 0; i < M; i++) {
int U, V, C;
cin >> U >> V >> C;
graph[V].push_back({U, C});
}
vector<int> places(K);
for (int i = 0; i < K; i++) {
cin >> places[i];
}
solution(places);
int answer = 1;
for (int i = 2; i <= N; i++) {
if (distances[i] > distances[answer]) answer = i;
}
cout << answer << '\n' << distances[answer] << '\n';
return 0;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋น์ฐํ ๋ค์ต์คํธ๋ผ๋ฅผ ํตํด ํ์ด์ผํ๋ค๊ณค ์๊ฐํ์ง๋ง ๋ต์ด ๋ ์ค๋ฅด์ง ์์์ต๋๋ค. ์ด์ฐจํผ ๋ฉด์ ์ฅ์ ์ต๋ ๊ฐ์๋ V(๋ชจ๋ ๋์์ ์)์ด๊ธฐ ๋๋ฌธ์ O(VElogV)๊ฐ ๋ ๊ฑฐ๋ผ ์๊ฐํด์ ์๊ฐ์ด๊ณผ๋ฅผ ์์ํ์์ฃ .
20๋ถ ๊ณ ๋ฏผํ๋ค๊ฐ ์ ๋ง ๋ต์ด ์๋ณด์ฌ์ ์ด๋ฒ์๋ ๋ต์ง๋ฅผ ๋ดค์ต๋๋ค. ์๋ฃจ์
์ด ์ ๋ ๋น์ทํ๋๋ฐ ํ์ ๋ฏธ๋ฆฌ ๋ชจ๋ ์ถ๋ฐ์ง๋ฅผ ๋ฃ๊ณ distance๋ฐฐ์ด์ ์ด๊ธฐํํ ์ค์ ์ ๋ง ๋ชฐ๋๋ค์.!
์ด๋ค ์ต์๊ฒฝ๋ก ๋ฌธ์ ๋ ๋ค์ต์คํธ๋ผ๋ก ํ๋ฆด ์ ์๋ค๋๊ฑธ ๋ค์๊ธ ํ์ธํ์ต๋๋ค :)
๊ทผ๋ฐ ๋๋ฌด ์ค๋๋ง์ด๋ผ... ๋ค์ต์คํธ๋ผ๋ฅผ ๊ตฌํํ๋๋ฐ ์ด์ง ์ ๋จน์.. ๋ถ๋ฐํ๊ฒ ์ต๋๋ค ๊ณ ์ํ์ จ์ด์~
package beakjoon;
import java.io.*;
import java.util.*;
public class Sol17835 {
static int V, E, K;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
V = Integer.parseInt(st.nextToken());
E = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
List<List<int[]>> graph = new ArrayList<>();
List<Integer> spot = new ArrayList<>();
for (int i = 0; i <= V; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < E; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
graph.get(v).add(new int[]{u, c});
}
st = new StringTokenizer(br.readLine());
for (int i = 0; i < K; i++) {
spot.add(Integer.parseInt(st.nextToken()));
}
br.close();
long[] distance = dijkstra(graph, spot);
long maxDist = -1;
int maxCity = 0;
for (int i = 1; i <= V; i++) {
if (distance[i] > maxDist) {
maxCity = i;
maxDist = distance[i];
}
}
System.out.println(maxCity);
System.out.println(maxDist);
}
private static long[] dijkstra(List<List<int[]>> graph, List<Integer> spot) {
PriorityQueue<Node> pq = new PriorityQueue<>((a, b) -> Long.compare(a.dist, b.dist));
long[] distance = new long[V + 1];
Arrays.fill(distance, Long.MAX_VALUE);
for (Integer s : spot) {
distance[s] = 0L;
pq.offer(new Node(s, 0L));
}
while (!pq.isEmpty()) {
int city = pq.peek().city;
long dist = pq.poll().dist;
if (dist > distance[city]) continue;
for (int[] next : graph.get(city)) {
long nextDist = next[1] + dist;
if (nextDist < distance[next[0]]) {
pq.offer(new Node(next[0], nextDist));
distance[next[0]] = nextDist;
}
}
}
return distance;
}
static class Node {
int city;
long dist;
public Node(int city, long dist) {
this.city = city;
this.dist = dist;
}
}
}
๐ ๋ฌธ์ ๋งํฌ
[BOJ] ๋ฉด์ ๋ณด๋ ์น๋ฒ์ด๋ค https://www.acmicpc.net/problem/17835
โ๏ธ ์์๋ ์๊ฐ
1h 30min
โจ ์๋ ์ฝ๋
์ด๋ฒ์ ๋ค์ต์คํธ๋ผ๋ก ํผ ๋ฌธ์ ๋ฅผ ๊ฐ์ ธ์์ต๋๋ค.
๋์๋ 100,000๊ฐ๊น์ง ๊ฐ๋ฅํ๊ณ ๋ฉด์ ์ฅ์ ๊ฐ์๋ ๊ทธ๋งํผ ๋ง์์ง ์ ์๊ธฐ ๋๋ฌธ์
๊ฐ ๋์๋ก๋ถํฐ ๋ค์ต์คํธ๋ผ๋ฅผ ์คํํ์ฌ ๋ฉด์ ์ฅ๊น์ง์ ๊ฑฐ๋ฆฌ๋ฅผ ๊ณ์ฐํ๋ ๊ฒ์ ๋นํจ์จ์ ์ผ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค.
๊ทธ๋์ ๋ฉด์ ์ฅ๋ค๋ก๋ถํฐ ๋ค์ต์คํธ๋ผ๋ฅผ ์คํํ์ฌ ์ต์ฅ๊ฑฐ๋ฆฌ์ ๋์๋ฅผ ์ฐพ์์ต๋๋ค.
ํด๋น ๋ฌธ์ ๋ ๋จ๋ฐฉํฅ ๊ทธ๋ํ ๋ฌธ์ ์ธ๋ฐ์.
๋ฉด์ ์ฅ๋ค๋ก๋ถํฐ ๋ค์ต์คํธ๋ผ๋ฅผ ์คํํ๊ธฐ ์ํด
์ ๋ ฅ ๋ฐ์ ๊ฐ์ ์ ๋ฐ๋ ๋ฐฉํฅ์ผ๋ก ์ ์ฅํ์์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ์๊น ์ธ๊ธํ๋๋ก ๋ฉด์ ์ฅ์ ๊ฐ์๋ ๋์์ ๊ฐ์๋งํผ ๋ง์์ง ์ ์๊ธฐ ๋๋ฌธ์
๋ฉด์ ์ฅ๋ง๋ค ๋ค์ต์คํธ๋ผ๋ฅผ ์ํํ๋ ๊ฒ์ ๋นํจ์จ์ ์ด๋ผ๊ณ ํฉ๋๋ค.
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ์ด๋ผ๊ณ ํ๋ฉด ํ๋์ ์ถ๋ฐ์ ์์ ์์ํ๋ ๊ฒ์ด ์ต์ํ๋ฐ
์ถ๋ฐ์ ์ ํ๋ฒ์ ๋ฃ์ด ์ฌ๋ฌ ์์์ ์์ ๊ฐ ์ ์ ๊น์ง์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ ์ ์์ต๋๋ค.
๊ทธ๋ ๊ฒ ๊ตฌํ ๊ฐ ์ ์ ๊น์ง์ ์ต๋จ ๊ฒฝ๋ก ์ค ๊ฐ์ฅ ๋จผ ๊ฑฐ๋ฆฌ์ ๋์ ๋ฒํธ๋ฅผ ์ถ๋ ฅํ๋ฉด ๋์ ๋๋ค.
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
์ฌ๋ฌ ์์์ ์์ ๊ฐ ์ ์ ๊น์ง์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๋ ๋ค์ค ์์์ ๋ค์ต์คํธ๋ผ๋ฅผ ์๊ฒ ๋์์ต๋๋ค