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


各项系数说明:

whappy[maxn]={0};//到达v点的最大happy
seq[maxn];//到达v点的最小序数
same[maxn];//到达v点的相同道路数量

#include<iostream>
#include<vector>
#include<string>
#include<stack>
#include<map>
using namespace std;
#define INF 10000000
#define maxn 200
bool visited[maxn]={false};
int road[maxn][maxn],dist[maxn],happy[maxn]={0},whappy[maxn]={0},seq[maxn],n,m,stid,path[maxn],same[maxn];
vector<int> city[maxn];
string st;
map<string ,int> stnum;
map<int,string> numts;
int fdist(){
    int min=INF,t=-1;
    for(int i=1;i<n;i++){
        if(!visited[i]&&dist[i]<min){
            min=dist[i];
            t=i;
        }
    }
    return t;
}
void dis(){
    dist[0]=0;
    seq[0]=0;
    whappy[0]=0;
    same[0]=1;
    int v=0;
    while(v!=-1){
        visited[v]=true;
        for(int i=0;i<city[v].size();i++){
            int k=city[v][i];
            if(!visited[k]&&road[v][k]!=INF){
                if(dist[k]>dist[v]+road[v][k]){
                    same[k]=same[v];
                    dist[k]=dist[v]+road[v][k];
                    path[k]=v;
                    seq[k]=seq[v]+1;
                    whappy[k]=whappy[v]+happy[k];
                    continue;
                }
                else if(dist[k]==dist[v]+road[v][k]){
                    same[k]+=same[v];
                    if(whappy[v]+happy[k]>whappy[k]){
                        path[k]=v;
                        whappy[k]=whappy[v]+happy[k];
                        seq[k]=seq[v]+1;
                        continue;
                    }
                    else if(whappy[v]+happy[k]==whappy[k]){
                        if(seq[v]+1<seq[k]){
                            path[k]=v;
                            seq[k]=seq[v]+1;
                            continue;
                        }
                    }
                }
            }
        }
        v=fdist();
    }
}
int main(){
    freopen("in.txt","r",stdin);
    cin>>n>>m>>st;
    stnum[st]=0;
    numts[0]=st;
    for(int i=1;i<n;i++){
        string s;
        cin>>s;
        cin>>happy[i];
        stnum[s]=i;
        numts[i]=s;
    }
    string s1,s2;
    fill(road[0],road[0]+maxn*maxn,INF);
    fill(seq,seq+maxn,INF);
    fill(dist,dist+maxn,INF);
    int t;
    for(int i=0;i<m;i++){
        cin>>s1>>s2>>t;
        int id1=stnum[s1],id2=stnum[s2];
        road[id1][id2]=t;
        road[id2][id1]=t;
        city[id1].push_back(id2);
        city[id2].push_back(id1);
    }
    dis();
    stack<int> s;
    int rid=stnum["ROM"];
    int z=rid;
    while(rid!=stid){
        s.push(rid);
        rid=path[rid];
    }
    printf("%d %d %d %d\n",same[z],dist[z],whappy[z],whappy[z]/seq[z]);
    cout<<st; 
    while(!s.empty()){
        cout<<"->"<<numts[s.top()];
        s.pop();
    }
    return 0;
}
Sure, based on the given problem, here is a class diagram that represents the entities and their relationships: ``` +-----------------+ +-----------------+ | Country |<>----------| City | +-----------------+ +-----------------+ | | | | | - name | | - name | | - cities | | - roads | | | | | | + addCity() | | + addRoad() | | + removeCity() | | + removeRoad() | +-----------------+ +-----------------+ ^ | | | | | +----------------------+ | TravelingUnit | +----------------------+ | | | - type | | - driver | | - wheels/legs | | - weapons | | - canFire | | - maxSpeed | | | | + move() | | + captureByBandit() | +----------------------+ ^ | | | | | +---------------------+ | Driver | +---------------------+ | | | - nationality | | - name | | | +---------------------+ ``` Explanation: - The `Country` class represents a country and has a relationship with multiple `City` objects through the `cities` attribute. It also has attributes like `name` and methods like `addCity()` and `removeCity()` for managing cities. - The `City` class represents a city and has a relationship with multiple `Road` objects through the `roads` attribute. It has attributes like `name` and methods like `addRoad()` and `removeRoad()` for managing roads. - The `TravelingUnit` class represents a traveling unit and contains attributes like `type`, `driver`, `wheels/legs`, `weapons`, `canFire`, and `maxSpeed`. It has methods like `move()` for moving the unit and `captureByBandit()` for capturing the unit. - The `Driver` class represents a driver and contains attributes like `nationality` and `name`. It is associated with the `TravelingUnit` class. Note: This is a basic representation of the system based on the given problem statement. There might be additional classes or relationships required depending on the specific requirements of the system.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值