题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2112、
题目大意:
求起点到终点的最短路
解题思路:
对地名进行编号即可
然后直接dijkstra算法
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = + ;
const int INF = 0x3f3f3f3f;
int Map[maxn][maxn];
int v[maxn], d[maxn];
int n, m;
set<string>cnt;
map<string, int>G;
int GetID(string s)
{
if(cnt.count(s))return G[s];
cnt.insert(s);
return G[s] = cnt.size();
}
void dijkstra(int s, int t)
{
memset(v, , sizeof(v));
for(int i = ; i <= n; i++)d[i] = (i == s ? : INF);
for(int i = ; i < n; i++)
{
int x = -, m = INF;
for(int i = ; i <= n; i++)if(!v[i] && d[i] <= m)m = d[x = i];
v[x] = ;
for(int i = ; i <= n; i++)
{
if(d[i] > d[x] + Map[x][i])
{
d[i] = d[x] + Map[x][i];
}
}
}
if(d[t] == INF)d[t] = -;
cout<<d[t]<<endl;
} int main()
{
while(scanf("%d", &n) != EOF && n != -)
{
int u, v, w;
string s1,s2;
cnt.clear();
G.clear();
cin >> s1 >> s2;
int s = GetID(s1);
int t = GetID(s2);
memset(Map, INF, sizeof(Map));
while(n--)
{
cin >> s1 >> s2 >> w;
Map[GetID(s1)][GetID(s2)] = Map[GetID(s2)][GetID(s1)] = min(Map[GetID(s1)][GetID(s2)], w);
}
n = cnt.size();
dijkstra(s, t);
}
}