你说得对,但是
查看原帖
你说得对,但是
1430250
_hud楼主2025/7/24 10:04

玄关求条 QAQ。

你说得对,本题正解并不是 A*,但本来只是想用 A* 水过去的,发现被卡了就阅读了巨佬 @STDLRZ 的题解,(https://www.luogu.com.cn/article/1sk5zo96),但我感觉本蒟蒻的代码和巨佬的代码并无很大差异啊?求条

qwq 帮帮这个蒟蒻吧 /ll

附:我的代码

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 60, INF = 1e15;
string str[N] = {
    "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 
    "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
    "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
    "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
    "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
    "50", "51", "52", "53", "54", "55", "56", "57", "58", "59"
};
typedef pair<int, int> PII;
struct E { int to, nxt, w; } e[N * N * 2];
int n, m, k, s, t, cnt, h[N], rh[N], dis[N];
bitset<N> f, qwq;
struct node {
    int g, h, u;
    bitset<N> vis;
    string v = "";
    node(int gg, int hh, int now, string s) : g(gg), h(hh), u(now), v(s) {}
    bool operator< (const node &x) const {
        return ((g + h) ^ (x.g + x.h)) ? (g + h) > (x.g + x.h) : v > x.v;
    }
};
inline void add(int u, int v, int w, bool is = 0) {
    e[++cnt] = {v, (is ? rh[u] : h[u]), w};
    is ? (rh[u] = cnt): (h[u] = cnt);
}
inline void dij() {
    for(int i = 1; i <= n; ++i) dis[i] = INF; f.reset();
    priority_queue<PII, vector<PII>, greater<PII>> q; q.push({(dis[t] = 0), t});
    while(!q.empty()) {
        int u = q.top().second; q.pop();
        if(f[u]) continue;
        f[u] = 1;
        for(int i = rh[u]; i; i = e[i].nxt) {
            int p = e[i].to;
            if(qwq[p]) continue;
            if(dis[p] > dis[u] + e[i].w) 
                q.push({(dis[p] = dis[u] + e[i].w), p});
        }
    }
}
inline void astar() {
    priority_queue<node> q;
    node now = {0, dis[s], s, str[s]}; now.vis.set(s);
    q.push(now); int awa = 0;
    while(!q.empty()) {
        now = q.top(); q.pop();
        int u = now.u;
        if(u == t) if((++awa) == k) { cout << now.v; return; }
        // f = now.vis; dij();
        qwq = now.vis; dij();
        for(int i = h[u]; i; i = e[i].nxt) {
            int p = e[i].to;
            if(now.vis[p]) continue;
            node nxt = {now.g + e[i].w, dis[p], p, now.v + "-" +str[p]};
            nxt.vis = now.vis; nxt.vis.set(p);
            q.push(nxt);
        }
    }
    cout << "No"; exit(0);
}
signed main() {
    cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
    cin >> n >> m >> k >> s >> t; 
    if(s == t) ++k;
    for(int i = 1, u, v, w; i <= m; ++i)
        cin >> u >> v >> w, add(u, v, w), add(v, u, w, 1);
    f.reset(); dij();
    if(dis[s] >= INF) { cout << "No"; return 0; }
    astar();
    return 0;
}

和题解中巨佬的代码(我的代码就是仿照着这个写的):

#include<bits/stdc++.h>
#define x0 x_0
#define x1 x_1
#define y0 y_0
#define y1 y_1
#define yn y_n
#define d0 d_0
#define d1 d_1
#define j0 j_0
#define j1 j_1
#define k0 k_0
#define k1 k_1
#define LL long long
#define LD long double
using namespace std;
int n,m,k,a,b;
struct node{
	int v;
	LL w;
	bool operator < (const node &o)const{
		return w>o.w;
	}
};
struct dij_node{
	int v;
	LL w;
	bool operator < (const dij_node &o)const{
		return w>o.w;
	}
};
struct STRING{
	char p[50];
	int l;
	bool operator > (const STRING &o)const{
		int cst=min(l,o.l);
		for(int i=0;i<cst;++i){
			if(p[i]>o.p[i]) return 1;
			else if(p[i]<o.p[i]) return 0;
		}
		if(l>=o.l) return 1;
	}
};
struct point{
	int v;
	LL g,h;
	STRING s;
	LL S;
	bool operator < (const point &o)const{
		if((g+h)^(o.g+o.h)) return g+h>o.g+o.h;
		return s>o.s;
	}
};
vector<node> G[60],G2[60];
char inc[60];
int decc[128];
char incode_int(int x){
	return (x<=26 ? 'A'+x-1 : 'a'-26+x-1);
}
int decode_char(char x){
	return (x>='A' && x<='Z' ? x-'A'+1 : x-'a'+27);
}
LL dis[60];
bool f[60];
const LL mex=1e15;
void dijkstra(int s,LL SSS){ // Part 1 部分
	for(int i=1;i<=n;++i) dis[i]=mex;
	dis[s]=0;
	memset(f,0,sizeof(f));
	priority_queue<dij_node> q;
	q.push({s,0});
	while(q.size()){
		dij_node dt=q.top();
		q.pop();
		if(f[dt.v]) continue;
		int x=dt.v;
		f[x]=1;
		for(int i=0;i<(int)G2[x].size();++i){
			int u=G2[x][i].v;
			if(SSS&(1LL<<u)) continue;
			LL w=G2[x][i].w;
			if(dis[u]>dis[x]+w){
				dis[u]=dis[x]+w;
				q.push({u,dis[u]});
			}
		}
	}
	return ;
}
int cnt[60];
void Astar(int s,int t,int k){
	memset(cnt,0,sizeof(cnt));
	priority_queue<point> q;
	STRING psh;
	psh.p[psh.l++]=incode_int(s);
	q.push({s,0,0,psh,1LL<<s});
	while(q.size()){
		point dt=q.top();
		q.pop();
		int x=dt.v;
		++cnt[x];
		if(x==t && cnt[x]==k){
			int len=dt.s.l;
			cout<<decc[dt.s.p[0]];
			for(int i=1;i<len;++i) cout<<"-"<<decc[dt.s.p[i]];
			cout<<"\n";
			exit(0);
		}
		dijkstra(t,dt.S);
		for(int i=0;i<(int)G[x].size();++i){
			int u=G[x][i].v;
			LL w=G[x][i].w;
			if(dt.S&(1LL<<u)) continue;
			dt.s.p[dt.s.l++]=inc[u];
			q.push((point){u,dt.g+w,dis[u],dt.s,dt.S|(1LL<<u)});
			--dt.s.l;
		}
	}
	cout<<"No\n";
	exit(0);
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>k>>a>>b;
	for(int i=1;i<=m;++i){
		int u,v;
		LL w;
		cin>>u>>v>>w;
		G[u].push_back({v,w});
		G2[v].push_back({u,w});
	}
	for(int i=1;i<=52;++i) inc[i]=incode_int(i);
	for(int i=0;i<128;++i) decc[i]=decode_char((char)i);
	dijkstra(b,0);
	if(dis[a]>=mex) cout<<"No\n",exit(0);
	Astar(a,b,k);
	return 0;
}
2025/7/24 10:04
加载中...