跳到正文

个人记录

负边权的小玩具

NegetiveEdgeWeight约 21 分钟
原文

这是 2026年1月26日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

主要是自用。需要的可以自取。

缺省源(6.31KB)

自带 freopen,计时器,随机数生成器,c++17 基数排序和 gcd。

C++
#include<bits/stdc++.h>
#include<immintrin.h>
#pragma GCC target("avx2")
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/trie_policy.hpp>
#include<ext/pb_ds/list_update_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/rope>
#define For(a,b,c) for(int a=b;a<=c;a++)
#define Fro(a,b,c) for(int a=b;a>=c;a--)
#define ms(a) memset(a,0,sizeof a)
#define msinf(a) memset(a,0x3f,sizeof a)
#define Pii pair<int,int>
#define Inline __inline__
#define fi first
#define se second
#define y1 y1ovo
#define y2 y2ovo
#define j1 j1ovo
#define j2 j2ovo
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define Int signed
#define uint unsigned
#define ll long long
#define ull unsigned long long
#define lll __int128
#define ulll __uint128_t
#define lld long double
#define llld __float128
#define Ceil(a,b) ceil(1.0*a/b)
#define Floor(a,b) floor(1.0*a/b)
#define stdpq std::priority_queue
#define pbpq __gnu_pbds::priority_queue
#define Fopen(in) freopen(in".in","r",stdin);freopen(in".out","w",stdout);
#define Happy io.flush();
#define Ending return cerr<<"\nTime Used: "<<fixed<<setprecision(0)<<timer.getTime()<<" ms",0
#define LL
#ifdef LL
#define int long long
#define inf 0x3f3f3f3f3f3f3f3f
#else
#define inf 0x3f3f3f3f
#endif
using namespace std;using namespace __gnu_pbds;using namespace __gnu_cxx;
namespace fast_IO{const int BS=1<<20;char ib[BS],ob[BS],*p1=ib,*p2=ib,*p3=ob;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2&&(p2=(p1=ib)+fread(ib,1,BS,stdin),p1==p2)?EOF:*p1++))
#define putchar(x) ((p3==ob+BS&&(fwrite(ob,p3-ob,1,stdout),p3=ob),*p3++=x))
#endif
#define isd(c) (c>47&&c<58)
#define iss(c) (c<33)
#define cin io
#define cout io
template<typename T>inline bool read(T&s){s=0;int w=1;char c;while(c=getchar(),!isd(c)&&c!=EOF)if(c=='-')w=-1;if(c==EOF)return 0;while(isd(c))s=s*10+c-48,c=getchar();return s*=w,1;}inline bool read(char&s){while(s=getchar(),iss(s));return s!=EOF;}inline bool read(char*s){char c;while(c=getchar(),iss(c));if(c==EOF)return 0;while(!iss(c))*s++=c,c=getchar();return *s=0,1;}inline bool read(std::string&s){s="";char c;while(c=getchar(),iss(c));if(c==EOF)return 0;while(!iss(c))s+=c,c=getchar();return 1;}template<typename T>inline bool read_f(T&x){x=0;T w=1;char c;while(c=getchar(),!isd(c)&&c!=EOF)if(c=='-')w=-1;if(c==EOF)return 0;while(isd(c))x=x*10+(c^48),c=getchar();if(c=='.'){T f=0,k=1;while(c=getchar(),isd(c))f=f*10+(c^48),k*=10;x+=f/k;}x*=w;return 1;}inline bool read(float&x){return read_f(x);}inline bool read(double&x){return read_f(x);}inline bool read(long double&x){return read_f(x);}template<size_t N>inline bool read(std::bitset<N>&b){b.reset();char c;while(c=getchar(),c!='0'&&c!='1'&&c!=EOF);if(c==EOF)return 0;static char buf[N+5];int i=0;do{buf[i++]=c;c=getchar();}while((c=='0'||c=='1')&&i<N);For(j,0,i-1)if(buf[j]=='1')b[i-1-j]=1;return 1;}template<typename T>inline void write(T x){if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+48);}inline void write(char x){putchar(x);}inline void write(char*x){while(*x)putchar(*x++);}inline void write(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}inline void write(std::string x){for(char c:x)putchar(c);}template<typename T>inline void write(T x,int k){if(x<0)putchar('-'),x=-x;T e=0.5;For(i,1,k)e/=10;x+=e;ull n=(ull)x;write(n);if(k>0){putchar('.');x-=n;For(i,1,k){x*=10;int c=(int)x;putchar(c+48);x-=c;}}}[[deprecated("Float detected: use write(x,k) for precision control")]]inline void write(float x){write(x,6);}[[deprecated("Float detected: use write(x,k) for precision control")]]inline void write(long double x){write(x,6);}[[deprecated("Float detected: use write(x,k) for precision control")]]inline void write(double x){write(x,6);}template<size_t N>inline void write(std::bitset<N>b){write(b.to_string());}struct F{bool s=1;inline void flush(){fwrite(ob,p3-ob,1,stdout);p3=ob;}~F(){flush();}explicit operator bool()const{return s;}}io;template<typename T>inline F&operator>>(F&i,T&b){i.s=read(b);return i;}template<typename T>inline F&operator<<(F&i,T b){write(b);return i;}}
using namespace fast_IO;
template<typename T> using Set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;template<typename T,typename TT> using Map=gp_hash_table<T,TT>;
void freopen(string name="test",int type=1,string aaa="",string bbb=""){if(type==1){freopen((name+".in").c_str(),"r",stdin),freopen((name+".out").c_str(),"w",stdout);}else if(type==2){freopen((name+aaa).c_str(),"r",stdin),freopen((name+bbb).c_str(),"w",stdout);}else if(type==3){freopen((name+aaa).c_str(),"r",stdin),freopen((name+bbb).c_str(),"wb",stdout);}else if(type==4){freopen((name+aaa).c_str(),"wb",stdout);}else{return;}}
class Timer{private:ull GetCycleCount(){unsigned hi,lo;__asm__ volatile("rdtsc":"=a"(lo),"=d"(hi));return ((ull)(hi)<<32)|lo;}ull startCycle,startTime;public:void restart(){startCycle=GetCycleCount();startTime=chrono::steady_clock::now().time_since_epoch().count();}ull getCycles(){return GetCycleCount()-startCycle;}double getTime(){return (chrono::steady_clock::now().time_since_epoch().count()-startTime)/1e6;}}timer;
class FastRand{private:ull s[4];ull rotl(const ull x,int k){return(x<<k)|(x>>(64-k));}ull next(){const ull res=rotl(s[0]+s[3],23)+s[0];const ull t=s[1]<<17;s[2]^=s[0];s[3]^=s[1];s[1]^=s[2];s[0]^=s[3];s[2]^=t;s[3]=rotl(s[3],45);return res;}public:FastRand(){mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());For(i,0,3)s[i]=rng();}int randint(int l,int r){ull range=(ull)r-l+1,v;if(range==0)return next();ull mask=(1ULL<<(64-__builtin_clzll(range-1)))-1;while((v=(next()&mask))>=range);return v+l;}double frand(double l,double r){const ull x=next();const union {ull i;double d;}u={.i=0x3FF0000000000000ULL|(x>>12)};return (u.d-1.0)*(r-l)+l;}}Rand;
ll gcd(ll aa,ll bb){ll a=aa,b=bb;if(!a||!b)return a|b;ll shift=__builtin_ctzll(a|b);a>>=__builtin_ctzll(a);while(b){b>>=__builtin_ctzll(b);if(a>b)swap(a,b);b-=a;}return a<<shift;}Int gcd(Int aa,Int bb){Int a=aa,b=bb;if(!a||!b)return a|b;Int shift=__builtin_ctz(a|b);a>>=__builtin_ctz(a);while(b){b>>=__builtin_ctz(b);if(a>b)swap(a,b);b-=a;}return a<<shift;}
constexpr Int N=5e5+5,MOD=998244353;
#if __cplusplus > 201703L
namespace Radix{int c[65536];template<typename T>void run(T* a,int n){static T t[N];T *x=a,*y=t,m=(T)1<<(sizeof(T)*8-1);For(i,0,n-1)x[i]^=m;for(int p=0;p<(int)sizeof(T)*8;p+=16){ms(c);For(i,0,n-1)c[(x[i]>>p)&0xFFFF]++;For(i,1,65535)c[i]+=c[i-1];Fro(i,n-1,0)y[--c[(x[i]>>p)&0xFFFF]]=x[i];swap(x,y);}if(x!=a)For(i,0,n-1)a[i]=x[i];For(i,0,n-1)a[i]^=m;}}template<typename T,typename Cmp=std::less<T>>void Sort(T* st,T* ed,Cmp cmp=Cmp()){int n=ed-st;if(n<2)Happy;if(n<131072||!is_integral_v<T>||!is_same_v<Cmp,std::less<T>>){sort(st,ed,cmp);Happy;}Radix::run(st,n);}
#endif

