题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112

spfa或者迪杰斯特拉都可以

注意公交车是有来回的---

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
#include <map>
#include <string>
using namespace std;
#define INF 0xfffffff
#define N 12000
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL; struct node
{
int v, t;
node(int v, int t):v(v), t(t){}
}; vector<vector<node> >G; int s, e;
int cnt, vis[N], dist[N]; int spfa()
{
met(vis, );
for(int i=; i<cnt; i++)
dist[i] = INF;
queue<int>Q;
Q.push(s);
vis[s] = ;
dist[s] = ; while(Q.size())
{
int p = Q.front(); Q.pop();
vis[p] = ;
int len = G[p].size(), q;
for(int i=; i<len; i++)
{
q = G[p][i].v;
if( dist[q] > dist[p]+G[p][i].t )
{
dist[q] = dist[p]+G[p][i].t;
if(vis[q] == )
{
vis[q] = ;
Q.push(q);
}
}
}
}
return dist[e]==INF?-:dist[e];
}
int main()
{
int n;
while(scanf("%d", &n), n!=-)
{
map<string, int> M;
M.clear();
G.clear();
G.resize(n*+); cnt = ; char s1[], s2[]; scanf("%s %s", s1, s2); if( !M[s1] ) M[s1] = cnt++;
if( !M[s2] ) M[s2] = cnt++; s = M[s1];
e = M[s2]; for(int i=; i<=n; i++)
{
int t, u, v;
scanf("%s %s %d", s1, s2, &t);
if(!M[s1]) M[s1] = cnt++;
if(!M[s2]) M[s2] = cnt++;
u = M[s1];
v = M[s2];
G[u].push_back(node(v, t));
G[v].push_back(node(u, t));
} printf("%d\n", spfa());
}
return ;
}

最新文章

  1. WebGIS中基于AGS的画圆查询简析以及通过Polygon来构造圆的算法
  2. Android热修复AndFix
  3. kNN算法python实现和简单数字识别
  4. HDU 5053 the Sum of Cube(简单数论)
  5. 一样的alert代码,样式不同
  6. [Java面试六]SpringMVC总结以及在面试中的一些问题.
  7. 详解jQ的support模块
  8. android.support.v4.app.Fragment和android.app.Fragment区别
  9. spring集成环境下的axis webservice的发布,调试
  10. Snagit 12 – 功能强的老牌截图软件
  11. [Environment Build] Maven环境配置
  12. C++运算符重载——重载一元运算符
  13. vs 中代码的字体也颜色设置
  14. 利用组策略禁用Oultook 各个版本的缓存模式!
  15. 他们主动布局(autolayout)环境的图像编辑器
  16. 自定义view(二)
  17. PCIE\AURORA\SRIO协议对比
  18. Redis学习-hash数据类型
  19. 1.Spring框架入门案例
  20. Nginx学习笔记(一)---Linux下安装Nginx

热门文章

  1. par函数的las参数-控制x轴和y轴标签的方向
  2. 关于C++中using namespace std
  3. luasql在Fedora20下的安装与使用示例
  4. xshell-常用指令汇总 linux 常用指令
  5. javascript 哈夫曼树构造
  6. Hbase1.1.0.1配置集群
  7. WPF 自定义命令 以及 命令的启用与禁用
  8. GLSL/C++ 实现滤镜效果
  9. PHP5 session 详解【经典】
  10. PHP Web 木马扫描器代码