AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
using namespace std;
const int N = 210;
int a[N];
int n, m;
string st, ed;
map <string,int> mp;
map <int, string> mp2;
int enemy[N], g[N][N], dis[N], city[N], kill[N], vis[N], num[N];
int pre[N];
vector <int> path;
void dijkstra ()
{
memset (dis, 0x3f, sizeof dis);
dis[mp[st]] = 0;
num[mp[st]] = 1;
for (int i = 1; i <= n; i ++)
{
int t = -1;
for (int j = 1; j <= n; j ++)
{
if (!vis[j] && (t == -1 || dis[j] < dis[t]))
t = j;
}
if (i == 1) t = mp[st];
vis[t] = 1;
for (int j = 1; j <= n; j ++)
{
if (dis[j] > dis[t] + g[t][j])
{
num[j] = num[t];
dis[j] = dis[t] + g[t][j];
kill[j] = kill[t] + enemy[j];
city[j] = city[t] + 1;
pre[j] = t;
}
else if (dis[j] == dis[t] + g[t][j])
{
num[j] += num[t];
if (city[j] < city[t] + 1)
{
city[j] = city[t] + 1;
kill[j] = kill[t] + enemy[j];
pre[j] = t;
}
else if (city[j] == city[t] + 1)
{
if (kill[j] < kill[t] + enemy[j])
{
kill[j] = kill[t] + enemy[j];
pre[j] = t;
}
}
}
}
}
}
int main ()
{
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
cin >> n >> m >> st >> ed;
int cnt = 1;
mp2[cnt] = st;
mp[st] = cnt ++;
for (int i = 1; i < n; i ++)
{
string s; cin >> s;
mp2[cnt] = s;
mp[s] = cnt ++;
int x; cin >> x;
enemy[mp[s]] = x;
}
memset (g, 0x3f, sizeof g);
while (m --)
{
string s1, s2; int x;
cin >> s1 >> s2 >> x;
int a = mp[s1], b = mp[s2];
g[a][b] = g[b][a] = x;
}
dijkstra ();
int a = mp[ed];
while (a != mp[st])
{
path.push_back(a);
a = pre[a];
}
path.push_back(mp[st]);
reverse (path.begin(), path.end ());
for (int i = 0; i < (int)path.size(); i ++)
{
if (i == 0)
cout << mp2[path[i]];
else
cout << "->" << mp2[path[i]];
}
cout << "\n";
cout << num[mp[ed]] << " " << dis[mp[ed]] << " " << kill[mp[ed]];
return 0;
}