此题是dij过不了SPFA能过??
查看原帖
此题是dij过不了SPFA能过??
385349
KonJAC_xrs楼主2021/10/13 18:52

蒟蒻写了两种,SPFA获得100分的低分,而dij获得了87分的好成绩(雾

具体可以看我10.13下午晚六点左右的交替记录

但我觉得此题过程中没有负边权啊,难道是dij,spfa的本质不太相同导致的吗?

(全程已开long long)

求dalao指点

代码如下

#warning By KonjAC_xrs
#warning ( testing == 0 ) ? Yes : No

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
using namespace std;

inline ll read()
{
    ll x=0,f=1; char ch=getchar();
    while (!isdigit(ch)) {if(ch=='-') f=-1; ch=getchar();}
    while ( isdigit(ch)) {x = x * 10+ch-48; ch=getchar();}
    return x*f;
}

inline void xrs(ll x)
{
    if(x<0) putchar('-') , x=-x;
    if(x>9) xrs(x/10);
    putchar(x%10+'0');
}

const ll inf=1e18;
//const ll mod=;
const ll nore=1e6+20;

ll n,m;
ll sum[nore],a[nore],u[nore],v[nore];

ll tot,ver[nore<<1],head[nore],nxt[nore<<1];
inline void add(ll x,ll y)
{
	ver[++tot]=y;
	nxt[tot]=head[x];
	head[x]=tot;
}

ll tim,cut,top;
ll dfn[nore],low[nore],scc[nore],sta[nore];
short int inn[nore];
inline void tarjan(ll x)
{
	dfn[x]=low[x]=++tim;
	sta[++top]=x;
	inn[x]=1;
	for(register ll i=head[x];i;i=nxt[i])
	{
		ll y=ver[i];
		if(!dfn[y])
		{
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(inn[y])
			low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x])
	{
		ll z;
		cut++;
		do{
			z=sta[top--];
			scc[z]=cut;
			sum[scc[z]]+=a[z];
			inn[z]=0;
		}while(x!=z);
	}
}

inline void rebuild()
{
	for(register ll i=1;i<=m;i++)
		if(scc[u[i]]!=scc[v[i]])
			add(scc[u[i]],scc[v[i]]);
}

ll d[nore];
short int vis[nore];

inline void dark_spfa(ll s)
{
	queue < ll > q;
	q.push(s);
	d[s]=sum[s];
	vis[s]=1;
	while(!q.empty())
	{
		ll x=q.front();
		q.pop();
		vis[x]=0;
		for(register ll i=head[x];i;i=nxt[i])
		{
			ll y=ver[i];
			if(d[y]<d[x]+sum[y])
			{
				d[y]=d[x]+sum[y];
				q.push(y);
			}
		}
	}
}

//inline void dark_dij(ll s)
//{
////	for(register ll i=1;i<=n;i++)
////		d[i]=sum[i] , vis[i]=0;
//	priority_queue < pair < ll , ll > > q;
//	d[s]=sum[s];
//	q.push(make_pair(d[s],s));
//	while(!q.empty())
//	{
//		ll x=q.top().second;
//		q.pop();
//		if(vis[x]) continue;
//		vis[x]=1;
//		for(register ll i=head[x];i;i=nxt[i])
//		{
//			ll y=ver[i];
//			if(d[y]<d[x]+sum[y])
//			{
//				d[y]=d[x]+sum[y];
//				q.push(make_pair(d[y],y));
//			}
//		}
//	}
//}

inline void init()
{
	memset(head,0,sizeof head);
	memset(ver,0,sizeof ver);
	memset(nxt,0,sizeof nxt);
}

int main()
{
//	freopen("debuging.in","r",stdin);
//	freopen("debug.out","w",stdout);
	n=read(); m=read();
	for(register ll i=1;i<=m;i++)
	{
		u[i]=read();  v[i]=read();
	 	add(u[i],v[i]);
	}
	for(register ll i=1;i<=n;i++)
		a[i]=read();
	for(register ll i=1;i<=n;i++)
		if(!dfn[i])
			tarjan(i);
	
	init();
	rebuild();
	
	ll s=read(); s=scc[s];
//	dark_dij(s);
	dark_spfa(s);
	
	ll maxx=-inf;
	ll num=read();
	for(register ll i=1;i<=num;i++)
	{
		ll x=read();
		maxx=max(maxx,d[scc[x]]);
	}
	xrs(maxx); puts("");
	
	getchar();
    return 0;
}
/*
6 7 
1 2 
2 3 
3 5 
2 4 
4 1 
2 6 
6 5 
10 
12 
8 
16 
1 
5 
1 4 
4 3 5 6

*/

码风比较丑,望大家谅解qwq

2021/10/13 18:52
加载中...