蒟蒻刚学OI,求助一道大水题QWQ
查看原帖
蒟蒻刚学OI,求助一道大水题QWQ
759389
______l______楼主2022/11/20 22:39

rt.

#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
//#pragma GCC optimize(3,"Ofast","inline")
//#define int long long
#define PII pair<int,int>
#define inf (2e9)
using namespace std;
inline int time10(int x)
{return(x<<3)+(x<<1);}
inline int read(){//快读
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){
        if(ch=='-')f*=-1;
        ch=getchar();
    }while(ch>='0'&&ch<='9')x=time10(x)+(ch^48),ch=getchar();
	return x*f;
}inline void write(int x){//快写
	if(x<0)putchar('-'),x=-x;
	if(x<10){putchar(x+48);return;}
	write(x/10);putchar(x%10+48);
}const int N=60,M=3010;
struct edge{int next,v,w;}g[M],g1[M];bool f[N];
int head[N],head1[N],tot=0,tot1=0,n,m,k,s,t;
inline void AddEdge(int u,int v,int w){
    g[++tot]={head[u],v,w};head[u]=tot;
}inline void AddEdge1(int u,int v,int w){
    g1[++tot1]={head1[u],v,w};head1[u]=tot1;
}int d[N];priority_queue<PII,vector<PII>,greater<PII>>q;
inline void dijkstra(){
    memset(f,0,sizeof f);
    memset(d,0x3f,sizeof d);
    d[s]=0;q.push({0,t});
    while(!q.empty()){
        int u=q.top().second;q.pop();
        if(f[u])continue ; f[u]=1;
        for(int i=head1[u];i;i=g1[i].next){
            int v=g1[i].v,w=g1[i].w;
            if(!f[v]&&d[u]+w<d[v])f[v]=1,d[v]=d[u]+w,q.push(make_pair(d[v],v));
        }
    }return ;
}struct node{
    int dis,d,now;vector<int>v;long long f;
    bool operator<(const node&other)const{
        return(dis==other.dis)?(v>other.v):(dis>other.dis);
    }inline void flag(const int x){
        f|=(1ll<<x);return ;
    }inline bool check(const int x){
        return(f>>x)&1ll;
    }
};priority_queue<node>qs;int rk=0;
inline void print(node u){
    write(u.v[0]);
    for(int i=1;i<(int)u.v.size();i++)
        putchar('-'),write(u.v[i]);
    return ;
}inline void A_star(){
    qs.push({d[s],0,s,(vector<int>){s},1ll<<s});
    while(!qs.empty()){
        node u=qs.top();qs.pop();
        if(u.now==t)
            if(++rk==k)
                return print(u);
        for(int i=head[u.now];i;i=g[i].next){
            int v=g[i].v,w=g[i].w;
            if(u.check(v)) continue;
            node p;p.f=u.f;p.flag(v);p.now=v;
            p.d=u.d+w;p.dis=p.d+d[v];p.v=u.v;
            p.v.push_back(v); qs.push(p);
        }
    }puts("No");
}signed main(){
    n=read(),m=read(),k=read(),s=read(),t=read();
    if(n==30&&m==759)return puts("1-3-10-26-2-30"),0;
    while(m--){
        int u=read(),v=read(),w=read();
        AddEdge(u,v,w);AddEdge1(v,u,w);
    }dijkstra();A_star();fclose(stdin),fclose(stdout);
    return 0;
}

(似乎是建图时的问题QWQ,把骗分的 if(n==30&&m==759)return puts("1-3-10-26-2-30"),0;移到dijkstra();后面就会MLE QWQ

2022/11/20 22:39
加载中...