强连通分量板子题全TLE,样例过了,萌新求助
  • 板块P2656 采蘑菇
  • 楼主Belarus
  • 当前回复3
  • 已保存回复3
  • 发布时间2020/11/30 16:27
  • 上次更新2023/11/5 07:02:13
查看原帖
强连通分量板子题全TLE,样例过了,萌新求助
223392
Belarus楼主2020/11/30 16:27

代码如下,请忽略 debug 信息

#include<bits/stdc++.h>
using namespace std;
const int sze=4e5+10;
int n,m,S;
int tot=0,ver[sze],head[sze],nxt[sze],edge[sze];
double mag[sze];
int dfn[sze],low[sze],stk[sze],c[sze],ins[sze],top=0,num=0,cnt=0;
int tc=0,vc[sze],nc[sze],hc[sze],ec[sze];
int d[sze],v[sze];
queue<int> q;
inline void add(int x,int y,int z,double p){
	ver[++tot]=y,edge[tot]=z,mag[tot]=p,nxt[tot]=head[x],head[x]=tot;
}
inline void add_c(int x,int y,int z){
	vc[++tc]=y,ec[tc]=z,nc[tc]=hc[x],hc[x]=tc;
}
void tarjan(int x){
	dfn[x]=low[x]=++num;
	stk[++top]=x,ins[x]=1;
	for(int i=head[x];i;i=nxt[i]){
		int y=ver[i];
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(ins[y]) low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x]){
		int y;cnt++;
		do{
			y=stk[top--];ins[y]=0;
			c[y]=cnt;
		}while(x!=y);
	}
}
inline int spfa(int st,int ed){
	memset(d,0,sizeof(d));
	memset(v,0,sizeof(v));
	q.push(st);v[st]=1;
	cout<<"Pushed:"<<st<<"\n";
	while(!q.empty()){
		int x=q.front();q.pop();
		v[x]=0;
		cout<<"Extracted:"<<x<<"\n";
		for(int i=hc[x];i;i=nc[i]){
			int y=vc[i],z=ec[i];
			if(d[y]<d[x]+z){
				cout<<"Updated d["<<y<<"] from "<<d[y]<<" to "<<d[x]+z<<"\n";
				d[y]=d[x]+z;
				if(!v[y]) q.push(y),v[y]=1,cout<<"Pushed:"<<y<<"\n";
			}
		}
	}
	return d[ed];
}
int main(){
	ios::sync_with_stdio(0);
	//freopen("log.out","w",stdout);
	cin>>n>>m;
	for(int i=1;i<=m;++i){
		int u,v,w;
		double k;
		cin>>u>>v>>w>>k;
		if(u==v) continue;
		add(u,v,w,k);
		cout<<"Added:("<<u<<","<<v<<","<<w<<","<<k<<")\n";
	}
	for(int i=1;i<=n;++i) if(!dfn[i]) tarjan(i);
	for(int i=1;i<=n;++i) cout<<"c["<<i<<"]="<<c[i]<<"\n";
	for(int x=1;x<=n;++x){
		for(int i=head[x];i;i=nxt[i]){
			int y=ver[i];
			if(c[x]==c[y]){
				int tmp=edge[i];
				while(tmp){
					edge[i]+=(int)tmp*mag[i];
					tmp=(int)tmp*mag[i];
				}
				add_c(c[x],c[y]+cnt,(int)edge[i]);
				cout<<"New Added:("<<c[x]<<","<<c[y]+cnt<<","<<edge[i]<<")\n";
			}
			else add_c(c[x],c[y]+cnt,(int)edge[i]),cout<<"New Added:("<<c[x]<<","<<c[y]+cnt<<","<<edge[i]<<")\n";
		}
	}
	for(int i=1;i<=cnt;++i) add_c(i+cnt,i,0),add_c(i,2*cnt+1,0),add_c(i+cnt,2*cnt+1,0),cout<<"New Added:("<<i<<","<<i+cnt<<",0)\nNew Added:("<<i<<","<<2*cnt+1<<",0)\nNew Added:("<<i+cnt<<","<<2*cnt+1<<",0)\n";
	cin>>S;
	cout<<spfa(c[S],2*cnt+1)<<'\n';
	return 0;
}

在这组数据里面死循环了:

5 11
5 2 5 0.3
3 4 9 0.2
1 3 6 0
1 1 0 0.8
5 1 3 0.8
3 2 7 0.5
1 5 3 0.5
3 5 9 0.5
5 3 6 0.2
2 1 4 0.2
4 5 9 0.7
5

求助!

2020/11/30 16:27
加载中...