Shortest
Single-source shortest path (1 ~ n)
//SPFA算法
#include<bits/stdc++.h> using namespace std; const int N= 15000; struct bian{ int x,y,d,next; }; int last[N],len,d[N]; int List[N],tail,head,v[N]; bian a[210000]; void ins(int x,int y,int d) { len++; a[len].x=x;a[len].y=y;a[len].d=d; a[len].next=last[x]; last[x]=len; } int main() { int n,m,x,y,c,st,ed; scanf("%d%d",&n,&m); //点,边 for(int i=1;i<=m;i++){ scanf("%d%d%d",&x,&y,&c); ins(x,y,c); ins(y,x,c); } for(int i=1;i<=n;i++)d[i]=9999999999; st=1;ed=n; d[st]=0;v[st]=1; List[1]=st;head=1;tail=2; while(head!=tail){ x=List[head]; for(int k=last[x];k;k=a[k].next){ y=a[k].y; if(d[y]>d[x]+a[k].d){ d[y]=d[x]+a[k].d; if(v[y]==0){ v[y]==1; List[tail]=y; tail++; if(tail==n+1) tail=1; } } } List[head]=0; head++; if(head==n+1)head=1; v[x]=0; } printf("%d",d[n]); }
// Dijsktra bare title number of the title forgotten
#include <the iostream> #include <CString> the using namespace STD; #define INF 0x3f3f3f3f const int N = 150 ; int n-, m, X, Y, Z, P; int A [ N] [N], DIS [N]; BOOL VIS [N]; void Dij ( int X, int Y) { for ( int I = . 1 ; I <= n-; I ++ ) { DIS [I] = A [ . 1 ] [I]; // single-source shortest VIS [I] = to false ; } vis[x] = 1; for(int i = 1; i <= n; i++){ int minn = INF; for(int j = 1; j <= n; j++){ if(!vis[j]&&dis[j]<minn){ minn = dis[j]; p = j; } } vis[p] = 1; for(int j = 1; j <= n; j++){ if(!vis[j]&&dis[p]+a[p][j]<dis[j]) dis[j] = dis[p] + a[p][j]; } } } int main() { while(cin >> n >> m){ if(n == 0 && m == 0) break; memset(a,INF,sizeof(a)); while(m--){ cin >> x >> y >> z; a[x][y] = a[y][x] = z; } dij(1,n); cout << dis[n] << endl; } return 0; }
2. Fast power and fast ride
#include <bits / STDC ++ H.> the using namespace STD; #define int long long // This will not open every time a long long const int N = 100050, China ; inline int Read () { // fast read char C = getchar ( ); int X = 0 , F = . 1 ; the while (C < ' 0 ' || C> ' . 9 ' ) { IF (C == ' - ' ) F = - . 1 ; C= getchar(); } while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } inline int qpow(int a,int b,int mod){ //快速幂 for(int c = 1; ;a = a * a % mod){ if(b & 1)c = c * a % mod; if(!(b >>= 1))return c; } } inline int qmul(int a,int b,int mod){ //快速乘 for(int c = 0; ;a = (a << 1) % mod){ if(b & 1)c = (c + a) % mod; if(!(b >>= 1))return c; } } int n,m,a,b,q,c,d,r; signed t; signed main() { t = read(); for(int i = 1;i <= t; i++){ a = read();b = read();q = read(); c = read();d = read();r = read(); cout << qpow(a,b,q) << " "<< qmul(c,d,r) << endl ; } }
3. dichotomy
int l = 1; int r = d; while (l <= r){ int mid = (l + r) / 2; if (check(mid)){ ans = mid; l = mid + 1; } else r = mid - 1; }
4. precision
High-precision subtraction
#include <bits / STDC ++ H.> the using namespace STD; const int N = 10500 ; int Na [N], Nb [N], NC [N]; BOOL BJ; String A, B; int main () { CIN > > A >> b; IF (a.size () <b.size () || (a.size () == b.size () && A <b)) { // I really was young and ignorant ... string can direct comparison of the ... (the ASCII code BJ = . 1 ; the swap (a, B); } for ( int I = a.size (); I> = . 1 ; I -) Na [I] = a [a.size () -i] - ' 0 ' ; for (int i=b.size();i>=1;i--)nb[i]=b[b.size()-i]-'0'; int n=max(a.size(),b.size()); for(int i = 1; i <= n; i ++){ if(na[i] < nb[i]){ na[i + 1] --; na[i] += 10; } nc[i] = na[i] - nb[i]; } while(nc[n]==0)n--; if(bj)cout<<"-"; for(int i=n;i>0;i--)cout<<nc[i]; if(n<1)cout<<"0"; }
Precision multiplication
#include<iostream> #include<cstring> #define maxn 100000+10 using namespace std; int na[maxn],nb[maxn],nc[maxn]; char a[maxn],b[maxn]; void mul() { int i,j,lena,lenb; memset(na,0,sizeof(na)); memset(nb,0,sizeof(nb)); memset(nc,0,sizeof(nc)); lena=strlen(a); lenb=strlen(b); for(i=0;i<lena;i++) na[i]=a[lena-i-1]-'0'; for(i=0;i<lenb;i++) nb[i]=b[lenb-i-1]-'0'; for(i=0;i<lena;i++) for(j=0;j<lenb;j++) nc[i+j]+=na[i]*nb[j]; int max=lena+lenb; for(i=0;i<max;i++) nc[i+1]+=nc[i]/10,nc[i]%=10; while(!nc[--max]); max++; for(i=0;i<max;i++) a[i]=nc[max-i-1]+'0'; a[max]='\0'; } int main() { while(cin>>a>>b) { mul(); puts(a); } return 0; }
The Euclidean (divided removed)
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a%b); }
6. leftist tree
(P3377 [template] leftist tree (and may heap))
100. mess
// size comparison overloaded operators
struct Node { int len; BOOL operator <( const Node A &) const { // overloaded <operator. May be used for both node <Comparison operators return len < a.len; } };