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<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
#define maxn 210
#define INF 10000000

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


struct Node{
    int v;
    int d;
};

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

int trace[maxn];

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

vector<Route> 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<N;i++){
        d[i]=INF;
    }
    change(start);
    happiness[strtoint[start]]=0;
    for(i=1;i<N;i++){
        cin>>str1>>H;
        change(str1);
        happiness[strtoint[str1]]=H;
    }
    for(i=0;i<K;i++){
        cin>>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;i<N;i++){
        u=-1,Min=INF;
        for(j=0;j<N;j++){
            if(vis[j]==0&&d[j]<Min){
                u=j;
                Min=d[u];
            }
        }
        if(u==-1){
            break;
        }
        vis[u]=1;
        for(j=0;j<G[u].size();j++){
            v=G[u][j].v;
            if(vis[v]==0){
                dis=G[u][j].d;
                if(d[v]>d[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;i<pre[u].size();i++){
        DFS(pre[u][i].v,depth+1,d-pre[u][i].d);
    } 
}

bool cmp(Route a,Route b){
    if(a.happy!=b.happy){
        return a.happy>b.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<<P.size()<<" "<<d[End]<<" "<<temp.happy<<" "<<temp.average<<endl;
    for(i=temp.depth;i>=0;i--){
        cout<<inttostr[temp.trace[i]];
        if(i>0){
            cout<<"->";
        }else{
            cout<<endl;
        }
    }
    return 0;
}

运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值