算法设计与分析(基本操作)
以下是算法设计要求的一些简单算法实现, 仅供参考,
如果想要加补充,可以留言.
大家好好复习.
再加一个prim算法实现:
1 #include <bits/stdc++.h> 2 #define N 50005 3 #define M 1005 4 #define P pair<int,int> 5 using namespace std; 6 struct Node{ 7 int u, val; 8 }; 9 vector<Node> v[N]; 10 int vis[N]; 11 int n, m; 12 int sum = 0; 13 priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > > q; // 优先队列 14 15 void prim(){ 16 q.push(P(0,1)); 17 int ans = 0; 18 while(!q.empty()){ 19 P p = q.top(); 20 q.pop(); 21 if(vis[p.second] == 1) 22 continue; 23 sum += p.first; 24 vis[p.second] = 1; 25 // cout << p.first << p.second << endl; 26 ans ++; 27 if(ans == n) 28 break; 29 for(int i = 0; i < v[p.second].size(); i++){ 30 Node node = v[p.second][i]; 31 if(vis[node.u] == 0){ 32 q.push(P(node.val, node.u)); 33 } 34 } 35 } 36 } 37 38 int main(){ 39 cin >> n >> m; 40 int x, y, z; 41 for(int i = 0; i < m; i++){ 42 cin >> x >> y >> z; 43 v[x].push_back({y, z}); 44 v[y].push_back({x, z}); 45 } 46 prim(); 47 cout << sum << endl; 48 return 0; 49 }
.... 加一个回朔法的装载问题
1 #include <bits/stdc++.h> 2 #define N 100 3 using namespace std; 4 queue<int> q; 5 int n, m; 6 int an [N]; 7 int bestw = 0; 8 int ew = 0; 9 void Push(int val, int i){ 10 if (i == n){ 11 if(val > bestw){ 12 bestw = val; 13 } 14 }else{ 15 q.push(val); 16 } 17 } 18 19 int solve(){ 20 int i = 1; 21 q.push(-1); 22 while(true){ 23 if(ew + an[i] <= m) 24 Push(ew + an[i], i); 25 Push(ew, i); 26 ew = q.front(); 27 q.pop(); 28 if(ew == -1){ 29 if(q.empty()){ 30 return bestw; 31 } 32 q.push(-1); 33 ew = q.front(); 34 q.pop(); 35 i++; 36 } 37 } 38 } 39 40 int main(){ 41 cin >> n >> m; 42 for(int i = 1 ; i <= n; i ++) 43 cin >> an[i]; 44 cout << solve() << endl; 45 return 0; 46 }
二分搜索算法:
1 #include <bits/stdc++.h> 2 #define N 100 3 using namespace std; 4 5 int n,m; 6 int an[N]; 7 8 int bin_search(int l, int r, int val){ 9 int mid; 10 int ll = l, rr = r; 11 while(ll < rr){ 12 mid = (ll + rr) >> 1; 13 if(an[mid] == val){ 14 return mid; 15 }else if(an[mid] < val){ 16 ll = mid + 1; 17 }else{ 18 rr = mid - 1; 19 } 20 } 21 return ll; 22 } 23 24 int main(){ 25 cin >> n >> m;//n表示数组长, m表示要找的数 26 for(int i = 0; i < n; i++) 27 cin >> an[i]; 28 sort(an, an + n); 29 30 int ans = bin_search(0, n - 1, m); 31 cout <<m<<"在排好序的数组的坐标为:"<< ans << endl; 32 return 0; 33 }
合并排序:
1 #include <bits/stdc++.h> 2 #define N 100 3 using namespace std; 4 int an[N]; 5 6 void query(int *xn, int l, int r){ 7 int mid = (l + r) >> 1; 8 for(int i = mid + 1; i <= r; i++){ 9 int ans = xn[i]; 10 int j = i; 11 while(xn[j - 1] > ans && j > l){ 12 xn[j] = xn[j-1]; 13 j --; 14 } 15 xn[j] = ans; 16 } 17 } 18 19 void SumSort(int *xn, int l, int r){ 20 if(l == r){ 21 return ; 22 } 23 int mid = (l + r) >> 1; 24 SumSort(xn, l, mid); 25 SumSort(xn, mid + 1, r); 26 query(xn, l, r); 27 } 28 29 30 int main(){ 31 32 for(int i = 0; i < N; i++){ 33 cin >> an[i]; 34 } 35 36 SumSort(an, 0, N-1); 37 38 for(int i = 0; i < N; i++){ 39 cout << an[i] << " "; 40 } 41 cout << endl; 42 return 0; 43 }
回朔法装载问题:
1 #include <bits/stdc++.h> 2 #define N 100 3 using namespace std; 4 int an[N]; 5 int n,m; 6 int bestw = 0; 7 int cw = 0; 8 int rem = 0; 9 10 void backover(int x){ 11 if(x > n){ 12 bestw = max(bestw, cw); 13 return ; 14 } 15 rem -= an[x]; 16 if(cw + an[x] <= m){ 17 cw += an[x]; 18 backover(x + 1); 19 cw -= an[x]; 20 } 21 if(cw + rem > bestw) 22 backover(x + 1); 23 rem += an[x]; 24 } 25 26 27 int main(){ 28 cin >> n >> m;//n 表示物体的个数, m 表示船的最高承重 29 for(int i = 1; i <= n; i++){ 30 cin >> an[i]; 31 rem += an[i]; 32 } 33 backover(1); 34 cout << bestw << endl; 35 return 0; 36 }
快速排序:
1 #include <bits/stdc++.h> 2 #define N 100 3 using namespace std; 4 5 int n; 6 int an[N]; 7 8 void quicksort(int *an, int l, int r){ 9 if(l >= r){ 10 return ; 11 } 12 13 int ll = l, rr = r; 14 int key = an[l]; 15 16 while(ll < rr){ 17 while(ll < rr && an[rr] >= key){ 18 rr --; 19 } 20 an[ll] = an[rr]; 21 22 while(ll < rr && an[ll] <= key){ 23 ll ++; 24 } 25 an[rr] = an[ll]; 26 } 27 an[ll] = key; 28 quicksort(an, l, ll - 1); 29 quicksort(an, ll + 1, r); 30 } 31 32 int main(){ 33 cin >> n; 34 // n = 10; 35 for(int i = 0; i < n; i ++) 36 cin >> an[i]; 37 38 quicksort(an, 0, n - 1); 39 40 for(int i = 0; i < n; i ++) 41 cout << an[i] << endl; 42 43 return 0; 44 }
最优装载问题:
1 #include <bits/stdc++.h> 2 #define N 100 3 using namespace std; 4 int val[N]; 5 6 int n, m; 7 int main(){ 8 cin >> n >> m; //n 表示多少种物体, m 表示最大承重 9 for(int i = 0; i < n; i ++) 10 cin >> val[i]; 11 sort(val, val + n); 12 int sum = 0; 13 for(int i = 0; i < n; i ++){ 14 sum += val[i]; 15 if(sum > m){ 16 sum -= val[i]; 17 break; 18 } 19 } 20 cout << sum << endl; 21 return 0; 22 }
最大字段和问题:
#include <bits/stdc++.h> #define N 100 using namespace std; int an[N]; int n; int main(){ cin >> n; for(int i = 0; i < n; i++){ cin >> an[i]; } int MA = 0, ans = 0, sum = 0; for(int i = 0; i < n; i ++){ sum += an[i]; sum = max(sum, ans);//时刻更新sum MA = max(sum, MA); } cout << MA << endl; return 0; }
最长公共子序列:
1 #include <bits/stdc++.h> 2 #define N 101 3 using namespace std; 4 int dp[N][N]; 5 stack<char> st; 6 7 void solve(string s, string ss){ 8 int slen = s.length(); 9 int sslen = ss.length(); 10 for (int i = 1; i <= slen; i++){ 11 for (int j = 1; j <= sslen; j++){ 12 if (s[i - 1] == ss[j - 1]){ 13 dp[i][j] = dp[i-1][j-1] + 1; 14 }else{ 15 dp[i][j] = max(dp[i][j-1], dp[i-1][j]); 16 } 17 } 18 } 19 20 cout << dp[slen][sslen] << endl; 21 22 for(int i = slen, j = sslen; i > 0 && j > 0; ){ 23 if(dp[i][j] == dp[i-1][j-1] + 1){ 24 st.push(s[i-1]); 25 i --; 26 j --; 27 }else if(dp[i][j-1] <= dp[i-1][j]){ 28 i --; 29 }else{ 30 j --; 31 } 32 } 33 } 34 35 string s, ss; 36 int main(){ 37 cin >> s >> ss; 38 solve(s, ss); 39 40 while(!st.empty()){ 41 cout << st.top() << " "; 42 st.pop(); 43 } 44 cout << endl; 45 return 0; 46 }
活动安排问题:
1 #include <bits/stdc++.h> 2 #define N 100 3 using namespace std; 4 5 struct Node{ 6 int s, f; 7 int index; 8 }node[N]; 9 10 bool cmp(Node a, Node b){ 11 if(a.f == b.f) 12 return a.s < b.s; 13 return a.f < b.f; 14 } 15 16 int solve(Node *an, int n){ 17 int ans = 0; 18 int end = 0; 19 for(int i = 0; i < n; i ++){ 20 if(node[i].s >= end){ 21 end = node[i].f; 22 printf("%d ", node[i].index); 23 ans ++; 24 } 25 } 26 printf("\n"); 27 return ans; 28 } 29 30 int main(){ 31 int n; 32 cin >> n; 33 for(int i = 0 ; i < n ; i ++){ 34 cin >> node[i].s >> node[i].f; 35 node[i].index = i + 1; 36 } 37 sort(node, node + n, cmp); 38 39 int cnt = solve(node, n); 40 41 cout << cnt << endl; 42 return 0; 43 }
装载问题:
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 int dp[100]; 5 int Dp[20][100]; 6 int n, m; 7 int an[20]; 8 stack<int> st; 9 int main(){ 10 cin >> n >> m;//n 表示物体的个数, m 表示船的最高承重 11 for(int i = 1; i <= n; i++) 12 cin >> an[i]; 13 //0-1背包优化版 14 for(int i = 1; i <= n; i ++){ 15 for(int j = m; j >= an[i]; j--){ 16 dp[j] = max(dp[j], dp[j - an[i]] + an[i]); 17 } 18 } 19 cout << dp[m] << endl; 20 21 //0-1背包普通版 22 for(int i = 1; i <= n; i ++){ 23 for(int j = 1; j <= m; j ++){ 24 if(j < an[i]) 25 Dp[i][j] = Dp[i-1][j]; 26 else 27 Dp[i][j] = max(Dp[i-1][j], Dp[i-1][j-an[i]] + an[i]); 28 } 29 } 30 //普通版输出选择的物体 31 for(int i = n, j = m; i > 0 && j > 0;){ 32 if(Dp[i][j] == Dp[i-1][j - an[i]] + an[i]){ 33 st.push(i); 34 j -= an[i]; 35 i --; 36 }else{ 37 i --; 38 } 39 } 40 41 cout << Dp[n][m] << endl; 42 while(!st.empty()){ 43 cout<<st.top()<<endl; 44 st.pop(); 45 } 46 47 return 0; 48 }
01背包问题:
1 #include <bits/stdc++.h> 2 #define N 100 3 #define M 1000 4 using namespace std; 5 6 int n, m; 7 int w[N], v[N]; 8 int dp[N][M]; 9 int DP[M]; 10 11 int main(){ 12 cin >> n >> m; // n表示物品数量, m 表示总容量 13 for(int i = 1; i <= n; i ++){ 14 cin >> w[i] >> v[i];//w 表示质量 v 表示价值 15 } 16 //第一种方式 17 18 for(int i = 1; i <= n; i ++){ 19 for(int j = 1; j <= m; j++){ 20 if(j < w[i]) 21 dp[i][j] = dp[i-1][j]; 22 else 23 dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]); 24 } 25 } 26 cout << dp[n][m] << endl; 27 28 29 //第二种方式是第一种的优化 30 31 for(int i = 1; i <= n; i ++){ 32 for(int j = m; j >= w[i]; j--){ 33 DP[j] = max(DP[j], DP[j - w[i]] + v[i]); 34 } 35 } 36 cout << DP[m] << endl; 37 38 return 0; 39 }
Kruskal算法:
1 #include <bits/stdc++.h> 2 #define N 50005 3 #define M 1005 4 using namespace std; 5 struct Node{ 6 int u,v,val; 7 }node[N]; 8 9 int fa[M]; 10 int n, m; 11 12 int find(int x){ 13 return fa[x] == x ? x : fa[x] = find(fa[x]); 14 } 15 16 bool cmp(Node a, Node b){ 17 return a.val < b.val; 18 } 19 20 int main(){ 21 cin >> n >> m; 22 for(int i = 0; i < m; i++){ 23 cin >> node[i].u >> node[i].v >> node[i].val; 24 } 25 for(int i = 0; i <= n; i++) 26 fa[i] = i; 27 sort(node, node + m, cmp); 28 int sum = 0, ans = 0; 29 for(int i = 0; i < m; i++){ 30 int xx = node[i].u; 31 int yy = node[i].v; 32 if(find(xx) == find(yy)){ 33 continue; 34 } 35 sum += node[i].val; 36 fa[find(xx)] = find(yy); 37 ans ++; 38 if(ans == m - 1) 39 break; 40 } 41 42 cout << sum << endl; 43 return 0; 44 }