https://www.patest.cn/contests/pat-a-practise/1087
1087. All Roads Lead to Rome (30)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers N (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K lines follow, each describes a route between two cities in the format "City1 City2 Cost". Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.
Output Specification:
For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommended. If such a route is still not unique, then we output the one with the maximum average happiness -- it is guaranteed by the judge that such a solution exists and is unique.
Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommended route. Then in the next line, you are supposed to print the route in the format "City1->City2->...->ROM".
Sample Input:6 7 HZH ROM 100 PKN 40 GDN 55 PRS 95 BLN 80 ROM GDN 1 BLN ROM 1 HZH PKN 1 PRS ROM 2 BLN HZH 2 PKN GDN 1 HZH PRS 1Sample Output:
3 3 195 97 HZH->PRS->ROM
思路:dijkstra算法硬刚↓
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<iostream>
using namespace std;
const int inf = 999999999;
int N,K;//N个城市,K条路径
map<string,int>city_num;//将城市名转换为城市编号
map<int,string>num_city;//将城市编号转换为城市名
bool book[222]={false};
int dis[222],we[222],num[222],pot[222],pre[222];//最短距离,最大点权,最短路径数目,最短路径的顶点个数,前驱点
int Ig[222][222];//无向图
int hp[222];//幸福值
string start,temp1,temp2;
int ed;
void dijkstra(int st)
{
fill(dis,dis+222,inf);
fill(we,we+222,0);
fill(num,num+222,0);
fill(pot,pot+222,0);
for(int i=0;i<N;i++)
{
pre[i]=i;
}
dis[st]=0;
we[st]=hp[st];
num[st]=1;
for(int i=0;i<N;i++)
{
int u=-1,min=inf;
for(int j=0;j<N;j++)
{
if(book[j]==false&&dis[j]<min)
{
u=j;
min=dis[j];
}
}
if(u==-1) return;
book[u]=true;
for(int v=0;v<N;v++)
{
if(book[v]==false&&Ig[u][v]!=inf)
{
if(dis[u]+Ig[u][v]<dis[v])
{
dis[v]=dis[u]+Ig[u][v];
we[v]=we[u]+hp[v];
num[v]=num[u];
pot[v]=pot[u]+1;
pre[v]=u;
}
else if(dis[u]+Ig[u][v]==dis[v])//相同长度的路径
{
num[v]+=num[u];
if(we[u]+hp[v]>we[v])
{
we[v]=we[u]+hp[v];
pot[v]=pot[u]+1;
pre[v]=u;
}
else if(we[u]+hp[v]==we[v])
{
double uu=(we[u]+hp[v])*1.0/(pot[u]+1);
double vv=we[v]*1.0/pot[v];
if(uu>vv)
{
pot[v]=pot[u]+1;
pre[v]=u;
}
}
}
}
}
}
}
void showpath(int x)//打印路径
{
if(x==0)
{
cout<<num_city[x];
return;
}
showpath(pre[x]);
cout<<"->"<<num_city[x];
}
int main()
{
cin>>N>>K>>start;
city_num[start]=0;
num_city[0]=start;//起点城市编号为0
int temp;
for(int i=1;i<=N-1;i++)
{
cin>>temp1>>temp;
city_num[temp1]=i;
num_city[i]=temp1;
hp[i]=temp;
}
fill(Ig[0],Ig[0]+222*222,inf);
for(int i=0;i<K;i++)
{
cin>>temp1>>temp2>>temp;
int num1,num2;
num1=city_num[temp1];
num2=city_num[temp2];
Ig[num1][num2]=temp;
Ig[num2][num1]=temp;//无向图两边都要连0w0
}
dijkstra(0);
string em="ROM";
ed=city_num[em];
printf("%d %d %d %d\n",num[ed],dis[ed],we[ed],we[ed]/pot[ed]);
showpath(ed);
return 0;
}