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强.这 ...
随机推荐
- WebSphere--安全性
WebSphere应用服务器具有很好的安全性支持.安全性简单地说就是确定谁可访问重要的系统资源,这些系统资源包括文件.目录.程序.连接和数据库.以独立模式运行WebSphere应用服务器比作为 Web ...
- 如何用docker部署redis cluster
前言 由于本人是个docker控,不喜欢安装各种环境,而且安装redis-trib也有点繁琐,索性用docker来做redis cluster. 本文用的是伪集群,真正的集群放到不同的机器即可.端口是 ...
- try{}catch(e){}不能捕获到异常
只能捕获到ReferenceError异常,I don't know why. try{ aa();//这是一个未被定义的方法 }catch(e){ if(e instanceof Reference ...
- tomcat-users.xml 配置
一:tomcat6配置管理员信息 1:打开tomcat6下的~/conf/tomcat-users.xml文件,关于用户角色.管理员的信息都在这个配置文件中. 2:在配置文件<tomcat-us ...
- CString(转)
CString::Compare int Compare( LPCTSTR lpsz ) const; 返回值 字符串一样 返回0 小于lpsz 返回-1 大于lpsz 返回1 区分大小字符 ...
- 排序算法之low B三人组
排序low B三人组 列表排序:将无序列表变成有充列表 应用场景:各种榜单,各种表格,给二分法排序使用,给其他算法使用 输入无序列表,输出有序列表(升序或降序) 排序low B三人组 1. 冒泡排序 ...
- 【AIX】AIX内存机制
[AIX]AIX内存机制 1 虚拟内存 虚拟内存是物理内存和交换空间(Paging Space)组合形成的虚拟内存空间, 通过虚拟的地址空间映射到物理内存或者 Paging Space. 在 AIX ...
- es随想一
一.安全问题 前段时间网上看到的,大量的MongoDB服务器和ElasticSearch服务器被黑客攻击,留下勒索信息. ElasticSearch的2种客户端连接方式,transport方式默认的9 ...
- http协议重点
https://www.cnblogs.com/ranyonsue/p/5984001.html HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议) ...
- UbuntuNFS服务器配置
NFS服务的配置====================1,下载并安装NFS sudo apt-get install nfs-kernel-server 2,配置NFS sudo vi /etc/e ...