题目: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;
}

 

 posted on 2020-09-05 22:25  邢涌芝  阅读(141)  评论(0编辑  收藏  举报