题目
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984
AC代码
#include<bits/stdc++.h>
using namespace std;
const int inf=999999;
int n,k;
map<string,int> m;
map<int,string> m1;
int e[201][201],weight[201],dis[201],vis[201];
vector<int> pre[201],path,tmppath;
int cntpath=0,maxvalue=0;
double maxavg;
void dfs(int root)
{
tmppath.push_back(root);
if(root==1)
{
int value=0;
for(int i=0;i<tmppath.size();i++)
{
value+=weight[tmppath[i]];
}
double tmpavg=1.0*value/(tmppath.size()-1);
if(value>maxvalue)
{
maxvalue=value;
maxavg=tmpavg;
path=tmppath;
}
else if(value==maxvalue&&tmpavg>maxavg)
{
maxavg=tmpavg;
path=tmppath;
}
tmppath.pop_back();
cntpath++;
return;
}
for(int i=0;i<pre[root].size();i++)
dfs(pre[root][i]);
tmppath.pop_back();
}
int main()
{
string s;
cin>>n>>k>>s;
m[s]=1;
m1[1]=s;
for(int i=1;i<n;i++)
{
cin>>s>>weight[i+1];
m[s]=i+1;
m1[i+1]=s;
}
memset(e,inf,sizeof(e));
memset(dis,inf,sizeof(dis));
string s1,s2;
int t;
for(int i=0;i<k;i++)
{
cin>>s1>>s2>>t;
e[m[s1]][m[s2]]=e[m[s2]][m[s1]]=t;
}
dis[1]=0;
for(int i=1;i<=n;i++)
{
int u=-1,minn=inf;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
if(u==-1) break;
vis[u]=1;
for(int j=1;j<=n;j++)
{
if(!vis[j])
{
if(dis[u]+e[u][j]<dis[j])
{
dis[j]=dis[u]+e[u][j];
pre[j].clear();
pre[j].push_back(u);
}
else if(dis[u]+e[u][j]==dis[j])
{
pre[j].push_back(u);
}
}
}
}
int root=m["ROM"];
dfs(root);
printf("%d %d %d %d\n",cntpath,dis[root],maxvalue,(int)maxavg);
for(int i=path.size()-1;i>=1;i--)
cout<<m1[path[i]]<<"->";
cout<<"ROM";
}