和L2-001几乎一模一样。。。套模板完事
#include <iostream>
#include <cstring>
#include <unordered_map>
#include <vector>
using namespace std;
const int N = 210;
int n, m, S, T;
string s, t;
int e[N][N], dist[N], cnt[N];
int total[N]; // 总城市数
int w[N], sum[N];
int pre[N];
bool st[N];
string city[N];
unordered_map<string, int> mp;
void dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[S] = 0, sum[S] = w[S], total[S] = 1, cnt[S] = 1;
for(int i = 0; i < n - 1; i++)
{
int t = -1;
for(int j = 1; j <= n; j++)
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
st[t] = true;
for(int j = 1; j <= n; j++)
{
if(dist[j] > dist[t] + e[t][j])
{
dist[j] = dist[t] + e[t][j];
cnt[j] = cnt[t];
total[j] = total[t] + 1;
sum[j] = sum[t] + w[j];
pre[j] = t;
}
else if(dist[j] == dist[t] + e[t][j])
{
cnt[j] = cnt[j] + cnt[t];
if(total[j] < total[t] + 1)
{
total[j] = total[t] + 1;
sum[j] = sum[t] + w[j];
pre[j] = t;
}
else if(total[j] == total[t] + 1)
{
if(sum[j] < sum[t] + w[j])
{
sum[j] = sum[t] + w[j];
pre[j] = t;
}
}
}
}
}
}
int main()
{
memset(e, 0x3f, sizeof e);
cin >> n >> m >> s >> t;
mp[s] = 1;
city[1] = s;
for(int i = 2; i <= n; i++)
{
string c;
int x;
cin >> c >> x;
mp[c] = i;
city[i] = c;
w[i] = x;
}
S = mp[s] , T = mp[t];
while(m --)
{
string ca, cb;
int x;
cin >> ca >> cb >> x;
int a = mp[ca], b = mp[cb];
e[a][b] = e[b][a] = x;
}
dijkstra();
vector<int> ans;
for(int i = T; i != 1; i = pre[i]) ans.push_back(i);
cout << s;
for(int i = ans.size() - 1; i >= 0; i--) cout << "->" << city[ans[i]];
cout << endl << cnt[T] << ' ' << dist[T] << ' ' << sum[T] << endl;
}