#include<bits/stdc++.h> usingnamespace std; #define maxn 100 #define INF 10000 int edge[maxn][maxn]; int path[maxn]; bool visit[maxn]; intmain() { int n, s; cin >> n >> s; for (int i = 0; i < n; i++) path[i] = INF;
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> edge[i][j]; if (edge[i][j] == 0) edge[i][j] = INF; } } for (int i = 0; i < n; i++) edge[i][i] = 0;
path[s] = 0; for (int i = 1; i < n; i++) { int min1 = -1; for (int j = 0; j < n; j++) { if ((min1 == -1 || path[j] < path[min1]) && visit[j] == false) { min1 = j; } } visit[min1] = true;
#include<bits/stdc++.h> usingnamespace std; #define maxn 10001000 int n, m, s; int head[maxn], to[maxn], edge[maxn], Next[maxn], cnt; int dist[maxn]; bool visit[maxn]; priority_queue<pair<int, int> > q; //前一个数是节点的权值,后一个是终点 // 原本是大根堆,我们将数字取负,变成小根堆 voidadd(int x, int y, int z) { edge[++cnt] = z, to[cnt] = y, Next[cnt] = head[x], head[x] = cnt; } intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> s; for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; add(x, y, z); } // 开始施法 memset(dist, 0x7f, sizeof(dist)); memset(visit, 0, sizeof(visit)); dist[s] = 0; q.push(make_pair(0, 1)); while (q.empty() == false) { int x = q.top().second; q.pop(); if (visit[x]) continue; visit[x] = true; for (int i = head[x]; i; i = Next[i]) { int y = to[i], z = edge[i]; if (dist[y] > dist[x] + z) { dist[y] = dist[x] + z; q.push(make_pair(-dist[y], y));//负的! } } } for (int i = 1; i <= n; i++) cout << dist[i] << " "; return0; }
#include<bits/stdc++.h> usingnamespace std; // 传送门:https://www.luogu.com.cn/problem/P2330 // 最小生成树+克鲁斯卡尔算法 #define maxn 10000 #define con 100000 int n, m; structnode { int start; int end; int cost; } edge[con]; int father[maxn]; boolcmp(node a, node b) { return a.cost < b.cost; } // int link[maxn]; intfind(int a) { if (father[a] == a) return a; else return father[a] = find(father[a]); } int num_ans, cost_ans; intmain() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; i++) { int remp1, remp2, remp3; cin >> remp1 >> remp2 >> remp3; edge[i].start = remp1, edge[i].end = remp2; edge[i].cost = remp3; } // 好戏登场 sort(edge + 1, edge + 1 + m, cmp); for (int i = 1; i <= n; i++) { father[i] = i; // link[i] = 1; } int link = 0; for (int i = 1; i <= m; i++) { int s = edge[i].start; int e = edge[i].end; if (find(s) != find(e)) { link++; num_ans++; cost_ans = edge[i].cost; //因为数组按照cost值升序,所以最大值一定是最后一个进入mst的路的权值 int fa1 = find(s); int fa2 = find(e); father[fa1] = father[fa2]; // int sum = link[fa1] + link[fa2]; // link[fa1] = link[fa2] = sum; // if (sum == n) // break; } if (link == n - 1) break; } cout << n - 1 << " " << cost_ans;//每个边一次连两个点,所以个数一定是n-1个 return0; }
#include<iostream> usingnamespace std; // 给定三个正整数A,B和C,求A ^B mod C的结果 longlongfastpower(longlong a, longlong b, longlong c) { a %= c; longlong ans = 1; while (b) { a %= c; if (b & 1) ans = (ans*a) % c; b = b / 2; a = (a*a)%c; } return ans; } intmain() { longlong a, b, c; cin >> a >> b >> c; cout << fastpower(a, b, c) << endl; return0; }
#include<bits/stdc++.h> usingnamespace std; structnode { int cost; }; structcmp1 { booloperator()(node a, node b) { return a.cost > b.cost; //从小到大 } }; structcmp2 { booloperator()(int a, int b) { return a > b; //从小到大 } }; intmain() { priority_queue<node, vector<node>, cmp1> a; priority_queue<int, vector<int>, cmp2> b; for (int i = 1; i <= 10; i++) { int remp; cin >> remp; node t; t.cost = remp; a.push(t); b.push(remp); } for (int i = 1; i <= 10; i++) { cout << a.top().cost << " "; a.pop(); } cout << endl; for (int i = 1; i <= 10; i++) { cout << b.top() << " "; b.pop(); } cout << endl; return0; }
区间dp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
区间dp模板 memset(dp, 0, sizeof(dp)); //初始dp数组 for (int len = 2; len <= n; len++) { //枚举区间长度 for (int i = 1; i + len - 1 < n; ++i) { //枚举区间的起点 int j = i + len - 1; //根据起点和长度得出终点 for (int k = i; k <= j; ++k) //枚举最优分割点 //状态转移方程 } }
#include<bits/stdc++.h> usingnamespace std; #define maxn 1000010 // LCA问题 // https://www.luogu.com.cn/problem/P3379 int cnt, n, m, s; int to[maxn], Next[maxn], head[maxn]; int d[maxn], f[maxn][20]; int t; queue<int> q; voidadd(int s, int e)//加边 { to[++cnt] = e, Next[cnt] = head[s], head[s] = cnt; } voidbfs() { q.push(s); d[s] = 1; while (q.empty() == false) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = Next[i]) { int tar = to[i]; if (d[tar]) continue; d[tar] = d[x] + 1;
f[tar][0] = x; for (int j = 1; j <= t; j++) f[tar][j] = f[f[tar][j - 1]][j - 1]; q.push(tar); } } } intlca(int x, int y) { if (d[x] > d[y]) swap(x, y); //默认x小于y for (int i = t; i >= 0; i--) if (d[f[y][i]] >= d[x]) y = f[y][i]; if (x == y) return x; for (int i = t; i >= 0; i--) if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i]; return f[x][0]; } intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> s; t = (int)(log(n) / log(2)) + 1; //层数 for (int i = 1; i < n; i++) { int s, e; cin >> s >> e; add(s, e); add(e, s); } bfs(); for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; cout << lca(a, b)<<endl; } return0; }
#include<bits/stdc++.h> usingnamespace std; // 传送门;https://www.luogu.com.cn/problem/P3386 // 匈牙利算法 #define maxn 10000100 int n, m, e, ans; bool f[1000][1000]; int to[maxn], Next[maxn], head[maxn], cnt; int match[maxn]; bool visit[maxn];
voidadd(int x, int y) { to[++cnt] = y, Next[cnt] = head[x], head[x] = cnt; } booldfs(int x) { for (int i = head[x]; i; i = Next[i]) { int y = to[i]; if (visit[y] == false) { visit[y] = true; if (!match[y] || dfs(match[y])) { match[y] = x; returntrue; } } } returnfalse; }
intmain() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> m >> e; for (int i = 1; i <= e; i++) { int u, v; cin >> u >> v; if (u <= n && v <= m) add(u, v); } ans = 0; for (int i = 1; i <= n; i++) { memset(visit, 0, sizeof(visit)); if (dfs(i)) ans++; } cout << ans << endl; return0; }