标签:
题目链接: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, 0); for(int i=1; i<cnt; i++) dist[i] = INF; queue<int>Q; Q.push(s); vis[s] = 1; dist[s] = 0; while(Q.size()) { int p = Q.front(); Q.pop(); vis[p] = 0; int len = G[p].size(), q; for(int i=0; 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] == 0) { vis[q] = 1; Q.push(q); } } } } return dist[e]==INF?-1:dist[e]; } int main() { int n; while(scanf("%d", &n), n!=-1) { map<string, int> M; M.clear(); G.clear(); G.resize(n*2+5); cnt = 1; char s1[50], s2[50]; 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=1; 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 0; }
HDU Today---hdu2112(最短路-_-坑在是无向图)
标签:
原文地址:http://www.cnblogs.com/zhengguiping--9876/p/5674724.html