分层图dij,孩子的数组大小要调疯了
查看原帖
分层图dij,孩子的数组大小要调疯了
405146
Ruan_ji楼主2023/12/19 19:37

太难了,数组开大点就MLE,小点就RE,恳请各位帮帮我吧。给关注啥的都行

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

#define inf 0x7f7f

using namespace std;

struct str{
    long long a, b;
    bool operator<(const str e)const {
        return b > e.b;
    }
};

vector<str> g[210100];
priority_queue<str, vector<str>, less<str> > q;
long long dis[210100], n, m, k;
long long s, t;

void dij() {
	q.push((str){s, 0});
	memset(dis,0x7f,sizeof(dis));
	dis[s] = 0;
	while (!q.empty()) {
		str cnt = q.top();
		long long u = cnt.a;
		long long v = cnt.b;
		q.pop();
		
		if (v > dis[u]) {
			continue;
		}
		for (int i = 0; i < g[u].size(); ++i) {
			long long c1 = g[u][i].a;
			long long c2 = g[u][i].b;
			if (c2 + dis[u] < dis[c1]) {
				dis[c1] = c2 + dis[u];
				q.push((str){c1, dis[c1]});
			}
		}
	}
}

int main() {
    cin >> n >> m >> k;
    s = 1; t = n;

    for (int i = 1; i <= m; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        for (int j = 0; j <= k; ++j) {
            g[u + j * n].push_back((str){v + j * n, w});
            g[v + j * n].push_back((str){u + j * n, w});

            if (j != k) {
                g[u + j * n].push_back((str){v + (j + 1) * n, 0});
                g[v + j * n].push_back((str){u + (i + 1) * n, 0});
            }
        }
    }

    dij();

    long long ans = inf;
    for (long long i = 0; i <= k; ++i) {
        ans = min(ans, dis[t + i * n]);
    }    
    cout << ans << endl;
    return 0;
}
2023/12/19 19:37
加载中...