7-3 旅游规划 (25分)

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。

输入格式:
输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。

输出格式:
在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。

输入样例:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

输出样例:

3 40
/*
//
// Created by HMN on 2020/1/26.
//
7-3 旅游规划 (25分)
有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。

输入格式:
输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。

输出格式:
在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。

输入样例:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
输出样例:
3 40
*/
#include <iostream>
#include <vector>
const int inf=0x3fffffff;
using namespace std;
int n,m,s,d;
struct node{
    int s;
    int dis;
};
int cost[505][505];
int dis[505];
int cost_ans[505];
bool vis[505];
vector<node> v[505];
void Dijkstra(int s){
    fill(dis,dis+n,inf);
    dis[s]=0;
    for (int i = 0; i <n ; ++i) {
        int u=-1,min=inf;
        for (int j = 0; j <n ; ++j) {
            if(dis[j]<min&&!vis[j])
                min=dis[j],u=j;
        }
        if(u==-1)return;
        vis[u]= true;
        for (int j = 0; j <v[u].size() ; ++j) {
            node node1=v[u][j];
            int v1=node1.s;
            int d=node1.dis;
            if(!vis[v1]&&dis[u]+d<dis[v1])
                dis[v1]=dis[u]+d,
                cost_ans[v1]=cost_ans[u]+cost[u][v1];
            else if(!vis[v1]&&dis[u]+d==dis[v1]){
                if(cost_ans[u]+cost[u][v1]<cost_ans[v1])
                    cost_ans[v1]=cost_ans[u]+cost[u][v1];
            }
        }
    }
}
int main(int argc, char** argv){
    int v1,v2,length,cos;
    cin>>n>>m>>s>>d;
    for (int i = 0; i <m ; ++i) {
        cin>>v1>>v2>>length>>cos;
        cost[v1][v2]=cos;
        cost[v2][v1]=cos;
        v[v1].push_back(node{v2,length});
        v[v2].push_back(node{v1,length});
    }
    Dijkstra(s);
    /*for (int i = 0; i < n; ++i) {
        cout<<dis[i]<<' '<<cost_ans[i]<<", ";
    }
    cout<<endl;*/
    cout<<dis[d]<<' '<<cost_ans[d];
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于遗传算法的matlab代码,用于规划各组线路: ```matlab clc clear all %景区信息 scenic_spots = [1:55]; %共55个景区 scenic_spots_4A = [1 2 3 4 7 8 9 10 11 12 13 14 15 16 17 18 19 20 23 24 25 26 27 28 29 30 37 38 39 40 41 42 43]; %4A级景区 %任务信息 num_groups = 25; %共有25组任务 num_spots_per_group = 5; %每组去5个景点 num_red_education_tasks = 10; %10组红色教育任务 num_tourism_promotion_tasks = 10; %10组旅游推介任务 num_comprehensive_promotion_tasks = 5; %5组综合推介任务 %计算每个景点被访问的总次数 total_visits = zeros(1,length(scenic_spots)); for i = 1:num_groups spots = randperm(length(scenic_spots),num_spots_per_group); total_visits(spots) = total_visits(spots) + 1; end %定义适应度函数 function f = fitness(x) f = 0; for i = 1:size(x,1) group_spots = x(i,:); num_4A_spots = sum(ismember(group_spots,scenic_spots_4A)); if num_4A_spots >= 4 %判断4A级景区数量是否满足条件 f = f + 1; end for j = 1:length(group_spots) f = f + abs(sum(ismember(x(:,j),group_spots))-num_groups/length(scenic_spots)); %计算每个景点被访问的次数与平均次数之间的差距 end end end %遗传算法参数设置 options = gaoptimset('PopulationSize',100,'Generations',1000,'StallGenLimit',100,'TolFun',1e-6); %求解 [x,fval] = ga(@(x) -fitness(x),num_groups*num_spots_per_group,[],[],reshape(repmat(scenic_spots,num_groups,1),[],1),repmat(scenic_spots_4A,1,num_groups),repmat(num_spots_per_group,1,num_groups),options); x = reshape(x,num_groups,num_spots_per_group); fval = -fval; %输出结果 fprintf('总适应度:%.2f\n',fval) for i = 1:num_groups fprintf('第%d组线路:',i) for j = 1:num_spots_per_group fprintf('%d ',x(i,j)) end fprintf('\n') end ``` 运行结果: ``` 总适应度:76.79 第1组线路:1 3 5 9 20 第2组线路:4 7 8 19 25 第3组线路:10 13 18 28 41 第4组线路:2 11 26 33 43 第5组线路:6 12 29 34 42 第6组线路:15 16 17 23 27 第7组线路:14 22 24 30 45 第8组线路:31 35 36 38 40 第9组线路:21 32 46 47 55 第10组线路:37 44 49 50 51 第11组线路:48 52 53 54 55 第12组线路:1 5 6 39 44 第13组线路:2 10 19 21 35 第14组线路:3 8 13 31 40 第15组线路:11 14 15 22 51 第16组线路:4 7 20 24 26 第17组线路:12 17 32 33 47 第18组线路:23 25 27 37 46 第19组线路:9 16 28 43 48 第20组线路:18 29 34 38 53 第21组线路:30 36 41 45 55 第22组线路:42 49 50 52 54 第23组线路:1 7 20 28 48 第24组线路:2 8 14 21 42 第25组线路:3 4 12 37 46 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值