正文
增加 217 行删除 13 行
12345678910111213141516171819202122232425262728293031323334353637383940133 行未改动全部展开174175176177各种模板(持续更新中)快读快写```cppnamespace FastIO{template<typename type>inline void read(type&x){x=0;static bool flag(0);char ch=getchar();while(ch<'0'||ch>'9'){flag=ch=='-';ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}flag?x=-x:0;}template<typename type>inline void write(type x,bool mode=1){x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);do{Stack[++top]=x%10;x/=10;}while(x);while(top){putchar(Stack[top--]|48);}mode?putchar('\n'):putchar(' ');}}```线段树```cppnamespace SegmentTree{const int MAXN=100005;//根据实际题目数据规模修改const int INF=0x3f3f3f3f3f3f3f3f;//用于初始化最小值(long long下,如使用int就更改)133 行未改动全部展开}}```[线段树模板1](https://www.luogu.com.cn/record/295651807)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667133 行未改动全部展开201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381各种模板(持续更新中)**快读快写**---```cppnamespace FastIO{template<typename type>inline void read(type&x){x=0;bool neg=0;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-'){neg=1;}if(ch==EOF){return;}ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}neg?x=-x:0;}inline string reads(){string str="";char ch=getchar();while(ch==' '||ch=='\n'||ch=='\r'){ch=getchar();}while(ch!=' '&&ch!='\n'&&ch!='\r'&&ch!=EOF){str+=ch;ch=getchar();}return str;}template<typename type>inline void write(type x,bool mode=1){if(x<0){putchar('-');x=-x;}char buf[25];int top=0;if(x==0){buf[top++]='0';}else{while(x>0){buf[top++]=(x%10)^48;x/=10;}}while(top--){putchar(buf[top]);}mode?putchar('\n'):putchar(' ');}}```**线段树**---```cppnamespace SegmentTree{const int MAXN=100005;//根据实际题目数据规模修改const int INF=0x3f3f3f3f3f3f3f3f;//用于初始化最小值(long long下,如使用int就更改)133 行未改动全部展开}}```[线段树模板1](https://www.luogu.com.cn/record/295651807)**数学相关**---```cppinline int modpow(int a,int b,int mod){int ans=1;while(b){if(b&1){ans*=a;ans=(ans%mod+mod)%mod;}a*=a;a=(a%mod+mod)%mod;b>>=1;}return ans;}``````cppinline int exgcd(int a,int b,int &x,int &y){if(!b){x=1;y=0;return a;}int d=exgcd(b,a%b,y,x);y-=a/b*x;return d;}```**Dijkstra**---```cpp#include<bits/stdc++.h>using namespace std;const int mxn=1e6+5;using pii=pair<int,int>;int n,m,s,dist[mxn];vector<pii>g[mxn];priority_queue<pii,vector<pii>,greater<pii>>pq;inline void Dijkstra(int ss){for(int i=1;i<=n;i++){dist[i]=0x3f3f3f3f;}dist[ss]=0;pq.push(make_pair(0,ss));while(!pq.empty()){pii p=pq.top();pq.pop();int u=p.first;int v=p.second;if(u!=dist[v]){continue;}for(int j=0;j<g[v].size();j++){int x=g[v][j].first,y=u+g[v][j].second;if(dist[x]>y){dist[x]=y;pq.push(make_pair(y,x));}}}}void solve(){cin>>n>>m>>s;for(int i=1,u,v,w;i<=m;i++){cin>>u>>v>>w;g[u].push_back(make_pair(v,w));}Dijkstra(s);for(int i=1;i<=n;i++){cout<<dist[i]<<' ';}}int main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);solve();return 0;}```$整数 n,m,s,分别表示点的个数、有向边的个数、出发点的编号$$接下来 m 行每行包含三个整数 u,v,w,表示一条 u→v 的,长度为 w 的边。$[P4779 【模板】单源最短路径(标准版)](https://www.luogu.com.cn/record/225750235)**Kruskal**---```cpp#include<bits/stdc++.h>using namespace std;#define int long longint n,m,u,v,cnt;struct edge{int start,to,val;}Edge[2000005];int father[100005];int ans;int find(int x){if(father[x]==x){return x;}else{father[x]=find(father[x]);return father[x];}}bool cmp(edge a,edge b){return a.val<b.val;}bool Kruskal(){for(int i=1;i<=m;i++){u=find(Edge[i].start);v=find(Edge[i].to);if(u==v){continue;}ans+=Edge[i].val;father[u]=v;cnt++;if(cnt==n-1){break;}}if(cnt!=n-1){return false;}else{return true;}}signed main(){ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);cin>>n>>m;for(int i=1;i<=n;i++){father[i]=i;}for(int i=1;i<=m;i++){cin>>Edge[i].start>>Edge[i].to>>Edge[i].val;}sort(Edge+1,Edge+m+1,cmp);if(Kruskal()==true){cout<<ans;}else if(Kruskal()==false){cout<<"orz";}return 0;}```$第一行包含两个整数 N,M,表示该图共有 N 个结点和 M 条无向边。$$接下来 M 行每行包含三个整数 Xi,Yi,Zi,$$表示有一条长度为 Zi的无向边连接结点 Xi,Yi。$[P3366 【模板】最小生成树](https://www.luogu.com.cn/record/169928993)