有大佬能用链式前项星帮蒟蒻一下吗
  • 板块P1396 营救
  • 楼主Azure__
  • 当前回复4
  • 已保存回复4
  • 发布时间2022/2/20 14:31
  • 上次更新2023/10/28 08:04:13
查看原帖
有大佬能用链式前项星帮蒟蒻一下吗
565945
Azure__楼主2022/2/20 14:31

呃,我知道是数组开大了,但是蒟蒻不太会写链式前项星,大佬们能帮个忙吗QwQ。

#include<bits/stdc++.h>
using namespace std;
int n,m,s,t;
int mp[10001][10001];
int minn=INT_MAX,maxx=0;
int que[10001];
bool f[10001];
inline int read()
{
    char ch=getchar();
    int x=0,f=1;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
        f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=(x<<3)+(x<<1)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
int check(int x){
	f[s]=1;
	que[1]=s;
	int head=1,tail=1;
	while(head<=tail){
		for(int i=1;i<=n;i++){
			if(mp[que[head]][i]!=0&&mp[que[head]][i]<=x&&!f[i]){
				tail++;
				f[i]=1;
				que[tail]=i;
				if(que[tail]==t) return 1;
			}
		}
		head++;
	}
	return 0;
}
int main()
{
	n=read();
	m=read();
	s=read();
	t=read();
	for(int i=1;i<=m;i++){
		int u,v,w;
		u=read();
		v=read();
		w=read();
		if(mp[u][v]==0){
	        mp[u][v]=w;
			mp[v][u]=w;		
		} 
		else{
		    mp[u][v]=min(mp[u][v],w);
			mp[v][u]=min(mp[v][u],w);	
		} 
		minn=min(minn,w);
		maxx=max(maxx,w);
	}
	int left=minn,right=maxx,p;
	while(left<=right){
		int mid=(left+right)>>1;
		if(check(mid)){
			right=mid-1;
			p=mid;
		}
		else
		left=mid+1;
		for(int i=1;i<=n;i++){
			f[i]=0;
		}
	}
	printf("%d\n",p);
	return 0;
}
2022/2/20 14:31
加载中...