solution Of 1087. All Roads Lead to Rome (30)

来源:互联网 发布:java date日期格式化 编辑:程序博客网 时间:2024/06/14 13:36

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 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 1
Sample Output:
3 3 195 97
HZH->PRS->ROM


结题思路 :
题意要求我们找到一条从起点通往终点的最短路。
要求1:DIJ找到最短的路径,需要将娱乐值和平均娱乐值考虑在内;
要求2:需要记录每次内次经过的节点之前经过了几个节点;
要点3:需要记录到达当前点的路径长度总和。

程序步骤:
第一步、建立无向图;
第二步、通过DIJ查找的方式进行寻路;
第三步、当前路径长度更短,变更父节点;长度相同,判断娱乐总值,来进行父节点的变更;若娱乐总值相同,判断平均娱乐值来进行父节点的变更。
第四步、从终点开始逆向存储经过节点,最后再逆向输出即可。

具体程序(AC)如下:

#include <iostream>#include <map>#include <vector>#include <string>#include <cstring>#include <stack>#define inf 0x3fffffff#define len 205using namespace std;struct node{  int end;  int dis;};string city[len];//索引转城市名的映射map<string,int> city2No;//城市名转索引的映射vector<int> parent;//父节点的保存vector<vector<node> >road;//无向图int happiness[len];//每个城市的娱乐值int finalHappiness[len];//每个城市最终的娱乐值int roadCount[len];//到达当前城市可行路径总和int dist[len];//到达当前城市经过的距离int deep[len];//到达当前城市经过的城市总和void dijkstra(vector<vector<node> > &road,vector<int>&parent,int start,int end){  bool visited[len];  int i,j;  memset(visited,0,sizeof(visited));  memset(finalHappiness,0,sizeof(finalHappiness));  parent.resize(road.size());  for(i=0;i<road.size();++i)    dist[i]=inf;  dist[start]=0;  deep[start]=1;  roadCount[start]=1;  parent[start]=-1;  int min;  while(1)  {    min=inf;    for(i=0;i<road.size();++i)      if(!visited[i]&&min>dist[i])      {        min=dist[i];        start=i;      }    if(min==inf)break;    visited[start]=true;    if(start==end)//查找结束      return;    int endC;    for(i=0;i<road[start].size();++i)    {      endC=road[start][i].end;      if(!visited[endC])      {        if(dist[endC]>road[start][i].dis+dist[start])        {          roadCount[endC]=roadCount[start];          parent[endC]=start;          dist[endC]=road[start][i].dis+dist[start];          deep[endC]=deep[start]+1;          finalHappiness[endC]=finalHappiness[start]+happiness[endC];        }        else if(dist[endC]==road[start][i].dis+dist[start])        {          roadCount[endC]+=roadCount[start];          if(finalHappiness[endC]<finalHappiness[start]+happiness[endC])          {            parent[endC]=start;            deep[endC]=deep[start]+1;            finalHappiness[endC]=finalHappiness[start]+happiness[endC];          }          else if(finalHappiness[endC]==finalHappiness[start]+happiness[endC]&&deep[endC]>deep[start]+1)//平均娱乐值          {             deep[endC]=deep[start]+1;             parent[endC]=start;          }        }      }    }  }}int main() {  // your code goes here  int numOfC,numOfR;  cin>>numOfC>>numOfR;  cin>>city[0];  city2No[city[0]]=0;  happiness[0]=0;  int i;  for(i=1;i<numOfC;++i)  {    cin>>city[i];    city2No[city[i]]=i;    cin>>happiness[i];  }  road.clear();  road.resize(numOfC);  string start,end;  int s;  for(i=0;i<numOfR;++i)//无向图中放入两条路径  {    cin>>start>>end;    s=city2No[start];    node t;    t.end=city2No[end];    cin>>t.dis;    road[s].push_back(t);    int ex;    ex=t.end;    t.end=s;    s=ex;    road[s].push_back(t);  }  //建图结束  int endOfC=city2No["ROM"];  dijkstra(road,parent,0,endOfC);  cout<<roadCount[endOfC]<<" "<<dist[endOfC]<<" "<<finalHappiness[endOfC]<<" "<<finalHappiness[endOfC]/(deep[endOfC]-1)<<endl;  cout<<city[0];  vector<int> path;  path.clear();  int curC=endOfC;  while(curC!=-1)  {    path.push_back(curC);    curC=parent[curC];  }  for(i=path.size()-2;i>=0;--i)    cout<<"->"<<city[path[i]];  cout<<endl;  return 0;}
0 0
原创粉丝点击