AC 后发现有一个小细节和题解不同 。
代码 :
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,m,k;
int s,t;
ll ans[N];
int tot;
struct Side
{
int nxt,to,cost;
} e[N],E[N];
int h1[N],h2[N],cnt;
ll dis[N];
bool vis[N];
void SPFA()
{
queue<int> q;
for(int i=1;i<=n;i++)
{
dis[i]=LONG_LONG_MAX;
vis[i]=false;
}
dis[s]=0;
vis[s]=true;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=h2[u];i;i=E[i].nxt)
{
int v=E[i].to,w=E[i].cost;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
if(!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
}
struct Node
{
int u,w;
// ll w;
};
bool operator < (Node a,Node b)
{
return a.w+dis[a.u] > b.w+dis[b.u];
}
void A_Star()
{
priority_queue<Node> q;
q.push((Node){s,0});
while(!q.empty())
{
int u=q.top().u,w=q.top().w;
q.pop();
if(u==t)
{
ans[++tot]=w;
if(tot>=k) return ;
continue;
}
for(int i=h1[u];i;i=e[i].nxt)
{
int v=e[i].to;
q.push((Node){v,w+e[i].cost});
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
s=n;
t=1;
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
e[++cnt]=(Side){h1[u],v,w};
E[cnt]=(Side){h2[v],u,w};
h1[u]=h2[v]=cnt;
}
SPFA();
A_Star();
for(int i=1;i<=tot;i++)
{
printf("%lld\n",ans[i]);
}
for(int i=tot+1;i<=k;i++)
{
puts("-1");
}
return 0;
}
题解中 SPFA 跑反图,应该是以 t 为单源跑最短路径 。
但 改过来后变成了 50分 。
本蒟蒻十分不解,望大佬们指点 。