HDU Today---hdu2112(最短路-_-坑在是无向图)
2024-09-04 11:08:36
题目链接: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 ;
}
最新文章
- WebGIS中基于AGS的画圆查询简析以及通过Polygon来构造圆的算法
- Android热修复AndFix
- kNN算法python实现和简单数字识别
- HDU 5053 the Sum of Cube(简单数论)
- 一样的alert代码,样式不同
- [Java面试六]SpringMVC总结以及在面试中的一些问题.
- 详解jQ的support模块
- android.support.v4.app.Fragment和android.app.Fragment区别
- spring集成环境下的axis webservice的发布,调试
- Snagit 12 – 功能强的老牌截图软件
- [Environment Build] Maven环境配置
- C++运算符重载——重载一元运算符
- vs 中代码的字体也颜色设置
- 利用组策略禁用Oultook 各个版本的缓存模式!
- 他们主动布局(autolayout)环境的图像编辑器
- 自定义view(二)
- PCIE\AURORA\SRIO协议对比
- Redis学习-hash数据类型
- 1.Spring框架入门案例
- Nginx学习笔记(一)---Linux下安装Nginx