P1087. All Roads Lead to Rome (30)

本文介绍了一个寻找从起点到罗马的最短路径的问题,并且在所有最短路径中找到获得最大快乐值及平均快乐值的路径。通过使用Dijkstra算法和深度优先搜索实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这个题大概意思是,先求出从起点到终点ROM的最短路,可能有多条,其中每个路径节点都有一个快乐值,求有多少条最短路,路径快乐值最大是多少,路径快乐平均值最大是多少。
因为题目的输入全部是字符串,所以我们用一个map映射我们自己规定的字符串和序号的映射。然后,用dijkstra算法来求得最短路并且对于每一个节点用vector来保存上一个节点。由于每个节点的父节点可能有多个,我们在执行完最短路算法之后再进行深度优先搜索,利用记录下来的last来访问前一个节点,最后到起始节点0的时候计算快乐值就ok了。
整个题目并不复杂,但是用到的东西很多,需要胆大心细的去用。
我运气不错,只调了2次就过了。
第一次题目看走眼了,快乐平均值是除去罗马的快乐值之外总快乐除以除去罗马之外总节点个数,我说样例怎么是97而我算得98来着。
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
map<int,string>dsa;
map<string,int> asd;
int grond[201][201];
int flag[201];
int dist[201];
int val[201];
vector<int> last[201];
vector<int> rem;
int m,n,fin;
double ans,pin;
int num1;

void dfs(int s,vector<int> d){
    if(s == 0){
        num1++;
        int len = d.size();
        double www = 0;
        for(int i = 0;i<len;i++){
            www += val[d[i]];
        }
        if(www == ans){
            if(www/len > pin){
                ans=www;
                pin = www/(len-1);
                rem = d;
            }
        }
        else if(www > ans)
        {
            ans=www;
            pin =  www/(len-1);
            rem = d;
        }
    }
    int len = last[s].size();
    if(len<1)
        return;
    for(int i =0 ;i<len;i++){
        if(!flag[last[s][i]]){
            flag[last[s][i]]=1;
            d.push_back(last[s][i]);
            dfs(last[s][i],d);
            d.pop_back();
            flag[last[s][i]]=0;
        }
    }
}

void dis(){
    memset(flag,0,sizeof(flag));
    dist[0]=0;
    flag[0]=1;
    for(int i =1;i<n;i++){
        dist[i]=grond[0][i];
        if(dist[i]!=9999)
            last[i].push_back(0);
    }
    for(int i = 1;i<n;i++){
        int k = -1;
        for(int j = 1;j<n;j++){
            if(!flag[j] && (k==-1 || dist[k]>dist[j]))
                k=j;
        }
        if(k==-1)
            break;
        flag[k]=1;
        for(int j = 0;j<n;j++){
            if(!flag[j] && dist[j]>dist[k]+grond[k][j]){
                dist[j]=dist[k]+grond[k][j];
                last[j].clear();
                last[j].push_back(k);
            }
            else if(!flag[j] && dist[j]==dist[k]+grond[k][j]){
                last[j].push_back(k);
            }
        }
    }
    ans = 0;
    pin = 0;
    memset(flag,0,sizeof(flag));
    int now = fin;
    int v = val[fin];
    vector<int> sd;
    sd.push_back(fin);
    num1 = 0;
    dfs(now,sd);
    int lll = rem.size();
    int ppppp = (int)pin;
    cout<<num1<<" "<<dist[fin]<<" "<<ans<<" "<<ppppp<<endl;
    for(int i = lll-1;i>0;i--){
        cout<<dsa[rem[i]]<<"->";
    }
    cout<<dsa[rem[0]]<<endl;
}

int main(){
    string s;
    cin>>n>>m>>s;
    asd[s]=0;
    dsa[0]=s;
    int tem;
    for(int i = 1;i<n;i++){
        cin>>s>>tem;
        if(s=="ROM")
            fin = i;
        asd[s]=i;
        dsa[i]=s;
        val[i]=tem;
    }
    string d;
    memset(grond,9999,sizeof(grond));
    for(int i =0;i<m;i++){
        cin>>s>>d>>tem;
        int aa = asd[s];
        int bb = asd[d];
        grond[aa][bb]=tem;
        grond[bb][aa]=tem;
    }
    dis();
    return 0;
}




内容概要:本文深入探讨了Kotlin语言在函数式编程和跨平台开发方面的特性和优势,结合详细的代码案例,展示了Kotlin的核心技巧和应用场景。文章首先介绍了高阶函数和Lambda表达式的使用,解释了它们如何简化集合操作和回调函数处理。接着,详细讲解了Kotlin Multiplatform(KMP)的实现方式,包括共享模块的创建和平台特定模块的配置,展示了如何通过共享业务逻辑代码提高开发效率。最后,文章总结了Kotlin在Android开发、跨平台移动开发、后端开发和Web开发中的应用场景,并展望了其未来发展趋势,指出Kotlin将继续在函数式编程和跨平台开发领域不断完善和发展。; 适合人群:对函数式编程和跨平台开发感兴趣的开发者,尤其是有一定编程基础的Kotlin初学者和中级开发者。; 使用场景及目标:①理解Kotlin中高阶函数和Lambda表达式的使用方法及其在实际开发中的应用场景;②掌握Kotlin Multiplatform的实现方式,能够在多个平台上共享业务逻辑代码,提高开发效率;③了解Kotlin在不同开发领域的应用场景,为选择合适的技术栈提供参考。; 其他说明:本文不仅提供了理论知识,还结合了大量代码案例,帮助读者更好地理解和实践Kotlin的函数式编程特性和跨平台开发能力。建议读者在学习过程中动手实践代码案例,以加深理解和掌握。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值