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
题目大意:
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<string>
using namespace std;
struct node1
{
int cost;
string str;
};
struct node
{
int happiness;
vector<struct node1> v;
};
map<string,struct node> Map;
map<string,int> visited;
int num,anscost=1<<30,cost=0,anshappiness=0,happiness=0,ansaverage=0;
vector<string> ansvec;
vector<string> vec;
void DFS(string start)
{
if(cost>anscost)
return ;
if(start=="ROM")
{
//printf("%d\n",anshappiness);
if(cost<anscost)
{
ansvec=vec;
anscost=cost;
anshappiness=happiness;
num=1;
}
else if(cost==anscost)
{
if(happiness>anshappiness)
{
anshappiness=happiness;
ansvec=vec;
}
else if(happiness==anshappiness)
{
if(happiness/(vec.size()-1)>anshappiness/(ansvec.size()-1))
{
anshappiness=happiness;
ansvec=vec;
}
}
num++;
}
return ;
}
for(int i=0;i<Map[start].v.size();i++)
{
if(visited[Map[start].v[i].str]==0)
{
visited[Map[start].v[i].str]=1;
cost+=Map[start].v[i].cost;
happiness+=Map[Map[start].v[i].str].happiness;
vec.push_back(Map[start].v[i].str);
DFS(Map[start].v[i].str);
vec.pop_back();
visited[Map[start].v[i].str]=0;
cost-=Map[start].v[i].cost;
happiness-=Map[Map[start].v[i].str].happiness;
}
}
}
int main()
{
int i,j,n,m,k,t;
string s,a,b;
scanf("%d %d",&n,&m);
cin>>s;
visited[s]=1;
for(i=1;i<n;i++)
{
cin>>a>>k;
Map[a].happiness=k;
visited[a]=0;
}
for(i=0;i<m;i++)
{
cin>>a>>b>>t;
struct node1 tmp;
tmp.str=b;
tmp.cost=t;
Map[a].v.push_back(tmp);
tmp.str=a;
Map[b].v.push_back(tmp);
}
vec.push_back(s);
DFS(s);
printf("%d %d %d %d\n",num,anscost,anshappiness,anshappiness/(ansvec.size()-1));
for(i=0;i<ansvec.size();i++)
{
if(i==0)
{
cout<<ansvec[i];
}
else
{
cout<<"->"<<ansvec[i];
}
}
return 0;
}