跳到正文

题解

sb做法

kevinZ99约 29 分钟
原文
文章信息与版本

文章信息

发布
2026年9月14日
收录
2026年9月15日
更新
2026年11月21日
篇幅
约 29 分钟
获赞
1
题目
P17462

版本

  1. v2当前
  2. v1

一个神秘做法。

成功把绿题做成紫题。

你如果是提高以上的选手可以当个乐子看,普及和其他选手就别看了。

有人说,主播主播,你的反图最短路太吃操作了,有没有不建立反图的做法啊,我只能想到答案上届可行性是单调的,可以二分!!!

于是容易想到整体二分预处理,但是我们有 n2n^2 级别的点对!整体二分会跑满,每一次用 dijkstra 判断是 O(n3log⁡n)\mathcal{O}(n^3\log{n}) 的!!!

写上去发现只有 5050 分。

主播主播你这个复杂度太唐了啊,就你还想过!

但是注意到这个路径的长度上限是 10510^{5} 所以考虑用 bitset 套多层分块维护堆,这样是 log⁡wV\log_{w}{V} 的,此时我们复杂度到了最优了,每一次操作就是常数级别的了,接下来考虑剪枝。

说句题外话:其实你用这个堆的实现写正解复杂度是 O(n(n+m)log⁡wV+q)O(n(n+m)\log_{w}{V}+q) 轻松爆标。

考虑 dijkstra 的时候给边的 ll 排序,如果不满足就退出了。

考虑尽可能收集 dijkstra 的信息。

维护 capucap_{u} 表示保持这条路径不变,从起点最晚可以推迟到什么时候出发。

每一次更新就是 capv=min⁡(capu, l−(disu−S))\boxed{cap_v=\min\left(cap_u,\,l-(dis_u-S)\right)} 的。

然后答案的下届 lowx,y=min⁡(lowx,y,capy)low_{x,y}=\min(low_{x,y},cap_{y}) 了。

这个可以大量减少跑最短路的次数。

然后你还可以跑一个传递闭包,来排除一些本身就到达不了的点对。

代码呈现

