PAT-A1087 All Roads Lead to Rome 题目内容及题解

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 recommanded. 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 recommanded 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. 使用结构体储存路径;
  2. 初始化并读入图的信息;
  3. 用Dijkstra算法找到最短路径,期间每个节点的前驱(找到到达终点的最短路径即可退出);
  4. 用DFS获取路径,并将其存储;
  5. 按照题目要求找到路径;
  6. 输出结果并返回零值。

代码

#include
#include
#include
#include
#include
using namespace std;
#define maxn 210
#define INF 10000000

map inttostr;
map strtoint;
int num=0;
int N,K;
string start;
int End;


struct Node{
    int v;
    int d;
};

int happiness[maxn];//快乐值 
vector G[maxn];//邻接表
int vis[maxn],d[maxn];//访问距离
vector pre[maxn];//前序表 

int trace[maxn];

struct Route{
    int depth;
    int trace[maxn];
    int happy;
    int average;
};

vector P;

void change(string a){
    strtoint[a]=num;
    inttostr[num]=a;
    num++;
}

void Init(){
    int i;
    string str1,str2;
    int a,b;
    int H;
    Node temp;
    cin>>N>>K>>start;
    for(i=1;i>str1>>H;
        change(str1);
        happiness[strtoint[str1]]=H;
    }
    for(i=0;i>str1>>str2>>temp.d;
        a=strtoint[str1];
        b=strtoint[str2];
        temp.v=a;
        G[b].push_back(temp);
        temp.v=b;
        G[a].push_back(temp);
    }
    End=strtoint["ROM"];
}

void Dijkstra(){
    int i,j,Min;
    int u,v,dis;
    Node temp;
    for(i=0;id[u]+dis){
                    d[v]=d[u]+dis;
                    temp.d=dis;
                    temp.v=u;
                    pre[v].clear();
                    pre[v].push_back(temp);
                }else if(d[v]==d[u]+dis){
                    temp.d=dis;
                    temp.v=u;
                    pre[v].push_back(temp);
                }
            } 
        }
        if(u==End){
            break;
        }
    }
}

void DFS(int u,int depth,int d){
    int i;
    Route temp;
    trace[depth]=u;
    if(d<0){
        return;//舍弃 
    }
    if(d==0){
        if(u==0){
            temp.depth=depth;
            temp.happy=0;
            for(i=0;i<=depth;i++){
                temp.trace[i]=trace[i];
                temp.happy+=happiness[trace[i]];
            }
            temp.average=temp.happy/(depth);
            P.push_back(temp);
        }
        return;//到达路径终点,或舍弃 
    }
    for(i=0;ib.happy;
    }else{
        return a.average>b.average;
    }
}

int main(){
    int i;
    Route temp;
    Init();
    Dijkstra();
    DFS(End,0,d[End]);
    sort(P.begin(),P.end(),cmp);
    temp=P[0];
    cout<=0;i--){
        cout<0){
            cout<<"->";
        }else{
            cout<

运行结果

PAT-A1087 All Roads Lead to Rome 题目内容及题解_第1张图片

 

你可能感兴趣的:(PAT甲级,PAT,甲级,OJ,编程学习)