zoukankan      html  css  js  c++  java
  • 图6 旅游规划

    题目:https://pintia.cn/problem-sets/1268384564738605056/problems/1284061680920891393
    题解:https://blog.csdn.net/hh66__66hh/article/details/83822673
    代码:

    #include <iostream>
    #include <stdio.h>
    #include <string>
    #include<string.h>
    #include <vector>
    #include <map>
    #include <stack>
    #include <algorithm>
    #include <stack>
    #include <queue>
    using namespace std;
    
    int graph[505][505];
    int path[505][505];
    int tip[505];
    int shortest[505];
    vector<int>parent[505];
    vector<int>temp;
    int paths;
    
    #define Biggest 1000
    
    void cost(int i, int s) {
        int j, k, a, b;
    
        if(i == s) {
            k = 0;
            for(j=0; j<temp.size(); j++) {
                k = k + temp[j];
            }
            if(k < paths) {
                paths = k;
            }
        }
        else {
            for(j=0; j<parent[i].size(); j++) {
                temp.push_back(path[i][parent[i][j]]);
                cost(parent[i][j], s);
                temp.pop_back();
            }
        }
    }
    
    void dijkstra(int s, int d, int n) {
        int i, j, k, this_node;
        memset(tip, 0, sizeof(tip));
    
        j = Biggest;
        shortest[s] = 0;
        parent[s].push_back(s);
    
        for(k=0; k<n; k++) {
            j = Biggest;
    
            for(i=0; i<n; i++) {
                if(tip[i] ==0 && j > shortest[i]) {
                    j = shortest[i];
                    this_node = i;
                }
            }
    
            tip[this_node] = 1;
            if(this_node == d) {
                break;
            }
    
            for(i=0; i<n; i++) {
                if(tip[i] == 0) {
                    if(shortest[i] > shortest[this_node] + graph[this_node][i]) {
                        shortest[i] = shortest[this_node] + graph[this_node][i];
                        parent[i].clear();
                        parent[i].push_back(this_node);
                    }
                    else if(shortest[i] == shortest[this_node] + graph[this_node][i]) {
                        shortest[i] = shortest[this_node] + graph[this_node][i];
                        parent[i].push_back(this_node);
                    }
                }
            }
        }
    
    }
    
    int main() {
        int i, j, k, l, n, m, s, d, ans;
    
        while(~scanf("%d %d %d %d", &n, &m, &s, &d)) {
    
            for(i=0; i<n; i++) {
                shortest[i] = Biggest;
                parent[i].clear();
                for(j=i; j<n; j++) {
                    if(i == j) {
                        graph[i][j] = 0;
                    }
                    else {
                        graph[i][j] = graph[j][i] = Biggest;
                    }
                }
            }
    
            while(m--) {
                scanf("%d %d %d %d", &i, &j, &k, &l);
                graph[i][j] = graph[j][i] = k;
                path[i][j] = path[j][i] = l;
            }
    
            dijkstra(s, d, n);
    
            i = d;
            ans = 0;
            while(i != s) {
                ans = ans + graph[i][parent[i][0]];
                i = parent[i][0];
            }
    
            temp.clear();
            paths = 300000;
            cost(d, s);
            cout<<ans<<" "<<paths<<endl;
    
        }
    
    
    
        return 0;
    }
  • 相关阅读:
    vue-cli中安装方法
    css初始化
    VUE基本指令(v-model,v-html,v-text,v-bind,v-if,v-show,v-for,v-on:click,组件,过滤器)
    在浏览器上安装 Vue Devtools工具
    vue前端框架面试问题汇总
    git修改用户名、邮箱
    js对字符串的一些操作方法
    11款JavaScript颜色拾取插件推荐
    vue-devtools的安装与使用
    JS里的居民们4-数组((堆)队列
  • 原文地址:https://www.cnblogs.com/simon-chou/p/13620059.html
Copyright © 2011-2022 走看看