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 65 66 67 68 69 70 71 72 73 74 75
| #include <cstdio> #include <cmath> #include <queue> using namespace std; typedef pair<int, int> PII; typedef pair<double, int> PDI; const double inf = 4e5 + 10; const int N = 210, M = 8e4 + 10; int n, m, pre[N]; PII p[N]; int idx = -1, hd[N], nxt[M], edg[M]; double d[N], wt[M]; double dijkstra(bool op) { for (int i = 1; i <= n; i++) d[i] = inf; d[1] = 0; priority_queue<PDI, vector<PDI>, greater<PDI>> q; q.push({0, 1}); while (!q.empty()) { int t = q.top().second; q.pop(); for (int i = hd[t]; ~i; i = nxt[i]) if (d[t] + wt[i] < d[edg[i]]) { d[edg[i]] = d[t] + wt[i]; if (op) pre[edg[i]] = i; q.push({d[edg[i]], edg[i]}); } } return d[n]; } double dis(int x, int y) { int dx = abs(p[x].first - p[y].first), dy = abs(p[x].second - p[y].second); return sqrt(dx * dx + dy * dy); } void add(int a, int b, double c) { nxt[++idx] = hd[a]; hd[a] = idx; edg[idx] = b; wt[idx] = c; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) hd[i] = -1; for (int i = 1; i <= n; i++) scanf("%d%d", &p[i].first, &p[i].second); for (int a, b; m; m--) { scanf("%d%d", &a, &b); double c = dis(a, b); add(a, b, c); add(b, a, c); } dijkstra(true); double res = inf; for (int i = n; i != 1; i = edg[pre[i] ^ 1]) { double tmp = wt[pre[i]]; wt[pre[i]] = inf; res = min(res, dijkstra(false)); wt[pre[i]] = tmp; } if (res == inf) puts("-1"); else printf("%.2lf", res); return 0; }
|