code
C++
//Author:kevinZ99
#include <bits/stdc++.h>
#define up(a,b,c) for(int (a)=(b);(a)<=(c);++(a))
#define dn(a,b,c) for(int (a)=(b);(a)>=(c);--(a))
#define fst first
#define sed second
#define pref static inline
using namespace std;using hint=__int128;using uhint=unsigned __int128;
using us=unsigned short;using ui=unsigned int;using ll=long long;using ull=unsigned long long;
using db=double;using ldb=long double;using pii=pair<int,int>;using pll=pair<ll,ll>;
using pil=pair<int,ll>;using pli=pair<ll,int>;using vi=vector<int>;using vl=vector<ll>;
using vpi=vector<pii>;using vpl=vector<pll>;using vpil=vector<pil>;using vpli=vector<pli>;
ostream& out128(ostream&,uhint,bool);ostream& operator<<(ostream&,hint);ostream& operator<<(ostream&,uhint);
namespace mystl{
	const int SI=1<<22,SO=1<<20;pref size_t fr(void*p,size_t n){
#ifdef __linux__
		return fread_unlocked(p,1,n,stdin);
#else
		return fread(p,1,n,stdin);
#endif
	}
	unsigned char mp[1<<16];struct Init{Init(){memset(mp,255,sizeof mp);for(unsigned i=0;i<10;++i)for(unsigned j=0;j<10;++j){unsigned char s[2]={static_cast<unsigned char>('0'+i),static_cast<unsigned char>('0'+j)};uint16_t x;memcpy(&x,s,2);mp[x]=static_cast<unsigned char>(i*10+j);}}}init;
	pref unsigned pv(const char*p){uint16_t x;memcpy(&x,p,2);return mp[x];}
	struct IB:streambuf{
		char b[SI];bool eofed=false;IB(){setg(b,b,b);}
		int_type underflow()override{if(gptr()<egptr())return traits_type::to_int_type(*gptr());size_t n=fr(b,SI);if(!n)return traits_type::eof();setg(b,b,b+n);return traits_type::to_int_type(*gptr());}
		inline void commit(char*p){gbump(static_cast<int>(p-gptr()));}
		inline bool refill(char*&p,char*&e){commit(p);if(traits_type::eq_int_type(underflow(),traits_type::eof())){eofed=true;return false;}p=gptr();e=egptr();return true;}
		inline bool head(char*&p,char*&e,bool&neg){eofed=false;for(;;){while(p<e&&static_cast<unsigned char>(*p)<=32)++p;if(p<e)break;if(!refill(p,e))return false;}neg=false;if(*p=='-'||*p=='+'){neg=*p=='-';++p;if(p==e&&!refill(p,e))return false;}return true;}
		template<class T,class U>inline bool slow(T&x,bool neg,char*&p,char*&e){U y=0;bool ok=false;for(;;){while(p<e){unsigned c=static_cast<unsigned char>(*p)-static_cast<unsigned>('0');if(c>9){commit(p);if(!ok)return false;x=neg?static_cast<T>(U(0)-y):static_cast<T>(y);return true;}ok=true;y=static_cast<U>(y*10+c);++p;}if(!refill(p,e)){if(!ok)return false;x=neg?static_cast<T>(U(0)-y):static_cast<T>(y);return true;}}}
		template<class T,class U,int K>inline bool num(T&x){x=0;char*p=gptr(),*e=egptr();bool neg;if(!head(p,e,neg))return false;if(e-p<2*K+1)return slow<T,U>(x,neg,p,e);U y=0;bool ok=false;for(int i=0;i<K;++i){unsigned v=pv(p);if(v>=255)break;ok=true;y=static_cast<U>(y*100+static_cast<U>(v));p+=2;}unsigned c=static_cast<unsigned char>(*p)-static_cast<unsigned>('0');if(c<=9){ok=true;y=static_cast<U>(y*10+c);++p;}commit(p);if(!ok)return false;x=neg?static_cast<T>(U(0)-y):static_cast<T>(y);return true;}
		inline bool read(short&x){return num<short,unsigned short,5>(x);}inline bool read(us&x){return num<us,us,5>(x);}inline bool read(int&x){return num<int,unsigned,5>(x);}inline bool read(unsigned&x){return num<unsigned,unsigned,5>(x);}inline bool read(long&x){return sizeof(long)<=4?num<long,unsigned long,5>(x):num<long,unsigned long,10>(x);}inline bool read(unsigned long&x){return sizeof(unsigned long)<=4?num<unsigned long,unsigned long,5>(x):num<unsigned long,unsigned long,10>(x);}inline bool read(ll&x){return num<ll,ull,10>(x);}inline bool read(ull&x){return num<ull,ull,10>(x);}inline bool read(hint&x){return num<hint,uhint,20>(x);}inline bool read(uhint&x){return num<uhint,uhint,20>(x);}
		inline bool read(char&x){x=0;eofed=false;char*p=gptr(),*e=egptr();for(;;){while(p<e&&static_cast<unsigned char>(*p)<=32)++p;if(p<e){x=*p++;commit(p);return true;}if(!refill(p,e))return false;}}
		inline bool read(string&x){eofed=false;x.clear();char*p=gptr(),*e=egptr();for(;;){while(p<e&&static_cast<unsigned char>(*p)<=32)++p;if(p<e)break;if(!refill(p,e))return false;}for(;;){char*q=p;while(q<e&&static_cast<unsigned char>(*q)>32)++q;x.append(p,static_cast<size_t>(q-p));p=q;if(p<e){commit(p);return true;}if(!refill(p,e))return true;}}
		template<class T>inline bool fp(T&x){x=0;string s;if(!read(s))return false;return cv(s.c_str(),x);}static inline bool cv(const char*s,float&x){char*e=nullptr;x=strtof(s,&e);return e!=s&&*e==0;}static inline bool cv(const char*s,double&x){char*e=nullptr;x=strtod(s,&e);return e!=s&&*e==0;}static inline bool cv(const char*s,long double&x){char*e=nullptr;x=strtold(s,&e);return e!=s&&*e==0;}
	}ib;
	struct OB:streambuf{
		char b[SO];OB(){setp(b,b+SO);}~OB(){sync();}
		int sync()override{size_t n=static_cast<size_t>(pptr()-pbase());if(n&&fwrite(b,1,n,stdout)!=n)return -1;setp(b,b+SO);return 0;}
		int_type overflow(int_type c=traits_type::eof())override{if(sync())return traits_type::eof();if(!traits_type::eq_int_type(c,traits_type::eof()))*pptr()=traits_type::to_char_type(c),pbump(1);return traits_type::not_eof(c);}
		inline void pc(char c){if(pptr()==epptr()&&traits_type::eq_int_type(overflow(),traits_type::eof()))return;*pptr()=c;pbump(1);}inline void ps(const char*s,size_t n){while(n){size_t k=static_cast<size_t>(epptr()-pptr());if(!k){if(traits_type::eq_int_type(overflow(),traits_type::eof()))return;k=static_cast<size_t>(epptr()-pptr());}k=min(k,n);memcpy(pptr(),s,k);pbump(static_cast<int>(k));s+=k;n-=k;}}
	}ob;
	struct FastIn:istream{
		FastIn():istream(&ib){}using istream::operator>>;template<class T>inline FastIn&go(T&x){if(!ib.read(x))setstate(ib.eofed?ios::eofbit|ios::failbit:ios::failbit);else if(ib.eofed)setstate(ios::eofbit);return *this;}inline FastIn&operator>>(short&x){return go(x);}inline FastIn&operator>>(us&x){return go(x);}inline FastIn&operator>>(int&x){return go(x);}inline FastIn&operator>>(unsigned&x){return go(x);}inline FastIn&operator>>(long&x){return go(x);}inline FastIn&operator>>(unsigned long&x){return go(x);}inline FastIn&operator>>(ll&x){return go(x);}inline FastIn&operator>>(ull&x){return go(x);}inline FastIn&operator>>(hint&x){return go(x);}inline FastIn&operator>>(uhint&x){return go(x);}inline FastIn&operator>>(char&x){return go(x);}inline FastIn&operator>>(string&x){return go(x);}inline FastIn&operator>>(float&x){if(!ib.fp(x))setstate(ios::failbit);return *this;}inline FastIn&operator>>(double&x){if(!ib.fp(x))setstate(ios::failbit);return *this;}inline FastIn&operator>>(long double&x){if(!ib.fp(x))setstate(ios::failbit);return *this;}inline FastIn&operator>>(bool&x){int y=0;go(y);x=y!=0;return *this;}
	}fin;
	struct FastOut:ostream{
		FastOut():ostream(&ob){}using ostream::operator<<;template<class U>inline FastOut&uo(U x){char s[64];int n=0;do{s[n++]=static_cast<char>('0'+static_cast<unsigned>(x%10));x/=10;}while(x);while(n)ob.pc(s[--n]);return *this;}template<class T>inline FastOut&so(T x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}typedef typename make_unsigned<T>::type U;U y;if(x<0){ob.pc('-');y=U(0)-static_cast<U>(x);}else y=static_cast<U>(x);return uo(y);}template<class T>inline FastOut&un(T x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}return uo(x);}
		inline FastOut&operator<<(short x){return so(x);}inline FastOut&operator<<(us x){return un(x);}inline FastOut&operator<<(int x){return so(x);}inline FastOut&operator<<(unsigned x){return un(x);}inline FastOut&operator<<(long x){return so(x);}inline FastOut&operator<<(unsigned long x){return un(x);}inline FastOut&operator<<(ll x){return so(x);}inline FastOut&operator<<(ull x){return un(x);}inline FastOut&operator<<(bool x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}ob.pc(x?'1':'0');return *this;}inline FastOut&operator<<(char c){if(width()!=0){static_cast<ostream&>(*this)<<c;return *this;}ob.pc(c);return *this;}inline FastOut&operator<<(const char*s){if(width()!=0){static_cast<ostream&>(*this)<<s;return *this;}ob.ps(s,strlen(s));return *this;}inline FastOut&operator<<(const string&s){if(width()!=0){static_cast<ostream&>(*this)<<s;return *this;}ob.ps(s.data(),s.size());return *this;}inline FastOut&operator<<(float x){static_cast<ostream&>(*this)<<x;return *this;}inline FastOut&operator<<(double x){static_cast<ostream&>(*this)<<x;return *this;}inline FastOut&operator<<(long double x){static_cast<ostream&>(*this)<<x;return *this;}
		inline FastOut&operator<<(hint x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}uhint y;if(x<0){ob.pc('-');y=uhint(0)-static_cast<uhint>(x);}else y=static_cast<uhint>(x);return uo(y);}inline FastOut&operator<<(uhint x){if(width()!=0){static_cast<ostream&>(*this)<<x;return *this;}return uo(x);}FastOut&operator<<(ios_base&(*f)(ios_base&)){f(*this);return *this;}FastOut&operator<<(ostream&(*f)(ostream&)){f(*this);return *this;}FastOut&operator<<(ios&(*f)(ios&)){f(*this);return *this;}
	}fout;
}
ostream& out128(ostream&out,uhint x,bool neg){char s[48];int n=0;do{s[n++]=static_cast<char>('0'+static_cast<unsigned>(x%10));x/=10;}while(x);streamsize w=out.width(0),k=max<streamsize>(0,w-n-neg);char f=out.fill();ios::fmtflags a=out.flags()&ios::adjustfield;if(a==ios::left){if(neg)out.put('-');while(n)out.put(s[--n]);while(k--)out.put(f);}else if(a==ios::internal&&neg){out.put('-');while(k--)out.put(f);while(n)out.put(s[--n]);}else{while(k--)out.put(f);if(neg)out.put('-');while(n)out.put(s[--n]);}return out;}
ostream& operator<<(ostream&out,hint x){bool neg=x<0;return out128(out,neg?uhint(0)-static_cast<uhint>(x):static_cast<uhint>(x),neg);}ostream& operator<<(ostream&out,uhint x){return out128(out,x,false);}
#define cin mystl::fin
#define cout mystl::fout
namespace my{
	const int N=505,Q=500005,V=100005,W0=(V+63)>>6,W1=(W0+63)>>6,inf=1000000000;
	template<typename T>pref T Min(T x,T y){return x<y?x:y;}
	template<typename T>pref T Max(T x,T y){return x>y?x:y;}
	int n,m,q,tot,maxlim;
	int mxout[N];
	struct node{
		int x,lim,w;
	};
	vector<node>g[N];
	int ans[N][N],low[N][N];
	int qx[Q],qy[Q],qs[Q];
	bool needpair[N][N];
	bitset<N>reach[N];
	pii point[N*N],tl[N*N],tr[N*N];
	int dis[N],cap[N],distag[N],done[N],goaltag[N],tim;
	ull b0[W0],b1[W1],top;
	int bt0[W0],bt1[W1];
	int head[V],headtag[V];
	int pre[N],nxt[N],key[N],heaptag[N];
	pref void bit_ins(int x){
		int a=x>>6,b=a>>6;
		ull w=1ull<<(x&63);
		if(bt0[a]!=tim)
			bt0[a]=tim,b0[a]=0;
		if(b0[a]&w)return;
		if(!b0[a]){
			if(bt1[b]!=tim)
				bt1[b]=tim,b1[b]=0;
			if(!b1[b])
				top|=1ull<<b;
			b1[b]|=1ull<<(a&63);
		}
		b0[a]|=w;
	}
	pref void bit_del(int x){
		int a=x>>6,b=a>>6;
		b0[a]&=~(1ull<<(x&63));
		if(!b0[a]){
			b1[b]&=~(1ull<<(a&63));
			if(!b1[b])
				top&=~(1ull<<b);
		}
	}
	pref int bit_min(){
		int b=__builtin_ctzll(top);
		int a=(b<<6)+__builtin_ctzll(b1[b]);
		return (a<<6)+__builtin_ctzll(b0[a]);
	}
	pref void heap_ins(int x,int d){
		if(headtag[d]!=tim)
			headtag[d]=tim,head[d]=0;
		if(!head[d])
			bit_ins(d);
		pre[x]=0;
		nxt[x]=head[d];
		if(head[d])
			pre[head[d]]=x;
		head[d]=x;
		key[x]=d;
		heaptag[x]=tim;
	}
	pref void heap_del(int x){
		int d=key[x],l=pre[x],r=nxt[x];
		if(l)nxt[l]=r;
		else head[d]=r;
		if(r)pre[r]=l;
		heaptag[x]=0;
		if(!head[d])
			bit_del(d);
	}
	pref void heap_push(int x,int d){
		if(heaptag[x]==tim)
			heap_del(x);
		heap_ins(x,d);
	}
	pref int heap_pop(){
		int d=bit_min(),x=head[d];
		heap_del(x);
		return x;
	}
	pref void dijkstra(int start,int times,int L,int R){
		++tim;
		top=0;
		int cnt=0;
		up(i,L,R){
			int y=point[i].sed;
			if(low[start][y]<times)
				goaltag[y]=tim,cnt++;
		}
		if(!cnt)return;
		dis[start]=times;
		cap[start]=inf;
		distag[start]=tim;
		heap_push(start,times);
		while(top){
			int x=heap_pop();
			if(done[x]==tim)continue;
			done[x]=tim;
			int dx=dis[x];
			for(const auto &v:g[x]){
				if(v.lim<dx)break;
				if(done[v.x]==tim)continue;
				int nd=dx+v.w;
				int nc=Min(cap[x],v.lim-(dx-times));
				if(distag[v.x]!=tim||nd<dis[v.x]){
					bool first=distag[v.x]!=tim;
					distag[v.x]=tim;
					dis[v.x]=nd;
					cap[v.x]=nc;
					if(needpair[start][v.x])
						low[start][v.x]=Max(low[start][v.x],nc);
					if(first&&goaltag[v.x]==tim)
						if(!--cnt)return;
					if(nd<=mxout[v.x])
						heap_push(v.x,nd);
				}
			}
		}
	}
	void solve(int l,int r,int L,int R){
		if(l>r||L>R)return;
		int mid=(l+r)>>1,ql=0,qr=0,idx=L;
		while(idx<=R){
			int s=point[idx].fst,j=idx+1;
			while(j<=R&&point[j].fst==s)
				j++;
			bool need=false;
			up(k,idx,j-1)
				if(low[s][point[k].sed]<mid){
					need=true;
					break;
				}
			if(need&&mid<=mxout[s])
				dijkstra(s,mid,idx,j-1);
			up(k,idx,j-1){
				int y=point[k].sed;
				if(low[s][y]>=mid)
					ans[s][y]=mid,tr[++qr]=point[k];
				else
					tl[++ql]=point[k];
			}
			idx=j;
		}
		if(ql)
			__builtin_memcpy(point+L,tl+1,sizeof(pii)*ql);
		if(qr)
			__builtin_memcpy(point+L+ql,tr+1,sizeof(pii)*qr);
		solve(l,mid-1,L,L+ql-1);
		solve(mid+1,r,L+ql,R);
	}
	pref void SOLVE(){
		cin>>n>>m>>q;
		__builtin_memset(low,-1,sizeof(low));
		up(i,1,n)
			mxout[i]=-1,reach[i][i]=1;
		up(i,1,m){
			int x,y,l,t;
			cin>>x>>y>>l>>t;
			maxlim=Max(maxlim,l);
			mxout[x]=Max(mxout[x],l);
			g[x].push_back({y,l,t});
			reach[x][y]=1;
		}
		up(i,1,n)
			sort(g[i].begin(),g[i].end(),
				[](const node &a,const node &b){
					return a.lim>b.lim;
				});
		up(i,1,q){
			cin>>qx[i]>>qy[i]>>qs[i];
			if(qx[i]!=qy[i]&&qs[i]<=mxout[qx[i]])
				needpair[qx[i]][qy[i]]=true;
		}
		up(x,1,n)
			for(const auto &v:g[x])
				if(needpair[x][v.x])
					low[x][v.x]=Max(low[x][v.x],v.lim);
		up(k,1,n)
			up(i,1,n)
				if(reach[i][k])
					reach[i]|=reach[k];
		__builtin_memset(ans,-1,sizeof(ans));
		up(x,1,n)
			up(y,1,n)
				if(x!=y&&needpair[x][y]&&reach[x][y])
					point[++tot]={x,y};
		if(tot)
			solve(0,maxlim,1,tot);
		up(i,1,q){
			int x=qx[i],y=qy[i],s=qs[i];
			if(x==y)
				cout<<"Yes\n";
			else
				cout<<(ans[x][y]>=s?"Yes\n":"No\n");
		}
	}
}
int main(){my::SOLVE();return 0;}
原文
存档评论15 条抓取于