50
Hint:
The best route is:
xiasha->ShoppingCenterofHangZhou->supermarket->westlake
虽然偶尔会迷路,但是因为有了你的帮助
**和**从此还是过上了幸福的生活。
??全剧终??
函数使用方法的不同运用
/*
Author: 2486
Memory: 2136 KB Time: 3151 MS
Language: G++ Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstdlib>
using namespace std;
const int maxn=170;
const int INF=100000000;
map<string,int>F;
string st,en,n1,n2;
int val,cnt,n;
struct edge {
int to,cost;
edge(int to,int cost):to(to),cost(cost) {}
};
vector<edge>G[maxn];
int d[maxn];
typedef pair<int,int>P;
void init() {
for(int i=0; i<maxn; i++)G[i].clear();
F.clear();
}
void dijkstra(int s) {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=0;i<cnt;i++)d[i]=INF;
d[s]=0;
que.push(P(0,s));
while(!que.empty()) {
P p=que.top();
que.pop();
int v=p.second;
if(d[v]<p.first)continue;
for(int i=0; i<G[v].size(); i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost) {
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
}
int main() {
//freopen("D://imput.txt","r",stdin);
while(~scanf("%d",&n),n!=-1) {
cnt=1;
init();
cin>>st>>en;
F[st]=cnt++;
F[en]=cnt++;
for(int i=0; i<n; i++) {
cin>>n1>>n2>>val;
if(F[n1]==0)F[n1]=cnt++;
if(F[n2]==0)F[n2]=cnt++;
G[F[n1]].push_back(edge(F[n2],val));
G[F[n2]].push_back(edge(F[n1],val));
}
dijkstra(F[st]);
if(d[F[en]]==INF)printf("-1\n");
else
printf("%d\n",d[F[en]]);
}
return 0;
}
int dis[155];
bool vis[155];
void dijkstra(int start,int thend) {
for(int i=0; i<155; i++)
dis[i]=INF,vis[i]=0;
dis[start]=0,vis[start]=1;
queue<int>q;
q.push(start);
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=0;
for(int j=0; j<g[u].size(); j++) {
int v=g[u][j].v;
int t=g[u][j].t;
if(dis[v]>dis[u]+t) {
dis[v]=dis[u]+t;
if(!vis[v]) {
vis[v]=1;
q.push(v);
}
}
}
}
if(dis[thend]>=INF)
puts("-1");
else
printf("%d\n",dis[thend]);
}