比以前写的好多了,也过了
#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<sstream>
#include<map>
#include<algorithm>
#include<unordered_map>
#include<regex>
using namespace std;
const int inf = 9999999;
unordered_map<string, int> str2int;
unordered_map<int, string> int2str;
int cnt = 1;
void getint(string s)
{
if (str2int[s] == 0) {
str2int[s] = cnt++;
int2str[str2int[s]] = s;
}
}
int happy[250];
int g[250][250];
int isok[250], dist[250];
vector <vector<int>> pre(250);
int st;
vector<int>tmpath, path;
int maxhap = 0, maxavghap = 0, dif = 0;
void dfs(int e,int hap)
{
if (e == st) {
dif++;
if (hap > maxhap) {
path.clear();
path = tmpath;
maxhap = hap;
maxavghap = maxhap /( path.size()-1);
}
else if (hap == maxhap) {
int tmpmaxavg = hap / (tmpath.size()-1);
if (tmpmaxavg > maxavghap) {
maxavghap = tmpmaxavg;
path.clear();
path = tmpath;
}
}
return;
}
for (int i = 0; i < pre[e].size(); ++i)
{
tmpath.push_back(pre[e][i]);
dfs(pre[e][i], hap + happy[pre[e][i]]);
tmpath.pop_back();
}
}
int main()
{
// freopen("test.txt", "r", stdin);
int n, k;
string start;
cin >> n >> k >> start;
getint(start);
for (int i = 1; i < n; ++i) {
string city;
int hap;
cin >> city >> hap;
getint(city);
happy[str2int[city]] = hap;
}
for (int i = 0; i < 250; ++i)
for (int j = 0; j < 250; ++j)
g[i][j] = inf;
for (int i = 0; i < k; ++i)
{
string s, e;
int cost;
cin >> s >> e >> cost;
int sint = str2int[s];
int eint = str2int[e];
g[sint][eint] = cost;
g[eint][sint] = cost;
}
int rom = str2int["ROM"];
st = str2int[start];
fill(dist, dist + 250, inf);
dist[st] = 0;
while (true)
{
int minindex = -1, mindist = inf;
for (int i = 1; i < cnt; ++i)
{
if (isok[i] == 0 && dist[i] < mindist)
{
minindex = i;
mindist = dist[i];
}
}
if (minindex == -1)break;
isok[minindex] = 1;
for (int i = 1; i < cnt; ++i)
{
if (isok[i] == 0)
{
if (mindist + g[minindex][i] < dist[i])
{
dist[i] = mindist + g[minindex][i];
pre[i].clear();
pre[i].push_back(minindex);
}
else if (mindist + g[minindex][i] == dist[i])
{
pre[i].push_back(minindex);
}
}
}
}
tmpath.push_back(rom);
dfs(rom,happy[rom]);
int cur = rom;
printf("%d %d %d %d\n", dif, dist[rom], maxhap, maxavghap);
cout << start;
for (int i = path.size()-2; i >=0; --i)
{
cout << "->" << int2str[path[i]];
}
return 0;
}