ural 1205. By the Underground or by Foot? Dijkstra
来源:互联网 发布:张强医生集团 知乎 编辑:程序博客网 时间:2024/05/20 14:44
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1205
题意描述:给定N个地铁站点坐标,给定步行和地铁速度,求出从A点到B点最小消耗时间;
思路大致就是将距离换算成时间,抽象成单源最短路径问题;
AC代码(这个暴丑,懒得改了):
//#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <stdio.h>#include <cstring>#include <string>#include <stack>#include <algorithm>#include <vector>#include <queue>#include <cmath>#include <map>#include <iomanip>using namespace std;vector<vector<double> >v;vector<pair<double, double> >cord;vector<pair<int, double> >node;vector<bool>vis;const int NAL = 99999999;int getOne(){// 找下一个距离最短点double _min = NAL;int idx;for (size_t i = 0; i <vis.size();i++)if (vis[i] == false && _min>node[i].second){_min = node[i].second;idx = i;}return idx;}void func(){double onFoot, onMetro;cin >> onFoot >> onMetro;int N; cin >> N;v.resize(N + 2, vector<double>(N + 2, NAL));cord.resize(N + 2);node.resize(N + 2, make_pair(-1, NAL));vis.resize(N + 2, false);for (int i = 1; i < N + 1; i++){cin >> cord[i].first >> cord[i].second;}int src, tar;while (cin >> src >> tar&&src&&tar){// 算地铁路径时间double tmp = sqrt(pow(cord[src].first - cord[tar].first, 2) + pow(cord[src].second - cord[tar].second, 2));tmp /= onMetro;v[src][tar] = tmp;v[tar][src] = tmp;}cin >> cord[0].first >> cord[0].second;cin >> cord[N + 1].first >> cord[N + 1].second;for (int i = 0; i <= N + 1; i++)for (int j = 0; j <= N + 1; j++){//用步行时间更新状态if (i == j)continue;double tmp = sqrt(pow(cord[i].first - cord[j].first, 2) + pow(cord[i].second - cord[j].second, 2));tmp /= onFoot;if (v[i][j] > tmp)v[i][j] = tmp;}node[0].second = 0;//for (int i = 1; i <= N + 1; i++)//node[i].second = v[0][i];while (true){src = getOne();if (src == N + 1)break;vis[src] = true;for (int i = 0; i <= N + 1; i++)if (i != src&&vis[i] == false && node[i].second > node[src].second + v[src][i]){node[i].second = node[src].second + v[src][i];node[i].first = src;}}cout << setprecision(7) << fixed << node[src].second << endl;stack<int>ans;while (node[src].first != -1){src = node[src].first;ans.push(src);}ans.pop();cout << ans.size();while (!ans.empty()){cout << ' ' << ans.top();ans.pop();}cout << endl;}int main(){//freopen("out.txt", "w", stdout);//freopen("in.txt", "r", stdin);func();}
0 0
- ural 1205. By the Underground or by Foot? Dijkstra
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra迪杰斯特拉算法)
- URAL 1205 By the Underground or by Foot (建图 + Dijkstra + 堆优化)
- URAL 1205 By the Underground or by Foot?
- By the Underground or by Foot?
- The Humble Programmer by Edsger W. Dijkstra
- By value? Or by reference?
- The behavior of App killed or restored by Android System or by users
- jaxb:extensionBindingPrefixes by disabling the strict mode or by using the extension
- and,or,order by
- java中的 by value or by reference
- java中的 by value or by reference
- JavaScript: Passing by Value or by Reference
- URAL 1651 Shorest Subchain DP #by Plato
- URAL 1828. Approximation by a Progression 数学
- URAL - 1828 Approximation by a Progression(最小二乘法)
- 谦卑的程序员(The Humble Programmer) by E.W.Dijkstra,1972
- mongo : query multi cols by "and" or by "or"
- U3D学习笔记(4)
- makefile中对.PHONE的认识
- Axis设置SOAP协议版本
- 翻过墙,驭天马行空
- hdu5406
- ural 1205. By the Underground or by Foot? Dijkstra
- LinkedIn架构这十年
- Caffe到底训练出了个什么东西:caffemodel解析
- 2015.6.25(CodeMirror插件)
- HBase基础知识(8):扫描操作之缓存与批量处理
- 修改设置->关于手机->法律信息 下有一些Item,如开放源代码许可、Google法律信息等,这里分析的是Android 4.4的代码
- hdu 2188 悼念512汶川大地震遇难同胞——选拔志愿者(巴什博奕)
- N皇后问题(回溯递归)
- Errors running builder 'JavaScript Validator' on project