就保存所有的路径,找出最短最便宜的叭

#include<iostream>
#include<vector>
#include<limits.h>
using namespace std;
struct node
{
    int distance;
    int cost;
};
int num,roads,c,des;
node map[501][501];
bool vis[501] = {false};
vector<vector<int>> road;
vector<int> path;

void DFS(int c,vector<int> & path){
     path.push_back(c);
     vis[c] = true;
    if (c == des)
    {
        road.push_back(path);
        path.pop_back();
        vis[c] = false;
        return;
    }

for (int i = 0; i < num; ++i)
      {
          if(vis[i]==false&&map[c][i].distance!=0)
              DFS(i, path);
      }
      path.pop_back();
      vis[c] = false;
}
int main(){

node k;
    k.cost = 0;
    k.distance = 0;
    fill(map[0], map[0] + 500 * 500, k);
    cin >> num >> roads >> c >> des;
    for (int i = 0; i < roads;++i){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        map[a][b].distance = c;
        map[a][b].cost = d;
        map[b][a].distance = c;
        map[b][a].cost = d;
    }
    DFS(c, path);
    vector<int> res;
    int mark = 99999999;
    int mincost;
    for (int i = 0; i < road.size();++i){
        int sum = 0,cost=0;
        for (int j = 1; j < road[i].size();++j){
            sum += map[road[i][j - 1]][road[i][j]].distance;
            cost += map[road[i][j - 1]][road[i][j]].cost;
        }
        if(sum<mark){
            mark = sum;
            mincost = cost;
            res = road[i];
        }
        if(sum==mark){
            if(cost<mincost)
               mincost=cost, res = road[i];
        }

}
    for (int i = 0; i < res.size();++i){
        cout << res[i] << " ";
    }
    cout << mark << " " << mincost;
}

1030 Travel Plan (30分)(俺是个粗人)相关推荐

  1. PAT甲级1030 Travel Plan (30分):[C++题解]dijkstra求单源最短路、保存路径

    文章目录 题目分析 题目链接 题目分析 来源:PAT网站 分析 dijkstra模板默写过来,然后多了一个保存路径,使用数组pre[N]记录最短路上每个点的前驱,通过pre数组保存到vector中 v ...

  2. 【PAT】【spfa + dfs】1030 Travel Plan (30 分)

    题目链接:1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, ...

  3. 【讲解】1030 Travel Plan (30 分)【DFS】_41行代码Ac

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 A traveler's map gives the distances between cities along the hig ...

  4. 1030 Travel Plan (30 分)

    题目链接: PTA | 程序设计类实验辅助教学平台千名教师建设,万道高质量题目,百万用户拼题的程序设计实验辅助教学平台https://pintia.cn/problem-sets/9948053427 ...

  5. 1030 Travel Plan (30 分) 【难度: 中 / 知识点: 最短路】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805464397627392 先跑一下Dijkstra() 然后再dfs( ...

  6. 1030 Travel Plan(甲级)

    1030 Travel Plan (30分) A traveler's map gives the distances between cities along the highways, toget ...

  7. PAT甲级 1030 Travel Plan

    PAT甲级 1030 Travel Plan 题目链接 A traveler's map gives the distances between cities along the highways, ...

  8. 1030 Travel Plan(超级无敌详细注释+47行代码)

    分数 30 全屏浏览题目 切换布局 作者 CHEN, Yue 单位 浙江大学 A traveler's map gives the distances between cities along the ...

  9. 2020PAT甲级秋季7-4 Professional Ability Test (30分)

    7-4Professional Ability Test(30分) Professional Ability Test (PAT) consists of several series of subj ...

最新文章

  1. “智慧城市”背后的安全隐患
  2. 青龙羊毛——帮多多(教程)
  3. Nginx-09:Nginx原理
  4. 成功解决AttributeError: 'GradientBoostingRegressor' object has no attribute 'staged_decision_function'
  5. python类方法是什么_python类方法和普通方法区别是什么
  6. Fetch API HTTP请求实用指南
  7. HTMl5的存储方式sessionStorage和localStorage详解
  8. Exchange Server 2010 SP3部署
  9. Bootstrap 容器(container)
  10. python语言中包含的标准数据类型有哪些_Python中的标准数据类型
  11. linux选择内核命令,Lenky个人站点
  12. scrollTop、clientHeight、 scrollHeight...学完真的理解了
  13. ITU-R BT601/BT709 BT656/BT1120区别与联系
  14. 高德地图开放平台(js免费引入)
  15. JavaEE Day14 ServletHTTPRequest
  16. 数字电路:常见的锁存器浅析(S-R,S‘-R‘,使能端的S-R,D)
  17. Kotlin中对象检查判断
  18. 中国大学Mooc浙大翁恺老师《零基础学Java语言》编程作业
  19. 关于解决idea 输入法不跟随问题
  20. 小明加密通道进入_如何利用PS通道去除面部雀斑

热门文章

  1. -01-OV7251摄像头与设计规划【Xilinx-LVDS读写功能实现】
  2. python 自动发微博_python3实现古城钟楼每个时辰自动发微博
  3. 统一知识图学习和推荐:更好地理解用户偏好
  4. Log4j使用方式详解
  5. 创意小发明:单片机制作电容电阻测量仪 (含C语言原源码,仿真文件与实物图)
  6. 为什么公司宁愿招新人也不愿涨工资?
  7. 计算机毕业设计Node.js校园二手拍卖网(源码+程序+LW+远程调试)
  8. SilkTest那些事儿_第一章 ST的HelloWord_录制回放1
  9. 06_平台总线匹配规则,自己搭建总线xbus
  10. linux自带的二进制查看器