Int main(){
	timer.restart();
	
	Happy Ending;
}
快读

支持读入和输出指定位数的 float、double、long double,所有整数,bitset和string。使用 cin 和 cout 进行输入输出,有 bool 返回值。若开启 -DONLINE_JUDGE 在控制台输入样例后要 CTRL_Z。

C++
namespace fast_IO{const int BS=1<<20;char ib[BS],ob[BS],*p1=ib,*p2=ib,*p3=ob;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2&&(p2=(p1=ib)+fread(ib,1,BS,stdin),p1==p2)?EOF:*p1++))
#define putchar(x) ((p3==ob+BS&&(fwrite(ob,p3-ob,1,stdout),p3=ob),*p3++=x))
#endif
#define isd(c) (c>47&&c<58)
#define iss(c) (c<33)
#define cin io
#define cout io
template<typename T>inline bool read(T&s){s=0;int w=1;char c;while(c=getchar(),!isd(c)&&c!=EOF)if(c=='-')w=-1;if(c==EOF)return 0;while(isd(c))s=s*10+c-48,c=getchar();return s*=w,1;}inline bool read(char&s){while(s=getchar(),iss(s));return s!=EOF;}inline bool read(char*s){char c;while(c=getchar(),iss(c));if(c==EOF)return 0;while(!iss(c))*s++=c,c=getchar();return *s=0,1;}inline bool read(std::string&s){s="";char c;while(c=getchar(),iss(c));if(c==EOF)return 0;while(!iss(c))s+=c,c=getchar();return 1;}template<typename T>inline bool read_f(T&x){x=0;T w=1;char c;while(c=getchar(),!isd(c)&&c!=EOF)if(c=='-')w=-1;if(c==EOF)return 0;while(isd(c))x=x*10+(c^48),c=getchar();if(c=='.'){T f=0,k=1;while(c=getchar(),isd(c))f=f*10+(c^48),k*=10;x+=f/k;}x*=w;return 1;}inline bool read(float&x){return read_f(x);}inline bool read(double&x){return read_f(x);}inline bool read(long double&x){return read_f(x);}template<size_t N>inline bool read(std::bitset<N>&b){b.reset();char c;while(c=getchar(),c!='0'&&c!='1'&&c!=EOF);if(c==EOF)return 0;static char buf[N+5];int i=0;do{buf[i++]=c;c=getchar();}while((c=='0'||c=='1')&&i<N);For(j,0,i-1)if(buf[j]=='1')b[i-1-j]=1;return 1;}template<typename T>inline void write(T x){if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+48);}inline void write(char x){putchar(x);}inline void write(char*x){while(*x)putchar(*x++);}inline void write(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}inline void write(std::string x){for(char c:x)putchar(c);}template<typename T>inline void write(T x,int k){if(x<0)putchar('-'),x=-x;T e=0.5;For(i,1,k)e/=10;x+=e;ull n=(ull)x;write(n);if(k>0){putchar('.');x-=n;For(i,1,k){x*=10;int c=(int)x;putchar(c+48);x-=c;}}}[[deprecated("Float detected: use write(x,k) for precision control")]]inline void write(float x){write(x,6);}[[deprecated("Float detected: use write(x,k) for precision control")]]inline void write(long double x){write(x,6);}[[deprecated("Float detected: use write(x,k) for precision control")]]inline void write(double x){write(x,6);}template<size_t N>inline void write(std::bitset<N>b){write(b.to_string());}struct F{bool s=1;inline void flush(){fwrite(ob,p3-ob,1,stdout);p3=ob;}~F(){flush();}explicit operator bool()const{return s;}}io;template<typename T>inline F&operator>>(F&i,T&b){i.s=read(b);return i;}template<typename T>inline F&operator<<(F&i,T b){write(b);return i;}}
using namespace fast_IO;
随机数生成器

在随机范围内随机生成若干个数时,效率大约为 mt19937 的1.5倍,且绝对均匀。

C++
class FastRand{private:ull s[4];ull rotl(const ull x,int k){return(x<<k)|(x>>(64-k));}ull next(){const ull res=rotl(s[0]+s[3],23)+s[0];const ull t=s[1]<<17;s[2]^=s[0];s[3]^=s[1];s[1]^=s[2];s[0]^=s[3];s[2]^=t;s[3]=rotl(s[3],45);return res;}public:FastRand(){mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());For(i,0,3)s[i]=rng();}int randint(int l,int r){ull range=(ull)r-l+1,v;if(range==0)return next();ull mask=(1ULL<<(64-__builtin_clzll(range-1)))-1;while((v=(next()&mask))>=range);return v+l;}double frand(double l,double r){const ull x=next();const union {ull i;double d;}u={.i=0x3FF0000000000000ULL|(x>>12)};return (u.d-1.0)*(r-l)+l;}}Rand;