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
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984
题目大意:有n个城市,m条路,每个城市具有幸福值,给定一个城市s,要求找到一条从s到ROM(罗马)的最短路线,如果最短路线有多条,那么则找沿线城市幸福值加起来最大的一条路线,如果这样还有多条路线,那么则找沿线城市平均幸福值最大的的一条路线。
要求输出最短路径的数目(最短长度相同的路线的数目),最短路径的长度,最大幸福值,最大平均幸福值
思路:Dijkstra+dfs。不能只使用Dijkstra,因为平均幸福值需要知道从s到ROM沿线城市有几个,Dijkstra是基于贪心的,对于本题局部最优无法保证全局最优。
AC代码:
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int e[210][210]; //存边
bool vis[210]; //标记数组
int dis[210]; //记录距离
int Hap[210]; //记录每个城市的
bool book[210]; //dfs标记数组
unordered_map<string,int>cityId; //存城市名与id的映射
unordered_map<int,string>cityName; //存id与城市名的映射
vector<int>mp[210],res,temp; //存最短路地图,最优路线,临时路线
int n,k;
string s;
void Dijkstra()
{
int start = cityId[s];
memset(dis,inf,sizeof(dis));
dis[start]=0;
for(int i=1;i<=n;i++)
{
int minn=inf,u=-1;
for(int j = 1; j <= n; j++)
{
if(!vis[j]&&dis[j]<minn)
{
minn=dis[j];
u=j;
}
}
if(u==-1)
break;
vis[u]=true;
for(int j = 1; j <=n ;j++)
{
if(!vis[j]&&dis[j]>dis[u]+e[u][j])
{
dis[j]=dis[u]+e[u][j];
mp[j].clear();
mp[j].push_back(u);
}
else if(!vis[j]&&dis[j]==dis[u]+e[u][j])
{
mp[j].push_back(u);
}
}
}
}
int maxHap;
int maxAvgHap;
int num;
void dfs(int i)
{
temp.push_back(i);
if(i==cityId[s])
{
num++;
int hap=0;
int avghap=0;
for(int i = 0; i < temp.size()-1; i++)
{
int id = temp[i];
hap+=Hap[id];
}
avghap=hap*1.0/(temp.size()-1);
if(hap>maxHap)
{
res=temp;
maxHap=hap;
maxAvgHap=avghap;
}
else if(hap==maxHap&&avghap>maxAvgHap)
{
res=temp;
maxAvgHap=avghap;
}
temp.pop_back();
return ;
}
for(int j=0;j<mp[i].size();j++)
{
if(!book[mp[i][j]])
{
book[mp[i][j]]=1;
int x= mp[i][j];
dfs(x);
book[mp[i][j]]=0;
}
}
temp.pop_back();
}
int main()
{
string t;
cin>>n>>k>>s;
cityId[s]=n;
cityName[n]=s;
for(int i=1;i<=n-1;i++)
{
int x;
cin>>t>>x;
Hap[i]=x;
cityId[t]=i;
cityName[i]=t;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)
e[i][j]=0;
else
e[i][j]=inf;
}
}
int w;
string u,v;
for(int i=1;i<=k;i++)
{
cin>>u>>v>>w;
e[cityId[u]][cityId[v]]=e[cityId[v]][cityId[u]]=w;
}
Dijkstra();
//打印路径
/*for(int i=1;i<=n;i++)
{
cout<<cityName[i]<<": ";
for(int j = 0; j < mp[i].size();j++)
{
cout<<cityName[mp[i][j]]<<" ";
}
cout<<endl;
}*/
int id=cityId["ROM"];
dfs(id);
int maxAvgg=maxAvgHap;
printf("%d %d %d %d\n",num,dis[cityId["ROM"]],maxHap,maxAvgg);
cout<<s;
for(int i = res.size()-2; i>=0;i--)
{
cout<<"->"<<cityName[res[i]];
}
}
博客围绕从城市到罗马的旅游路线问题展开。给定城市数量、路线数量、各城市幸福值等信息,要求找到成本最低且幸福值最高的路线。若有多条最短路线,依次比较总幸福值和平均幸福值。解题思路为Dijkstra+dfs,还给出了题目链接和AC代码。
2万+

被折叠的 条评论
为什么被折叠?



