1003. Emergency (25)

来源:互联网 发布:java adobe 生成pdf 编辑:程序博客网 时间:2024/06/04 17:43

pat试题,网上都说简单,可能是我写代码的能力比较弱,写了一下午才写好。
用的C++面向对象方式,具体思路就是Djikstra最短路径+DFS。

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.InputEach input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.OutputFor each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.Sample Input5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output2 4

talk is cheap ,show me the code.

// 1003. Emergency.cpp : Defines the entry point for the console application.//#include <climits>#include<iostream>#include<vector>using namespace std;class City{public:  City(int CCityNo,     int CrescueCounts,      bool CisCollected = false,    unsigned int Cdist = UINT_MAX);  City();  ~City();  vector<int> preCityNO;//最优路径上一个城市  unsigned int dist;  bool isCollected;//是否在已访问集S中  int addSecureNum;  int CityNo(){ return cityNO; };  int rescueNo(){ return rescueCounts; };    void operator =(City Ccity){    cityNO = Ccity.cityNO;    rescueCounts = Ccity.rescueCounts;    preCityNO = Ccity.preCityNO;    dist = Ccity.dist;    isCollected = Ccity.isCollected;  }private:   int cityNO;//城市序号   int rescueCounts;//城市救援人数量};City::City(int CCityNo,   int CrescueCounts,   bool CisCollected/* = false*/,  unsigned int Cdist/* = UINT_MAX*/) :cityNO(CCityNo), rescueCounts(CrescueCounts){  isCollected = CisCollected;  dist = Cdist;  addSecureNum = CrescueCounts;}City::~City(){}class Map{public:  Map(int Csource, int CcityNum, int destID);  ~Map();    vector<vector<unsigned int>> Graph;  vector<City> CityList;    int pathNum;  int maxSecureNum;  City findMinstCity();  void Dijkstra();  void DFS(City start, int secureNum);private:  int cityNum;  int sourceID;  int destID;};Map::Map(int Csource, int CcityNum, int CdestID){  sourceID = Csource;  cityNum = CcityNum;  destID = CdestID;  pathNum = 0;  maxSecureNum = 0;  //初始化Graph  for (int i = 0; i < cityNum; i++){    vector<unsigned> tem;    for (int j = 0; j < cityNum; j++){      if (i == j)        tem.push_back(0);      else        tem.push_back(UINT_MAX);    }          Graph.push_back(tem);  }}Map::~Map(){}City Map::findMinstCity(){  unsigned int minDist = UINT_MAX;  City minCity(-1,0);  for (int i = 0; i < cityNum; i++)  {    if (!CityList[i].isCollected && CityList[i].dist < minDist){      minCity = CityList[i];      minDist = CityList[i].dist;    }  }  return minCity;}void Map:: Dijkstra(){  //收录源点  CityList[sourceID].isCollected = true;  CityList[sourceID].dist = 0;  while (true)  {    City minCity = findMinstCity();    if (minCity.CityNo() == -1)      break;    CityList[minCity.CityNo()].isCollected = true;    for (int i = 0; i < cityNum; i++)    {      if (!CityList[i].isCollected        && Graph[minCity.CityNo()][i] < UINT_MAX){        if (minCity.dist + Graph[minCity.CityNo()][i] < CityList[i].dist){          CityList[i].dist = minCity.dist + Graph[minCity.CityNo()][i];          CityList[i].preCityNO.clear();          CityList[i].preCityNO.push_back(minCity.CityNo());        }//if        else if (minCity.dist + Graph[minCity.CityNo()][i] == CityList[i].dist && minCity.CityNo()!=i)          CityList[i].preCityNO.push_back(minCity.CityNo());      }//if    }//for  }}void Map::DFS(City start, int LastSecureNum){  if (start.rescueNo() + LastSecureNum > start.addSecureNum)    start.addSecureNum = start.rescueNo() + LastSecureNum;  if (start.CityNo() == sourceID){    pathNum++;    if (start.addSecureNum > maxSecureNum)      maxSecureNum = start.addSecureNum;  }  for (int i = 0; i < start.preCityNO.size(); i++)  {    if (start.preCityNO[i] != start.CityNo())      DFS(CityList[start.preCityNO[i]], start.addSecureNum);  }}int main(){  int cityNum, pathNum, source, dest;  cin >> cityNum >> pathNum >> source >> dest;  Map map(source, cityNum, dest);  //存储城市,CityList  for (int i = 0; i < cityNum; i++)  {    int secure;    cin >> secure;    map.CityList.push_back(City(i, secure));  }  //存储图Graph  for (int i = 0; i < pathNum; i++)  {    int city1, city2;    unsigned int dist;    cin >> city1 >> city2 >> dist;    map.Graph[city1][city2] = dist;    map.Graph[city2][city1] = dist;  }  //更新city dist,preCity  for (int i = 0; i < cityNum; i++)  {    map.CityList[i].dist = map.Graph[source][i];    if (map.CityList[i].dist < UINT_MAX)      map.CityList[i].preCityNO.push_back(source);  }  //Dijkstra算法  map.Dijkstra();  //DFS算法  map.DFS(map.CityList[dest], 0);  cout << map.pathNum << " " << map.maxSecureNum;  return 0;}


0 0