求助最后三个点
  • 板块P1186 玛丽卡
  • 楼主沉鸣cmh
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/8/23 18:13
  • 上次更新2023/11/4 09:19:07
查看原帖
求助最后三个点
334041
沉鸣cmh楼主2021/8/23 18:13

SPFA能打满分吗

#include<bits/stdc++.h>
using namespace std;
int n,m,fi[1005],t,d[2005],last[1005],a[1005],sum,f,o,oo,ooo,f1,b[1005];
bool pd[1005];
struct p{
	int ne,to,s;
}l[1000005];
void add(int x,int y,int z){
	l[++t].ne=fi[x];
	l[t].to=y;
	l[t].s=z;
	fi[x]=t;
}
void spfa(){
	queue<int>q;
	q.push(1),d[1]=0,pd[1]=true;
	while(!q.empty()){
		int u=q.front();q.pop(),pd[u]=false;
		for(int i=fi[u];i;i=l[i].ne){
			int v=l[i].to;if((u==f&&v==f1)||(v==f&&u==f1))continue;
			if(d[v]>d[u]+l[i].s){
				d[v]=d[u]+l[i].s,last[v]=u;
				if(!pd[v])q.push(v),pd[v]=true;
			}
		}
	}
}
inline int read(){
    int Out = 0,f = 1;char ch = getchar();
    while(!isdigit(ch)){if(ch == '-')f = -1;ch = getchar();}
    while(isdigit(ch)){Out = Out * 10 + ch - '0';ch = getchar();}
    return Out * f;
}
int main(){memset(d,63,sizeof(d));
	n=read(),m=read();for(int i=1;i<=m;i++){o=read(),oo=read(),ooo=read();add(o,oo,ooo),add(oo,o,ooo);}
	spfa(),t=0;a[0]=n,b[0]=last[n];
	for(int i=last[n];i>1;i=last[i])a[++t]=i,b[t]=last[i];
	for(int i=0;i<=t;i++)memset(d,63,sizeof(d)),f=b[i],f1=a[i],spfa(),sum=max(sum,d[n]);
	cout<<sum;
	return 0;
}
2021/8/23 18:13
加载中...