1087. All Roads Lead to Rome (30)

102 篇文章 0 订阅
37 篇文章 0 订阅

1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
这题就是经常用来30分的,我用的是DFS的。好几题DFS都是这个思路,我写的截枝基本上都在循环里面的if。
输入:
【N(有N个城市包括出发点)  K(城市间的道路)  start(出发点)】
接着N-1行不包括出发点的【城市  和该城市的属性加成】
……
k行【城市1  城市2 距离】
……
输出:
距离最短的路有几条countnum    最短的距离是多少mincost  全部属性加成是多少maxhappy   maxhappy/(选择的路中的城市  减去 一个出发点)
从出发点->……->ROM  

我通过输入给出发点编号0,其他城市的编号根据N-1行的顺序编号(1-N-1);
  map < string, int> cities;标记(城市名,城市编号)这个map可以通过string获取index;在这里就用来找ROM用,因为出发点我就设为0了 
  vector<pair<string, int>>happiness(N);标记(城市名,属性加成)这里可以通过vector的下标,输出时获得城市名,DFS时获得属性加成
  vector<vector<pair<int, int>>>roads(N);第一个vector的下标为某个城市,第二个vector包含全部与这个城市有连通的路到的pair<与另一个城市,距离>(PS:即数组邻接表)


DFS就是把全部的路走到没有路或者城市都走过了,然后看看当前的才城市是不是到ROM是的话,根据要求
    这条路是不是最短,是用当前的这条路countnum=1,属性加成和最短距离更新;
        否则和原来的路相等,那么我们就要coutnum++;
                          并开始判断,当前这条路获得的全部属性加成是不是最大,是覆盖原来的路,更新属性加成;
                                      否则,看看平均个每个路过的城市属性加成是不是最大,是覆盖;
                                              没有否则了,题目说上面的一定会唯一的。

 评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月18日 15:00答案正确301087C++ (g++ 4.7.2)2436datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确130815/15
1答案正确14363/3
2答案正确13085/5
3答案正确13083/3
4答案正确23084/4
#include<iostream>
#include<string>
#include<map>
#include<vector>   
using namespace std;
void readln(map<string, int>*cities, int N, int K, vector<vector<pair<int, int>>>*roads, vector<pair<string, int>>*happiness)
{
  int index;
  string str, strr;
  int happy; 
  for (index = 1; index < N; index++)
  {
    cin >> str >> happy;
    (*cities)[str] = index;
    (*happiness)[index] = make_pair(str, happy);
  }
  for (index = 0; index < K; index++)
  {
    cin >> str >> strr >> happy;
    (*roads)[(*cities)[str]].push_back(make_pair((*cities)[strr], happy));
    (*roads)[(*cities)[strr]].push_back(make_pair((*cities)[str], happy));
  }
}
void DFSROme(vector<pair<string, int>>*happiness, vector<bool>*visit, map<string, int>*cities, vector<vector<pair<int, int>>>*roads, int startindex, int endindex,
  vector<int>*route, vector<int>*routetemp, int* mincost, int *maxhappy, int*countnum, int sumcost, int sumhappy)
{
  (*visit)[startindex] = true;
  for (vector<pair<int, int>>::iterator iter = (*roads)[startindex].begin(); iter != (*roads)[startindex].end(); iter++)
  {
    (*routetemp).push_back(iter->first);
    if (!(*visit)[iter->first] && (*roads)[iter->first].size() > 0 && iter->first != endindex)
      DFSROme(happiness, visit, cities, roads, iter->first, endindex,
      route, routetemp, mincost, maxhappy, countnum, sumcost + iter->second, sumhappy + (*happiness)[iter->first].second);
    else if (iter->first == endindex)
    {
      if (0 == (*countnum) ||(*countnum)>0&& (*mincost) > sumcost + iter->second)
      {
        (*countnum) = 1;
        (*mincost) = sumcost + iter->second;
        (*maxhappy) = sumhappy + (*happiness)[iter->first].second;/*忘了这个,答案错误!!!*/
        (*route) = (*routetemp);
      }
      else if ((*mincost) == sumcost + iter->second)
      {
        (*countnum)++;/*统计相等花费最短的*/
        if (sumhappy + (*happiness)[iter->first].second > (*maxhappy) ||
          sumhappy + (*happiness)[iter->first].second == (*maxhappy) && route->size() > routetemp->size())
        {
          (*maxhappy) = sumhappy + (*happiness)[iter->first].second;
          (*route) = (*routetemp);
        }
      }
    }
    (*routetemp).pop_back();
  }

  (*visit)[startindex] = false;
}
int main()
{
  int N, K, mincost, maxhappy, countnum;
  string start;
  cin >> N >> K >> start;
  map < string, int> cities; 
  vector<pair<string, int>>happiness(N);
  vector<vector<pair<int, int>>>roads(N);
  cities[start] = 0;
  happiness[0] = make_pair(start, 0);
  readln(&cities, N, K, &roads, &happiness);
  vector<int>route;
  vector<bool>visit(N, false);
  vector<int>routetemp;
  routetemp.push_back(cities[start]);
  countnum = 0;
  DFSROme(&happiness, &visit, &cities, &roads, cities[start], cities["ROM"],
    &route, &routetemp, &mincost, &maxhappy, &countnum, 0, 0);
  cout << countnum << " " << mincost << " " << maxhappy << " " << maxhappy / (route.size() - 1) << endl;
  cout << happiness[route.front()].first;
  for (vector<int>::iterator iter = route.begin() + 1; iter != route.end(); iter++)
    cout << "->" << happiness[(*iter)].first;
  cout << endl;
  system("pause");
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值