HDU - 2112 HDU Today Dijkstra
注意:
1、去重边
2、终点和起点一样,应当输出0
3、是无向图
AC代码
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 150 + 5;
int vis[maxn], d[maxn], w[maxn][maxn];
map<string, int>Hash;
int cur;
int get_ID(string &p) {
if(!Hash.count(p)) Hash[p] = cur++;
return Hash[p];
}
struct point{
int d, u;
point(){
}
point(int d, int u):d(d), u(u) {
}
bool operator < (const point& p) const {
return d > p.d;
}
};
string s, e;
int dijkstra(int n) {
int x = get_ID(s), y = get_ID(e);
for(int i = 0; i < n; ++i) d[i] = inf;
d[x] = 0;
memset(vis, 0, sizeof(vis));
priority_queue<point>Q;
Q.push(point(0, x)); //将起点加入队列
while(!Q.empty()) {
point p = Q.top(); Q.pop();
int u = p.u;
if(u == y) return d[y];
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0; i < n; ++i) {
if(d[i] > d[u] + w[u][i]) { //update
d[i] = d[u] + w[u][i];
if(d[i] > inf) d[i] = inf;
Q.push(point(d[i], i));
}
}
}
return -1;
}
int main() {
int n;
while(scanf("%d", &n) == 1 && n != -1) {
memset(w, inf, sizeof(w));
Hash.clear();
cur = 0;
cin >> s >> e;
string x, y;
int cost;
for(int i = 0; i < n; ++i) {
cin >> x >> y >> cost;
int u = get_ID(x), v = get_ID(y);
w[v][u] = w[u][v] = min(w[u][v], cost); //去重边
}
printf("%d\n", dijkstra(Hash.size()));
}
return 0;
}
如有不当之处欢迎指出!
HDU - 2112 HDU Today Dijkstra的更多相关文章
- HDU 2112 HDU Today(Dijkstra)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others ...
- hdu 2112 HDU Today
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的 ...
- HDU 2112 HDU Today (Dijkstra算法)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 2112 HDU Today(map与dijkstra的结合使用)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 2112 HDU Today (最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目大意:给出起点和终点,然后算出最短的路. 不过有好多细节要注意: (1)起始点和终止点相等的 ...
- hdu 2112 HDU Today 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目意思:又是求最短路的,不过结合埋字符串来考查. 受之前1004 Let the Balloo ...
- HDU 2112 HDU Today(最短路径+map)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- hdu 2112 HDU Today (floyd算法)
这道题貌似在原来学长给我们的搞的小比赛中出过! 这次又让我遇到,果断拿下! 不过方法很蠢,跑了1000多ms,虽然要求5000ms以内! 题目就是给你一些位置之间的距离,然后再让你求特定的两点之间的距 ...
- HDU 2112 HDU Today 最短路
题目描述: Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这 ...
随机推荐
- SQL 优化经验总结34条(转)
(1) 选择最有效率的表名顺序(只在基于规则的优化器中有效): ORACLE 的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table)将被最 ...
- Servlet--传参和接参
OK,现在基本的Servlet接口和常用类都整理的差不多的,本篇博客开始整理Servlet和页面的交互. 1,接参 以下几个常用的方法: getParameter public String getP ...
- php微信扫码支付
一 概述 扫码支付是商户系统按微信支付协议生成支付二维码,用户再用微信"扫一扫"完成支付的模式.该模式适用于PC网站支付.实体店单品或订单支付.媒体广告支付等场景.前几天公司需要做 ...
- JDOJ-P1260 VIJOS-P1083 小白逛公园
首先,在这里给大家推荐一个网站,https://neooj.com:8082,这是我母校的网站 言归正传,题目描述 VIJOS-P1083 小白逛公园 Time Limit: 1 Sec Memor ...
- 【转】shell学习笔记(四)——条件测试
1 test 条件检测 当我要检测系统上面某些文件或者是相关的属性时,利用 test 这个命令来工作真是好用得不得了, 举例来说,我要检查 /home/oracle/zy是否存在时,使用: test ...
- mysql主从延迟高的原因
1.1.1故障1:从库数据与主库冲突 1 2 3 4 5 6 show slave status; 报错:且show slave status\G Slave_I/O_Running:Yes Slav ...
- java对象引用-要掌握的细节2
目的:加深对java引用变量的理解,避免数据的处理的偏差. 问题: 1.一个集合List ,将新建Student对象stu放入List之后,再给stu的成员变量例如id,name赋值.此时List中的 ...
- wpf timePicker 时间选择控件
wpf里有日期选择控件,但没有时间选择控件.其他地方也有类似的,但效果并不太好,而且复杂.所以就自己写了个.参考codeproject上的. 分两部分. 第一部分是.cs文件.也就是control控件 ...
- POJ2891 Strange Way to Express Integers [中国剩余定理]
不互质情况的模板题 注意多组数据不要一发现不合法就退出 #include <iostream> #include <cstdio> #include <cstring&g ...
- 用 label 控制 Pod 的位置 - 每天5分钟玩转 Docker 容器技术(128)
默认配置下,Scheduler 会将 Pod 调度到所有可用的 Node.不过有些情况我们希望将 Pod 部署到指定的 Node,比如将有大量磁盘 I/O 的 Pod 部署到配置了 SSD 的 Nod ...