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 recommanded. 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 recommanded 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 1
Sample Output:
3 3 195 97
HZH->PRS->ROM
题意:给定n个点,m条边,和起点sx,每个点有一个快乐值,每条边有一个花费值,要求你输出两行,第一行是到终点{最少花费路径条数,最少花费,快乐值,平均快乐值(不算起点)},第二行输出路径。
优先选择花费少的路径,如果相同优先选择快乐值高的,否则选择平均快乐值高的。
解析:因为输入是字符串,因此需要map来映射,然后最短路跑一边即可,记录一个路径path,跑最短路时记录下这个点的前置点是谁,最后从终点返回起点,最后反转一下就是路径,对于最短路更新详细可以参考代码。
测试点2是针对路径条数
#include <bits/stdc++.h>
using namespace std;
const int N=205;
typedef pair<int,int> PII;
int n,m;
int w[N],cost[N],hap[N],pre[N],point[N],road[N];
//每个点的花费,到该点所需总花费,到该点的快乐值,该点的前置点,到该点经过了几个点,到该点有几条路径选择
string sx;//
map<string,int> id;//每个地点名对应的数字ID
map<int,string> name;//每个ID对应的地点名
vector<PII> v[N];
bool vis[N];//标记该点是否访问
void spfa()
{
queue<int> q;
q.push(id[sx]);
//初始化
memset(cost,0x3f,sizeof cost);
memset(point,0x3f,sizeof point);
cost[id[sx]]=0;
road[id[sx]]=1;
point[id[sx]]=0;
while(q.size())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=0;i<v[u].size();i++)
{
int j=v[u][i].first;//邻点
int k=v[u][i].second;//所需花费
if(cost[j]>cost[u]+k)
{
point[j]=point[u]+1;//经过点数+1
cost[j]=cost[u]+k;
hap[j]=hap[u]+w[j];
road[j]=road[u];//继承路径条数
pre[j]=u;//记录前置点
if(!vis[j])
{
vis[j]=true;
q.push(j);
}
}else if(cost[j]==cost[u]+k)//相同花费
{
road[j]+=road[u];//注意是+road[u]而不是+1,这个是针对测试点2的
if(hap[j]<hap[u]+w[j])
{
hap[j]=hap[u]+w[j];
point[j]=point[u]+1;
pre[j]=u;
if(!vis[j])
{
vis[j]=true;
q.push(j);
}
}else//相同快乐值
{
if(point[j]>point[u]+1)//优先选择经过点数少的
{
point[j]=point[u]+1;
pre[j]=u;
if(!vis[j])
{
vis[j]=true;
q.push(j);
}
}
}
}
}
}
}
void solve()
{
cin>>n>>m>>sx;
int cnt=1;//记录映射数
id[sx]=1;
name[1]=sx;
for(int i=1;i<n;i++)
{
string k;
cin>>k;
if(!id[k]) id[k]=++cnt,name[cnt]=k;
cin>>w[id[k]];
}
for(int i=1;i<=m;i++)
{
string x,y;
int c;
cin>>x>>y>>c;
int a=id[x],b=id[y];
v[a].push_back({b,c});
v[b].push_back({a,c});
}
spfa();
int y=id["ROM"],pos=y;//终点
vector<int> path;//保存路径方案
while(pos!=id[sx])
{
path.push_back(pos);
pos=pre[pos];
}
path.push_back(id[sx]);//起点
printf("%d %d %d %d\n",road[y],cost[y],hap[y],hap[y]/((int)path.size()-1));
reverse(path.begin(), path.end());//反转
for(int i=0;i<path.size();i++)
{
if(i!=0) printf("->");
cout<<name[path[i]];
}
printf("\n");
}
int main()
{
int t=1;
//scanf("%d",&t);
while(t--) solve();
return 0;
}