티스토리 뷰

Algorithm/BOJ

15971 두 로봇

henry1214 2018. 9. 16. 02:28

https://www.acmicpc.net/problem/15971



두 로봇의 위치 사이의 거리에서 가장 큰 비용의 간선을 빼주면 된다. 그러면 최소 비용으로 양쪽에서 오다가 서로 만날 수 있다.



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
29
30
31
32
33
34
35
36
37
38
39
40
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
 
int n,s,e,u,v,w,c[100001],ans,f;
vector<pair<int,int>> a[100001];
 
void dfs(int now,int cost,int mcost)
{
    c[now]=true;
    if(f) return;
    if(now==e)
    {
        f=1;
        ans=cost-mcost;
        return;
    }
    for(auto i : a[now])
    {
        int next=i.first;
        int ncost=i.second;
        if(c[next]==false)
            dfs(next,cost+ncost,max(mcost,ncost));
    }
}
 
int main()
{
    scanf("%d %d %d",&n,&s,&e);
    for(int i=0;i<n-1;i++)
    {
        scanf("%d %d %d",&u,&v,&w);
        a[u].push_back({v,w});
        a[v].push_back({u,w});
    }
    dfs(s,0,0);
    printf("%d\n",ans);
    return 0;
}
cs


'Algorithm > BOJ' 카테고리의 다른 글

1174 줄어드는 숫자  (0) 2018.09.20
1038 감소하는 수  (0) 2018.09.20
11723 집합  (0) 2018.09.13
2098 외판원 순회  (0) 2018.09.13
1563 개근상  (0) 2018.09.12
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday