1087. All Roads Lead to Rome (30)

来源:互联网 发布:淘宝拍卖房产过户 编辑:程序博客网 时间:2024/05/22 09:47

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 HZHROM 100PKN 40GDN 55PRS 95BLN 80ROM GDN 1BLN ROM 1HZH PKN 1PRS ROM 2BLN HZH 2PKN GDN 1HZH PRS 1
Sample Output:
3 3 195 97HZH->PRS->ROM

还是求最短路径的问题,而且按照以往经验猜也能猜到最短路径不唯一,要使得路径的节点的某数据的总和最大或最小什么的。还是一样的套路,用dijkstra算法算出最短路径长度,然后用dfs+剪枝,求出等于最短路径长度的长度,然后通过比较选出城市幸福指数总和最大的,如果最大的不唯一,选择平均幸福指数最大的,也就是选择路径中节点数最少的。这里给出的节点的键值是字符串,为了方便使用dijkstra算法和dfs,要用map把城市名转化成数字键值,还要用vector储存数字对应的城市名,然后输出的时候就可以根据数字输出城市名。


代码:

#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>#include <map>using namespace std;int n,m;const int INF=1<<30;vector<int>happiness;vector<vector<int> >road;int dijkstra(int des){vector<int>cost(n);for(int i=0;i<n;i++) cost[i]=road[0][i];vector<bool>isvis(n,false);while(true){int index,Min=INF;for(int i=1;i<n;i++){if(!isvis[i]&&cost[i]<Min){Min=cost[i];index=i;}}if(index==des){return Min;}isvis[index]=true;for(int i=1;i<n;i++){if(!isvis[i]&&road[index][i]<INF&&cost[index]+road[index][i]<cost[i]){cost[i]=cost[index]+road[index][i];}}}}int count=0,max_sum=0;vector<int>res;void dfs(int cur,int des,int cost,int min_cost,int sum,vector<bool>isvis,vector<int>path){if(cost>min_cost) return;if(cur==des){if(cost==min_cost){count++;if(sum>max_sum){max_sum=sum;res=path;}else if(sum==max_sum&&path.size()<res.size()){res=path;}}}isvis[cur]=true;for(int i=1;i<n;i++){if(!isvis[i]&&road[cur][i]<INF){path.push_back(i);dfs(i,des,cost+road[cur][i],min_cost,sum+happiness[i],isvis,path);path.pop_back();}}}int main(){cin>>n>>m;happiness.resize(n);road.resize(n,vector<int>(n,INF));string src;cin>>src;map<string,int>str2int;vector<string>city(n);str2int[src]=0;int des;for(int i=1;i<n;i++) {string s;cin>>s;if(s=="ROM") des=i;str2int[s]=i;cin>>happiness[i];city[i]=s;}for(int i=0;i<m;i++){string a,b;int dis;cin>>a>>b>>dis;int x=str2int[a],y=str2int[b];road[x][y]=road[y][x]=dis;}int min_cost=dijkstra(des);//cout<<min_cost;vector<bool>isvis(n,false);vector<int>path;dfs(0,des,0,min_cost,0,isvis,path);printf("%d %d %d %d\n",count,min_cost,max_sum,max_sum/res.size());cout<<src;for(int i=0;i<res.size();i++){cout<<"->"<<city[res[i]];}}


0 0
原创粉丝点击