Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 풀이
- 해시구현
- 1764
- 별 찍기 10
- 백준 5397
- 백준 1406
- 백준 17779
- Stack 이란
- 백준 2447
- 조세퍼스 순열
- ㅣ풀이
- 구현
- 스택의 특징
- dfs
- 백준 1158
- AVL 시간 복잡도
- heap
- 게리멘더링2
- 자료구조
- 해시 구현
- 백준 17822
- C/C++ 구현
- c#
- 원판 돌리기
- qorwns
- 시간 복잡도
- 5397
- 백준
- 버킷 정렬
- 백준 17471
Archives
- Today
- Total
홍시홍의 프로그래밍
[백준 1916] 최소비용 구하기 본문
https://www.acmicpc.net/problem/1916
1916번: 최소비용 구하기
첫째 줄에 도시의 개수 N(1 ≤ N ≤ 1,000)이 주어지고 둘째 줄에는 버스의 개수 M(1 ≤ M ≤ 100,000)이 주어진다. 그리고 셋째 줄부터 M+2줄까지 다음과 같은 버스의 정보가 주어진다. 먼저 처음에는 그 버스의 출발 도시의 번호가 주어진다. 그리고 그 다음에는 도착지의 도시 번호가 주어지고 또 그 버스 비용이 주어진다. 버스 비용은 0보다 크거나 같고, 100,000보다 작은 정수이다. 그리고 M+3째 줄에는 우리가 구하고자 하는 구간
www.acmicpc.net
요구사항
주어진 정점까지 최소거리
풀이
기본 다익스트라 알고리즘
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct go{
int x;
int y;
};
int n,m;
int dist[1001];
vector<go>v[100001];
int heap_size=0;
go heap[100001];
int qsize=0;
void push(go data){
int target=heap_size+1;
while(target!=1 && heap[target/2].y > data.y){
heap[target]=heap[target/2];
target /=2;
}
heap[target]=data;
qsize++;
heap_size++;
}
void pop(){
int parent=1;
int child=2;
go temp=heap[heap_size];
while(child<heap_size){
if(child+1<heap_size && heap[child].y > heap[child+1].y)
child++;
if(temp.y<=heap[child].y)
break;
heap[parent]=heap[child];
parent=child;
child *=2;
}
heap[parent]=temp;
heap_size--;
qsize--;
}
bool com(go x, go y){
return (x.y<y.y);
}
int main()
{
cin>>n>>m;
for(int i=1; i<=n; i++){
dist[i]=987654321;
}
for(int i=0 ; i < m ; i++){
int x,y,z;
cin>>x>>y>>z;
v[x].push_back({y,z});
}
int s,c;
cin>>s>>c;
//priority_queue<pair<int,int>> q;
go temp;
temp.x=s;
temp.y=0;
push(temp);
dist[s]=0;
while(qsize!=0)
{
int cost = heap[1].y;
int node = heap[1].x;
// cout<<cost<<" "<<node<<endl;
pop();
for(int i=0 ; i < v[node].size() ; i++){
int nownode= v[node][i].x;
int nowcost = v[node][i].y +cost;
if(nowcost < dist[nownode]){
dist[nownode]=nowcost;
temp.x = nownode;
temp.y = nowcost;
push(temp);
}
}
}
cout<<dist[c]<<endl;
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 1944] 복제 로봇 (0) | 2020.01.11 |
---|---|
[백준 1194] 달이 차오른다, 가자 (0) | 2020.01.09 |
[백준 1753]최단 경로 (0) | 2020.01.09 |
[백준 10825] 국영수 (0) | 2020.01.09 |
[백준 1937] 욕심쟁이 판다 (0) | 2020.01.09 |
Comments