求助! 三、四点WA了
查看原帖
求助! 三、四点WA了
108821
Mintind楼主2020/7/16 12:33
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

typedef pair<int, int> PII;

const int N = 55, M = 5005;

int dis[N], st[N];
int h[N], rh[N], ver[M], edge[M], nex[M];

int tot = 0;
inline void add(int *h, int x, int y, int z)
{
    tot++;
    ver[tot] = y;
    edge[tot] = z;
    nex[tot] = h[x];
    h[x] = tot;
}

void dijkstra(int s)
{
    memset(dis, 0x3f, sizeof dis);
    memset(st, 0, sizeof st);
    priority_queue<PII> q;
    
    dis[s] = 0;
    q.push({0, s});
    
    while (q.size())
    {
        int x = q.top().second;
        q.pop();
        if (st[x]) continue;
        st[x] = 1;
        for (int i = rh[x]; i; i = nex[i])
        {
            int y = ver[i], z = edge[i];
            if (dis[y] > dis[x] + z)
            {
                dis[y] = dis[x] + z;
                q.push({-dis[y], y});
            }
        }
    }
}

struct Node
{
    int g, x;
    long long vis;
    vector<int> path;
    bool operator < (const Node &t) const
    {
        if (g != t.g) return g > t.g;
        int l = min(path.size(), t.path.size());
        for (int i = 0; i < l; i++)
            if (path[i] != t.path[i]) return path[i] > t.path[i];
        return path.size() > t.path.size();
    }
};
void a_star(int s, int t, int k)
{
    dijkstra(t);
    
    memset(st, 0, sizeof st);
    priority_queue<Node> q;
    
    Node now;
    now.g = dis[s], now.x = s;
    now.vis = 1ll << s;
    now.path.push_back(s);
    q.push(now);
    
    while (q.size())
    {
        now = q.top();
        q.pop();
        
        int x = now.x, d = now.g - dis[x];
        if (st[x] >= k) continue;
        st[x]++;
        
        if (x == t && st[x] == k)
        {
            for (int i = 0; i < now.path.size(); i++)
                printf("%d%c", now.path[i], i == now.path.size() - 1 ? '\n' : '-');
            return;
        }
        
        for (int i = h[x]; i; i = nex[i])
        {
            int y = ver[i], z = edge[i];
            if ((now.vis >> y) & 1) continue;
            if (st[y] < k)
            {
                Node ne = now;
                ne.g = d + z + dis[y], ne.x = y;
                ne.vis = now.vis | (1ll << y);
                ne.path.push_back(y);
                q.push(ne);
            }
        }
    }
    
    printf("No");
}

int main()
{
    int n, m;
    int s, t, k;
    scanf("%d%d%d%d%d", &n, &m, &k, &s, &t);
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(h, a, b, c), add(rh, b, a, c);
    }
    a_star(s, t, k);
    return 0;
}
2020/7/16 12:33
加载中...