1030. Travel Plan (30)

来源:互联网 发布:淘宝联盟有什么用 编辑:程序博客网 时间:2024/06/05 20:26

题目还是很正常的最短路径,用DIJ

#include<iostream>#include<vector>#define MAX_V 510#define INF 0x3f3f3fusing namespace std;typedef struct ArcNode {    int length;//长度    int info;//花费    ArcNode() { this->length = INF;this->info = 0; }    bool operator<(const ArcNode &that) const    {        return (this->length < that.length ||(this->length == that.length && this->info <that.info));    }    ArcNode operator+(ArcNode that) const    {        that.length += this->length;        that.info += this->info;        return that;    }}ArcNode;int N, M, S, D;ArcNode Temp[MAX_V];//临时"最长路径"vector<int> Path[MAX_V];//存路径int d[MAX_V] = {};//最短路径int Money[MAX_V] = {};//存总花费bool visited[MAX_V] = {};ArcNode arc[MAX_V][MAX_V];//邻接矩阵void DIJ(int index){    d[index] = 0;    Path[index].push_back(index);    Money[index] = 0;    for (int t = 0;t < N;t++)    {        visited[t] = false;        if (!visited[t] && arc[index][t].length != INF)        {            Temp[t] = arc[index][t];            Path[t] = Path[index];        }    }    visited[index] = true;        while (!visited[D])        {               int v;            ArcNode min;            for (int t = 0;t < N;t++)                if (!visited[t] && Temp[t] < min) { min = Temp[t];v = t; }            d[v] = min.length; Money[v] = min.info;            visited[v] = true;Path[v].push_back(v);            for (int t = 0;t < N;t++)            {                if (!visited[t] && (arc[v][t] + min < Temp[t]))                {                    Temp[t] = arc[v][t] + min;                    Path[t] = Path[v];                }            }        }    }int main(){    cin >> N >> M >> S >> D;    for (int t = 0;t < M;t++)    {        int a, b, c, d;        cin >> a >> b >> c >> d;        arc[a][b].info = arc[b][a].info = d;        arc[a][b].length = arc[b][a].length = c;    }    DIJ(S);    for (auto x : Path[D])        cout << x<< " " ;    cout << d[D] << " " << Money[D] << endl;}
0 0
原创粉丝点击