题目描述
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 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个城市,k条路径,源结点的城市名;每个城市给定城市名和happy值;每条路径给出源节点的城市名、目的结点的城市名以及两者之间的cost。要求第一行输出满足条件的从源节点到目的结点的最短路径条数、最短路径值、该路径的happy总值,以及平均happy值;第二行输出满足条件的路径值。
- 满足条件的路径:首先选择最小耗费的路径,如果耗费相等,则选择happy总值最高的路径;如果happy总值仍然相等,则选择happy平均值最高的路径。
- 解题思路:用Dijkstra算法,首先将源节点放入队列中,每次都选择队列中dist值最大的弹出(因为加了一个-放入优先队列中)进行判断更新。如果弹出的结点已经遍历过,则跳过。
代码实现(AC)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#define INF 1e9
using namespace std;
const int maxc=210;
int n,k;
string start;
int goal;
map<string,int>CitytoNum;
map<int,string>NumtoCity;
int vis[maxc],dis[maxc],path[maxc];
int happy[maxc],happySum[maxc],numCity[maxc];
int route[maxc];
vector<pair<int,int>>edge[maxc];
void init()
{
cin>>n>>k>>start;
CitytoNum[start]=0;
NumtoCity[0]=start;
happy[0]=0;
for(int i=1;i<n;i++)
{
string s;int tmp;
cin>>s>>tmp;
if(s=="ROM") goal=i;
happy[i]=tmp;
CitytoNum[s]=i;
NumtoCity[i]=s;
}
for(int i=0;i<k;i++)
{
string c1,c2;
int w;
cin>>c1>>c2>>w;
edge[CitytoNum[c1]].push_back({CitytoNum[c2],w});
edge[CitytoNum[c2]].push_back({CitytoNum[c1],w});
}
}
void Dijkstra(int src,int dst)
{
fill(dis,dis+maxc,INF);
dis[src]=0;
priority_queue<pair<int,int> >q;
q.push({0,src});
numCity[src]=0;
happySum[src]=happy[src];
route[src]=1;
while(!q.empty())
{
int u=q.top().second;
q.pop();
if(vis[u]==1) continue; //已被访问过,则跳过该结点
vis[u]=1;
for(int i=0;i<edge[u].size();i++)
{
int v=edge[u][i].first;
int w=edge[u][i].second;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
route[v]=route[u];
numCity[v]=numCity[u]+1;
happySum[v]=happySum[u]+happy[v];
path[v]=u;
if(vis[v]==0) q.push({-dis[v],v});//如果该结点未被访问过,则需要加入待访问序列
}
else if(dis[v]==dis[u]+w)
{
route[v]+=route[u];
if(happySum[v]<happySum[u]+happy[v])
{
path[v]=u;
happySum[v]=happySum[u]+happy[v];
numCity[v]=numCity[u]+1;
if(vis[v]==0) q.push({-dis[v],v});
}
else if(happySum[v]==happySum[u]+happy[v]&&numCity[v]>numCity[u]+1)
{
path[v]=u;
numCity[v]=numCity[u]+1;
if(vis[v]==0) q.push({-dis[v],v});
}
}
}
}
cout<<route[dst]<<" "<<dis[dst]<<" "<<happySum[dst]<<" "<<happySum[dst]/numCity[dst]<<endl;
}
void getPath(int src,int dst)
{
stack<int>pre;
pre.push(dst);
dst=path[dst];
while(dst!=src)
{
pre.push(dst);
dst=path[dst];
}
cout<<NumtoCity[src];
while(!pre.empty())
{
cout<<"->"<<NumtoCity[pre.top()];
pre.pop();
}
}
int main()
{
init();
Dijkstra(0,goal);
getPath(0,goal);
return 0;
}