怎么卡空间?
查看原帖
怎么卡空间?
538427
czy0323楼主2023/7/21 19:39
#include <iostream>
#include <queue>
#include <map>
#include <vector>
#include <stack>
using namespace std;
const int N = 3005;
int n, m, k;
int f[N][N];
short pre[N][N];

struct swear{
    short x, y, z;
    bool operator <(const swear &b) const{
        if( x != b.x )  return x < b.x;
        if( y != b.y )  return y < b.y;
        return z < b.z;
    }
};

struct state{
    short last, now;
};

vector<short> g[N];
map<swear, bool> iswear;
queue<state> q;
stack<short> st;

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n >> m >> k;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            f[i][j] = 1e9;
    for(int i = 1; i <= m; i++){
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    swear it;
    for(int i = 1; i <= k; i++){
        cin >> it.x >> it.y >> it.z;
        iswear[it] = 1;
    }
    state s, s1;
    s.last = 0, s.now = 1;
    q.push(s);
    while( !q.empty() ){
        s = q.front();
        q.pop();
        for(auto i : g[s.now]){
            it.x = s.last, it.y = s.now, it.z = i;
            if( iswear[it] )    continue;
            s1.last = s.now, s1.now = i;
            if( f[s1.last][s1.now] != 1e9 )
                continue;
            f[s1.last][s1.now] = f[s.last][s.now] + 1;
            pre[s1.last][s1.now] = s.last;
            q.push(s1);
        }
    }
    int ans = 1e9, last, now = n;
    for(int i = 1; i < n; i++){
        if( f[i][n] < ans ){
            last = i;
            ans = f[i][n];
        }
    }
    if( ans == 1e9 ){
        cout << -1;
        return 0;
    }
    cout << ans << "\n";
    st.push(now);
    while( last ){
        st.push(last);
        short temp = last;
        last = pre[last][now];
        now = temp;
    }
    while( !st.empty() ){
        cout << st.top() << ' ';
        st.pop();
    }
    return 0;
}
2023/7/21 19:39
加载中...