hahahahazzz 提交的代码
提交时间:2021年2月8日 18:11 语言:C++运行时间:0ms占用内存:256K
运行状态: Accepted
题目:最短路径问题1344

登录之后查看代码,点此登录账号

                
                    #include <bits/stdc++.h>
using namespace std;

#define INF 0x3f3f3f3f;
const int maxn = 105;

struct Edge {
    int u, v, w, p;
    // Edge(int a, int b, int d, int p) {
    //     this->a = a;
    //     this->b = b;
    //     this->d = d;
    //     this->p = p;
    // };
    Edge(int u, int v, int w, int p): u(u), v(v), w(w), p(p){};
    Edge(){};
};

struct node {
    int d, u;
    node(int d, int u): d(d), u(u) {};
    friend bool operator < (node a, node b) {
        return a.d > b.d;
    }
};

vector<Edge> edges;
vector<int> G[maxn];
vector<node> nodes;
int dist[maxn];
int vis[maxn];
int p[maxn];
int cost[maxn];

void dijkstra(int s, int n) {
    priority_queue<node> q;
    for (int i = 0; i <= n; i++) {
        dist[i] = INF;
    }
    dist[s] = 0;
    memset(vis, 0, sizeof(vis));
    q.push(node(0, s));
    
    while (!q.empty()) {
        node now = q.top();
        q.pop();
        int u = now.u;
        vis[u] = 0;
        
        for (int i = 0; i < G[u].size(); i++) {
            Edge &e = edges[G[u][i]];
            if (dist[e.v] > dist[u] + e.w) {
                dist[e.v] = dist[u] + e.w;
                p[e.v] = G[u][i];
                cost[e.v] = cost[u] + e.p;
                if (vis[u] == 1) {
                    continue;
                }
                vis[u] = 1;
                q.push(node(dist[e.v], e.v));
            } else if (dist[e.v] == dist[u] + e.w) {
                if (cost[e.v] > cost[u] + e.p) {
                    cost[e.v] = cost[u] + e.p;
                }
            }
        }
    }
}

void addedge(int u, int v, int w, int p) {
    edges.push_back(Edge(u, v, w, p));
    int sz = edges.size();
    G[u].push_back(sz - 1);
}

void init(int n) {
    for (int i = 0; i <= n; i++) {
        G[i].clear();
    }
    edges.clear();
}

int main() {
    int n, m;
    int s, t;
    while (cin >> n >> m) {
        if (n == 0 || m == 0) {
            break;
        }
        init(n);
        for (int i = 0; i < m; i++) {
            int a, b, d, p;
            cin >> a >> b >> d >> p;
            addedge(a, b, d, p);
            addedge(b, a, d, p);
        }
        cin >> s >> t;
        dijkstra(s, n);

        cout << dist[t] << " " << cost[t] << endl;

    }
    return 0;
}