#8#9WA求调
查看原帖
#8#9WA求调
188879
VioletIsMyLove楼主2023/5/11 22:03
#include<bits/stdc++.h>
using namespace std;
const int TT=3000;
struct VioletEvergarden{
	int id,v;
	bool operator <(const VioletEvergarden b)const{return v<b.v;}
}hep[3005];
int n,m,tot,len;
int lnk[3005],son[9005],nxt[9005],v[9005],h[3005],q[3005],du[3005],dis[3005];
bool vis[3005];
long long ans;
inline int read(){
	int ret=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch)){ret=ret*10+ch-'0';ch=getchar();}
	return ret*f;
}
void make(int x,int y,int z){son[++tot]=y;nxt[tot]=lnk[x];v[tot]=z;lnk[x]=tot;}
int SPFA(){
	memset(h,127,sizeof h);
	int hed=0,til=0;q[++til]=0;h[0]=0;vis[0]=1;
	while(hed^til){
		hed=(hed+1)%TT;vis[q[hed]]=0;
		for(int i=lnk[q[hed]];i;i=nxt[i])if(h[q[hed]]+v[i]<h[son[i]]){
			h[son[i]]=h[q[hed]]+v[i];
			if(!vis[son[i]]){
				q[til=(til+1)%TT]=son[i];
				vis[son[i]]=1;
				if(++du[son[i]]==n+1)return 1;
			}
			
		}
	}
	return 0;
}
void put(VioletEvergarden x){
	int fa=++len;hep[len]=x;
	while(1<fa&&hep[fa]<hep[fa>>1])swap(hep[fa],hep[fa>>1]),fa>>=1;
}
VioletEvergarden get(){
	VioletEvergarden cnt=hep[1];
	int fa=1,son;hep[1]=hep[len--];
	while(fa<<1<=len){
		if(len<(fa<<1|1)||hep[fa<<1]<hep[fa<<1|1])son=fa<<1;else son=fa<<1|1;
		if(hep[son]<hep[fa])swap(hep[fa],hep[son]),fa=son;else break;
	}
	return cnt;
}
void dij(int x){
	memset(vis,0,sizeof vis);
	for(int i=1;i<=n;i++)dis[i]=1000000000;dis[x]=0;;
	put((VioletEvergarden){x,dis[x]});
	while(len){
		VioletEvergarden now=get();
		if(vis[now.id])continue;
		vis[now.id]=1;
		for(int i=lnk[now.id];i;i=nxt[i]){
			if(!vis[son[i]]&&dis[now.id]+v[i]<dis[son[i]]){
				dis[son[i]]=dis[now.id]+v[i];
				put((VioletEvergarden){son[i],dis[son[i]]});
			}
		}
	}
}
int main(){
	freopen("P5905.in","r",stdin);
	freopen("P5905.out","w",stdout);
	n=read();m=read();
	for(int i=1;i<=n;i++)make(0,i,0);
	for(int i=1;i<=m;i++){
		int x=read(),y=read(),z=read();
		make(x,y,z);
	}
	if(SPFA()){printf("-1\n");return 0;}
	for(int i=1;i<=n;i++)
		for(int j=lnk[i];j;j=nxt[j])v[j]+=h[i]-h[son[j]];
	for(int i=1;i<=n;i++){
		dij(i);
		for(int j=1;j<=n;j++){
			if(dis[j]==1000000000)ans+=(long long)j*dis[j];
			else ans+=(long long)j*(dis[j]-h[i]+h[j]);
		}
		printf("%lld\n",ans);ans=0;
	}
	return 0;
}

和答案的一个点就差一点点>_<

也翻过讨论区了,没有一样的问题>_<

2023/5/11 22:03
加载中...