void SPFA(int s)
{
memset(dis, 0x3f, sizeof(dis));
queue<int> q;
q.push(s);
vis[s] = true;
dis[s] = 0;
while (!q.empty())
{
int x = q.front();
q.pop();
vis[x] = false;
for (int i = 0; i < g[x].size(); i++)
{
int y = g[x][i];
if (dis[y] > dis[x] + d[x][i])
{
dis[y] = dis[x] + d[x][i];
if (vis[y] == true) continue;
q.push(y);
vis[y] = true;
}
}
}
}
Dijkstra模板:
C++
void Dijkstra()
{
memset(dis, 0x3f, sizeof(dis));
dis[ts] = 0;
for (int i = 1; i <= n; i++)
{
int mindis = 1e9, u;
for (int j = 1; j <= n; j++) if (!vis[j] && dis[j] < mindis) mindis = dis[j], u = j;
vis[u] = 1;
for (int j = 0; j < g[u].size(); j++)
{
int v = g[u][j];
if (dis[v] > dis[u] + d[u][j]) dis[v] = dis[u] + d[u][j];
}
}
}
堆优化Dijkstra模板:
C++
struct Node
{
int x, key;
bool operator < (const Node &b) const
{
return key > b.key;
}
};
priority_queue<Node> q;
void Dijkstra()
{
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
q.push(Node{s, 0});
int dot = 0;
while (!q.empty())
{
int u = q.top().x;
q.pop();
if (vis[u]) continue;
vis[u] = true;
dot++;
if (dot == n) break;
for (int i = 0; i < g[u].size(); i++)
{
int v = g[u][i];
if (dis[v] > dis[u] + d[u][i])
{
dis[v] = dis[u] + d[u][i];
q.push(Node{v, dis[v]});
}
}
}
}
多源最短路Floyd模板:
C++
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= m; i++)
{
int a, b, c;
cin >> a >> b >> c;
dis[a][b] = min(dis[a][b], c);
}
for (int i = 1; i <= n; i++) dis[i][i] = 0;
for (int k = 1; k <= n; k++)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
高精度模板:
C++
struct Bigint
{
int x[5010];
Bigint()
{
memset(x, 0, sizeof(x));
x[0] = 1;
}
void read()
{
cin >> s;
for (int i = 0; i < s.size(); i++) x[s.size() - i] = s[i] - '0';
x[0] = s.size();
}
void print()
{
for (int i = x[0]; i >= 1; i--) cout << x[i];
cout << endl;
}
Bigint operator+(const Bigint &b) const
{
Bigint c = *this;
c.x[0] = max(x[0], b.x[0]);
for (int i = 1; i <= b.x[0]; i++) c.x[i] += b.x[i];
for (int i = 1; i <= c.x[0]; i++)
{
c.x[i + 1] += c.x[i] / 10;
c.x[i] %= 10;
if (c.x[c.x[0] + 1] > 0) c.x[0]++;
}
return c;
}
bool operator<(const Bigint &b) const
{
Bigint c = *this;
if (c.x[0] != b.x[0]) return c.x[0] < b.x[0];
for (int i = c.x[0]; i >= 1; i--) if (c.x[i] != b.x[i]) return c.x[i] < b.x[i];
return true;
}
Bigint operator-(const Bigint &b) const
{
Bigint c = *this;
c.x[0] = max(x[0], b.x[0]);
for (int i = 1; i <= b.x[0]; i++) c.x[i] -= b.x[i];
for (int i = 1; i <= c.x[0]; i++)
if (c.x[i] < 0)
{
c.x[i + 1]--;
c.x[i] += 10;
}
while (c.x[0] > 1 && c.x[c.x[0]] == 0) c.x[0]--;
return c;
}
Bigint operator*(const int &k) const
{
Bigint c = *this;
for (int i = 1; i <= c.x[0]; i++) c.x[i] *= k;
for (int i = 1; i <= c.x[0]; i++)
{
c.x[i + 1] += c.x[i] / 10;
c.x[i] %= 10;
if (c.x[c.x[0] + 1] > 0) c.x[0]++;
}
return c;
}
Bigint operator/(const int &k) const
{
Bigint c = *this;
int yu = 0;
for (int i = c.x[0]; i >= 1; i--)
{
int t = yu * 10 + c.x[i];
c.x[i] = t / k;
yu = t % k;
}
while (c.x[c.x[0]] == 0 && c.x[0] > 1) c.x[0]--;
return c;
}
};
快速幂模板:
C++
int fp(int a, int b, int p)
{
int mul = 1;
while (b)
{
if (b & 1) mul = 1ll * mul * a % p;
a = 1ll * a * a % p;
b >>= 1;
}
return mul;
}
快速排序模板:
C++
void qsort(int l, int r)
{
int m = (l + r) / 2;
int am = a[m];
int i = l, j = r;
while (i <= j)
{
while (a[i] < am) i++;
while (a[j] > am) j--;
if (i <= j)
{
swap(a[i], a[j]);
i++;
j--;
}
}
if (l < j) qsort(l, j);
if (i < r) qsort(i, r);
}
归并排序模板:
C++
void msort(int l, int r)
{
if (l == r) return;
int m = (l + r) / 2;
int i = l, j = m + 1, k = l;
msort(l, m);
msort(m + 1, r);
while (i <= m && j <= r)
{
if (a[i] <= a[j]) ans[k++] = a[i++];
else
{
ans[k++] = a[j++];
cnt += m - i + 1;
}
}
while (i <= m) ans[k++] = a[i++];
while (j <= r) ans[k++] = a[j++];
for (int i = l; i <= r; i++) a[i] = ans[i];
}
堆模板:
C++
int heap[NR];
void up(int x)
{
if (x == 1) return;
if (heap[x] <= heap[x / 2]) return;
swap(heap[x], heap[x / 2]);
up(x / 2);
}
void down(int x)
{
int ls = 2 * x, rs = 2 * x + 1, id = x;
if (ls <= heap[0] && heap[ls] > heap[id]) id = ls;
if (rs <= heap[0] && heap[rs] > heap[id]) id = rs;
if (id == x) return;
swap(heap[id], heap[x]);
down(id);
}
高斯消元模板:
C++
void Gauss()
{
int row = 1;
for (int j = 1; j <= n; j++)
{
int i = row;
while (i <= n && abs(a[i][j]) < eps) i++;
if (i > n) continue;
swap(a[i], a[row]);
for (i = row + 1; i <= n; i++)
{
double p = a[i][j] / a[row][j];
for (int k = j; k <= n + 1; k++) a[i][k] -= a[row][k] * p;
}
row++;
}
if (row <= n)
{
for (int i = row; i <= n; i++)
if (abs(a[i][n + 1]) > eps)
{
puts("-1");
exit(0);
}
puts("0");
exit(0);
}
for (int i = n; i >= 1; i--)
{
for (int j = i + 1; j <= n; j++)
a[i][n + 1] -= x[j] * a[i][j];
x[i] = a[i][n + 1] / a[i][i];
}
for (int i = 1; i <= n; i++)
printf("x%d=%.2lf\n", i, x[i]);
}