BOJ 1238
νν° μ±κ³΅λ€κ΅μ΄
1 μ΄ 128 MB 29692 14648 9730 47.454% λ¬Έμ
Nκ°μ μ«μλ‘ κ΅¬λΆλ κ°κ°μ λ§μμ ν λͺ μ νμμ΄ μ΄κ³ μλ€.
μ΄λ λ μ΄ Nλͺ μ νμμ΄ X (1 ≤ X ≤ N)λ² λ§μμ λͺ¨μ¬μ νν°λ₯Ό λ²μ΄κΈ°λ‘ νλ€. μ΄ λ§μ μ¬μ΄μλ μ΄ Mκ°μ λ¨λ°©ν₯ λλ‘λ€μ΄ μκ³ iλ²μ§Έ κΈΈμ μ§λλλ° Ti(1 ≤ Ti ≤ 100)μ μκ°μ μλΉνλ€.
κ°κ°μ νμλ€μ νν°μ μ°ΈμνκΈ° μν΄ κ±Έμ΄κ°μ λ€μ κ·Έλ€μ λ§μλ‘ λμμμΌ νλ€. νμ§λ§ μ΄ νμλ€μ μλ κ²μλ¬μ μ΅λ¨ μκ°μ μ€κ³ κ°κΈ°λ₯Ό μνλ€.
μ΄ λλ‘λ€μ λ¨λ°©ν₯μ΄κΈ° λλ¬Έμ μλ§ κ·Έλ€μ΄ μ€κ³ κ°λ κΈΈμ΄ λ€λ₯Όμ§λ λͺ¨λ₯Έλ€. Nλͺ μ νμλ€ μ€ μ€κ³ κ°λλ° κ°μ₯ λ§μ μκ°μ μλΉνλ νμμ λꡬμΌμ§ ꡬνμ¬λΌ.
μ λ ₯
첫째 μ€μ N(1 ≤ N ≤ 1,000), M(1 ≤ M ≤ 10,000), Xκ° κ³΅λ°±μΌλ‘ ꡬλΆλμ΄ μ λ ₯λλ€. λ λ²μ§Έ μ€λΆν° M+1λ²μ§Έ μ€κΉμ§ iλ²μ§Έ λλ‘μ μμμ , λμ , κ·Έλ¦¬κ³ μ΄ λλ‘λ₯Ό μ§λλλ° νμν μμμκ° Tiκ° λ€μ΄μ¨λ€. μμμ κ³Ό λμ μ΄ κ°μ λλ‘λ μμΌλ©°, μμμ κ³Ό ν λμ Aμμ λ€λ₯Έ λμ Bλ‘ κ°λ λλ‘μ κ°μλ μ΅λ 1κ°μ΄λ€.
λͺ¨λ νμλ€μ μ§μμ Xμ κ°μ μκ³ , Xμμ μ§μΌλ‘ λμμ¬ μ μλ λ°μ΄ν°λ§ μ λ ₯μΌλ‘ μ£Όμ΄μ§λ€.
μΆλ ₯
첫 λ²μ§Έ μ€μ Nλͺ μ νμλ€ μ€ μ€κ³ κ°λλ° κ°μ₯ μ€λ 걸리λ νμμ μμμκ°μ μΆλ ₯νλ€.
μμ μ λ ₯ 1 볡μ¬
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
μμ μΆλ ₯ 1 볡μ¬
10β
λ Έλμ κ°μκ° 1000κ°λΌ νλ‘μ΄λλ νλ² ν΄λ΄€λλ° μμ μκ°μ΄κ³Όλ€. μμ¬ν κ² μμ§ 10^9 κ°κΉμμ§λ©΄ λ°λ‘ λ¬λ€.
Nκ°μ λ Έλμμ λ€μ΅μ€νΈλΌλ₯Ό λλ¦¬κ³ , Xλ²μ§Έ λ Έλμμ λ€μ΅μ€νΈλΌλ₯Ό λ리λ
N * (N-1) + log E
μ 체λ₯Ό λ€ λλ €μ£Όμ΄μΌ νλ Nκ°μ λ Έλ pqμμ©ν κ°μ λ½κΈ°
μ λμ μκ°μ΄ λμ, 1000^2 log 10000 μ λλ‘ μμ μ μ΄λ€.
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 987654321
using namespace std;
int N, M, X, ans;
vector<vector<pair<int,int>>>graph(1001);
vector<int> dstra(int start){
vector<int>dist(1001,INF);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;
dist[start] = 0;
pq.push({0,start});
while(!pq.empty()){
int cur_node = pq.top().second;
int cur_dist = pq.top().first;
pq.pop();
for(auto node : graph[cur_node]){
int nxt_node = node.second;
int nxt_dist = node.first + cur_dist;
if(dist[nxt_node] > nxt_dist){
dist[nxt_node] = nxt_dist;
pq.push({nxt_dist, nxt_node});
}
}
}
return dist;
}
int dstra2(int start){
vector<int>dist(1001,INF);
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;
dist[start] = 0;
pq.push({0,start});
while(!pq.empty()){
int cur_node = pq.top().second;
int cur_dist = pq.top().first;
pq.pop();
for(auto node : graph[cur_node]){
int nxt_node = node.second;
int nxt_dist = node.first + cur_dist;
if(dist[nxt_node] > nxt_dist){
dist[nxt_node] = nxt_dist;
pq.push({nxt_dist, nxt_node});
}
}
}
return dist[X];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>N>>M>>X;
for(int i=0; i<M; i++){
int a, b, c;
cin>>a>>b>>c;
graph[a].push_back({c,b});
}
for(int i=0; i<graph.size(); i++)
sort(graph[i].begin(), graph[i].end());
vector<int>x_dist = dstra(X);
for(int i=1; i<=N; i++){
if(i == X) continue;
ans = max(ans, x_dist[i]+dstra2(i));
}
cout<<ans;
}