[그래프] ROUTING 신호 라우팅

2021. 2. 19. 16:07알고리즘/알고리즘 문제 [easy]

1. 다익스트라 문제

 

이 문제는 간단한 다익스트라 문제이다.

 

이 문제의 핵심은 정수의 거리가 아닌 

 

실수인 노이즈의 증폭도라는 것이다.

 

 

따라서 간선을 지나가면서 가중치를 더하는게 아니라 곱을 하는 것이다.

 

 

2. 책의 풀이

 

책에서는 곱을 로그의 합으로 변환해서 문제를 해결하였다.

 

로그의 합의 최소를 구하고 exp()만 해주면 답을 구할 수 있다. 

 

 

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <queue>
#include <algorithm>
#include <vector>
#include <iomanip>
 
using namespace std;
 
 
int C;
int N, M;
 
vector<vector<pair<doubleint> > > circuit;
 
double dijkstra()
{
    vector<double> ret(N, 5.0/0.0);
    priority_queue<pair<doubleint> , vector<pair<doubleint> >, greater<pair<doubleint> > > pq;
    ret[0= 1.0;
    pq.push(make_pair(1.00));
    while(!pq.empty())
    {
        double cost = pq.top().first;
        int here = pq.top().second;
        pq.pop();
 
        if(ret[here] < cost) continue;
 
        for(int i = 0; i < circuit[here].size(); i++)
        {
            int there = circuit[here][i].second;
            double nextDist = cost * circuit[here][i].first;
            if(nextDist  < ret[there] )
            {
                ret[there] = nextDist;
                pq.push(pair<doubleint>(nextDist, there));
            }
        }
    }
    return ret[N-1];
}
 
 
int main()
{
    cin>>C;
    while(C--)
    {
 
        cin>>N>>M;
        circuit = vector<vector<pair<doubleint> > >(N);
        for(int i = 0; i < M; i++)
        {
            int a, b;
            double c;
            cin>>a>>b>>c;
            circuit[a].push_back(make_pair(c, b));
            circuit[b].push_back(make_pair(c, a));
        }
        cout<<fixed<<setprecision(10);
        cout<<dijkstra()<<"\n";
 
    }
}
cs

ROUTING