Recent Posts
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
- JanusGateway
- PytestPluginManager
- pytest
- 자원부족
- JanusWebRTC
- kotlin
- JanusWebRTCServer
- PersistenceContext
- taint
- 티스토리챌린지
- 겨울 부산
- preemption #
- 코루틴 빌더
- vfr video
- table not found
- 개성국밥
- Value too long for column
- tolerated
- python
- JanusWebRTCGateway
- 달인막창
- VARCHAR (1)
- 코루틴 컨텍스트
- terminal
- 헥사고날아키텍처 #육각형아키텍처 #유스케이스
- mp4fpsmod
- 깡돼후
- 오블완
- k8s #kubernetes #쿠버네티스
- Spring Batch
Archives
너와 나의 스토리
(BOJ) 12763 지각하면 안 돼 본문
반응형
문제: https://www.acmicpc.net/problem/12763
문제 풀이:
dfs로 풀었다
dist[n]에 pair로 시간이랑 금액 저장
현재 가는 정점이 원래 저장된 값(다른 루트로 온 결과 값)보다 시간이랑 금액 둘 다 손해면 안 가고
둘 중 하나라도 이득이면 감
만약 제한된 금액이나 시간이 이미 지나게 되면 안감
소스 코드:
typedef pair<int, pair<int, int>> P;
int n,m, time, money,res;
vector<pair<int, int>> dist;
vector<vector<P>> adj;
void dfs(int cur) {
if (cur == n) {
res = min(res, dist[cur].second);
return;
}
for (auto &i : adj[cur]) {
if (dist[i.first].first <= dist[cur].first + i.second.first&&dist[i.first].second <= dist[cur].second + i.second.second) continue;
else if (dist[cur].first + i.second.first > time || dist[cur].second + i.second.second > money) continue;
dist[i.first].first = dist[cur].first + i.second.first;
dist[i.first].second = dist[cur].second + i.second.second;
dfs(i.first);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
cin >> n>>time>>money>>m;
for (int i = 0; i <= n; i++) {
dist.push_back({inf,inf});
}
adj.resize(n + 1);
res = inf;
for (int i = 0; i < m; i++) {
int q, w, e, r;
cin >> q >> w >> e >> r;
adj[q].push_back({ w,{e,r} });
adj[w].push_back({ q,{e,r } });
}
dist[1] = make_pair(0, 0);
dfs(1);
if (res > money) cout << "-1\n";
else cout << res << '\n';
return 0;
}
반응형
'Algorithm > bfs, dfs' 카테고리의 다른 글
1949. [모의 SW 역량테스트] 등산로 조성 (0) | 2020.06.05 |
---|---|
[BOJ] 1938 통나무 옮기기 (0) | 2020.06.02 |
[BOJ] 13565 침투 (0) | 2019.10.04 |
(BOJ) 3830 교수님은 기다리지 않는다 (0) | 2019.07.31 |
(BOJ) 16236 아기 상어 (0) | 2019.03.07 |
Comments