Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <bits/stdc++.h>
- using namespace std;
- const int maxn = 1e5 + 10;
- const double INF = 2e9;
- vector<pair<int, double> > graph[maxn];
- int n, m;
- struct node {
- int idx;
- double cost;
- node () {}
- node(int _idx, double _cost) {
- idx = _idx;
- cost = _cost;
- }
- bool operator < (const node & tmp) const {
- return cost > tmp.cost;
- }
- };
- void dijkstra(int S, int E) {
- vector<double> dist(n, INF);
- vector<bool> visited(n, false);
- dist[S] = 0;
- priority_queue<node> pq;
- pq.push(node(S, 0));
- while(!pq.empty()) {
- node c = pq.top();
- pq.pop();
- if(visited[c.idx]) continue;
- visited[c.idx] = true;
- for(pair<int, double> tmp : graph[c.idx]) {
- int neighbour = tmp.first;
- double weight = tmp.second;
- if(!visited[neighbour] and c.cost + weight < dist[neighbour]) {
- pq.push(node(neighbour, c.cost + weight));
- dist[neighbour] = c.cost + weight;
- }
- }
- }
- printf("%.6lf\n", dist[E]);
- }
- int main() {
- int t;
- cin >> t;
- vector<pair<double, double> > v;
- for(int i = 0; i < t; i++) {
- double x, y;
- cin >> x >> y;
- v.push_back(make_pair(x, y));
- }
- for(int i = 0; i < t; i++) {
- for(int j = 0; j < t; j++) {
- double d = sqrt((v[i].first - v[j].first) * (v[i].first - v[j].first) + (v[i].second - v[j].second) * (v[i].second - v[j].second));
- if(d <= 10) {
- graph[i].push_back(make_pair(j, d));
- graph[j].push_back(make_pair(i, d));
- }
- }
- }
- n = t;
- dijkstra(0, n - 1);
- return 0;
- }
- /*
- 5 5
- 1 2 2
- 2 3 4
- 1 3 2
- 3 4 5
- 4 5 6
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement