• 北京化工大学数据结构2022/11/24作业 题解


    后天要去最后一场区域赛了,文字先鸽

    打完润了 

    目录

    问题 A: 图的最小生成树-Prim算法

    问题 B: 图的最小生成树-Kruskal算法

    问题 C: 算法7-9:最小生成树

    问题 D: 算法7-15:迪杰斯特拉最短路径算法

    问题 E: 算法7-16:弗洛伊德最短路径算法

    问题 F: 图的最短路径-Floyd算法输出最短路径包含的边


    问题 A: 图的最小生成树-Prim算法

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. #define fer(i,a,b) for(int i=a;i<=b;++i)
    5. #define der(i,a,b) for(int i=a;i>=b;--i)
    6. #define all(x) (x).begin(),(x).end()
    7. #define pll pair
    8. #define et cout<<'\n'
    9. #define xx first
    10. #define yy second
    11. using namespace std;
    12. template <typename _Tp>void input(_Tp &x){
    13. char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
    14. x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
    15. if(f)x=-x;
    16. }
    17. template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
    18. const int N = 110;
    19. int arr[100][100];
    20. int cost[N],adj[N];
    21. signed main(){
    22. int n;
    23. cin>>n;
    24. fer(i,0,n-1){
    25. fer(j,0,n-1){
    26. cin>>arr[i][j];
    27. }
    28. }
    29. fer(i,0,n-1){
    30. if(arr[0][i]!=0){
    31. cout<" "<0][i]<<" "<<"0"<<'\n';
    32. }
    33. else{
    34. cout<" - -"<<'\n';
    35. }
    36. }
    37. }

    问题 B: 图的最小生成树-Kruskal算法

    这题非要这咱手写排序

    巨**

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. #define fer(i,a,b) for(int i=a;i<=b;++i)
    5. #define der(i,a,b) for(int i=a;i>=b;--i)
    6. #define all(x) (x).begin(),(x).end()
    7. #define pll pair
    8. #define et cout<<'\n'
    9. #define xx first
    10. #define yy second
    11. using namespace std;
    12. template <typename _Tp>void input(_Tp &x){
    13. char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
    14. x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
    15. if(f)x=-x;
    16. }
    17. template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
    18. const int N=1000,M=N*2;
    19. struct edg
    20. {
    21. int arc,ver,c;
    22. };
    23. signed main()
    24. {
    25. int n;
    26. cin>>n;
    27. vector g;
    28. for(int i=0;i
    29. {
    30. for(int j=0;j
    31. {
    32. int x;
    33. cin>>x;
    34. if(x)
    35. {
    36. g.push_back({i,j,x});
    37. }
    38. }
    39. }
    40. for(int i=0;isize();i++)
    41. {
    42. for(int j=i+1;jsize();j++)
    43. {
    44. if(g[i].c>g[j].c)
    45. {
    46. swap(g[i],g[j]);
    47. }
    48. }
    49. }
    50. for(int i=0;isize();i++){
    51. cout<<"<"<","<">"<<":"<'\n';
    52. }
    53. }

    问题 C: 算法7-9:最小生成树

    kruskal板子

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. #define fer(i,a,b) for(int i=a;i<=b;++i)
    5. #define der(i,a,b) for(int i=a;i>=b;--i)
    6. #define all(x) (x).begin(),(x).end()
    7. #define pll pair
    8. #define et cout<<'\n'
    9. #define xx first
    10. #define yy second
    11. using namespace std;
    12. template <typename _Tp>void input(_Tp &x){
    13. char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
    14. x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
    15. if(f)x=-x;
    16. }
    17. template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
    18. const int N=1000,M=N*2;
    19. int p[N];
    20. const int INF=1000000007;
    21. struct Edge
    22. {
    23. int a,b,w;
    24. bool operator< (const Edge &W)const
    25. {
    26. return w < W.w;
    27. }
    28. }edges[M];
    29. int find(int x)
    30. {
    31. if (p[x] != x) p[x] = find(p[x]);
    32. return p[x];
    33. }
    34. int n,m;
    35. int kruskal()
    36. {
    37. sort(edges, edges + m);
    38. for (int i = 1; i <= n; i ++ ) p[i]=i;
    39. int res = 0, cnt = 0;
    40. for (int i = 0; i < m; i ++ )
    41. {
    42. int a = edges[i].a, b = edges[i].b, w = edges[i].w;
    43. a = find(a), b = find(b);
    44. if (a != b)
    45. {
    46. p[a] = b;
    47. res += w;
    48. cnt ++ ;
    49. }
    50. }
    51. if (cnt < n - 1) return INF;
    52. return res;
    53. }
    54. signed main(){
    55. cin>>n;
    56. fer(i,1,n){
    57. fer(j,1,n){
    58. int xx;
    59. cin>>xx;
    60. if(xx){
    61. edges[m]={i,j,xx};
    62. m++;
    63. }
    64. }
    65. }
    66. cout<<kruskal();
    67. }

    问题 D: 算法7-15:迪杰斯特拉最短路径算法

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. #define fer(i,a,b) for(int i=a;i<=b;++i)
    5. #define der(i,a,b) for(int i=a;i>=b;--i)
    6. #define all(x) (x).begin(),(x).end()
    7. #define pll pair
    8. #define et cout<<'\n'
    9. #define xx first
    10. #define yy second
    11. using namespace std;
    12. template <typename _Tp>void input(_Tp &x){
    13. char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
    14. x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
    15. if(f)x=-x;
    16. }
    17. template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
    18. const int N=1000,inf=0x3f3f3f3f;
    19. int w[N][N];
    20. int dist[N];
    21. int vis[N];
    22. void dijistra(int n,int be){
    23. memset(dist,0x3f,sizeof dist);
    24. vis[be]=1;
    25. dist[be]=0;
    26. int now=be;
    27. for(int i=0;i-1;i++){
    28. for( int j=0;j
    29. if(w[now][j]==0||vis[j]==1) continue;
    30. dist[j]=min(dist[j],dist[now]+w[now][j]);
    31. }
    32. int ne=0,minn=inf;
    33. for( int j=0;j
    34. if(vis[j]==1) continue;
    35. if(dist[j]
    36. minn=dist[j];
    37. ne=j;
    38. }
    39. }
    40. now=ne;
    41. vis[ne]=1;
    42. }
    43. return ;
    44. }
    45. signed main(){
    46. int n;
    47. int be;
    48. input(n,be);
    49. fer(i,0,n-1){
    50. fer(j,0,n-1){
    51. input(w[i][j]);
    52. }
    53. }
    54. dijistra(n,be);
    55. for(int i=0;i
    56. if(i==be){
    57. continue;
    58. }
    59. else{
    60. cout<" ";
    61. }
    62. }
    63. }

    问题 E: 算法7-16:弗洛伊德最短路径算法

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. #define fer(i,a,b) for(int i=a;i<=b;++i)
    5. #define der(i,a,b) for(int i=a;i>=b;--i)
    6. #define all(x) (x).begin(),(x).end()
    7. #define pll pair
    8. #define et cout<<'\n'
    9. #define xx first
    10. #define yy second
    11. using namespace std;
    12. template <typename _Tp>void input(_Tp &x){
    13. char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
    14. x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
    15. if(f)x=-x;
    16. }
    17. template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
    18. int mat[500][500];
    19. int dis[500][500];
    20. signed main(){
    21. int n;
    22. cin>>n;
    23. memset(dis,0x3f,sizeof dis);
    24. for(int i=0;i
    25. for(int j=0;j
    26. input(mat[i][j]);
    27. if(mat[i][j]!=0)
    28. dis[i][j]=mat[i][j];
    29. }
    30. }
    31. for(int i=0;i
    32. dis[i][i]=0;
    33. for(int k=0;k
    34. for(int i=0;i
    35. for(int j=0;j
    36. if(dis[i][k]+dis[k][j]
    37. dis[i][j]=dis[i][k]+dis[k][j];
    38. }
    39. }
    40. }
    41. for(int i=0;i
    42. for(int j=0;j
    43. cout<" ";
    44. }
    45. cout<<'\n';
    46. }
    47. }

    问题 F: 图的最短路径-Floyd算法输出最短路径包含的边

    1. #include
    2. #define int long long
    3. #define pb push_back
    4. #define fer(i,a,b) for(int i=a;i<=b;++i)
    5. #define der(i,a,b) for(int i=a;i>=b;--i)
    6. #define all(x) (x).begin(),(x).end()
    7. #define pll pair
    8. #define et cout<<'\n'
    9. #define xx first
    10. #define yy second
    11. using namespace std;
    12. template <typename _Tp>void input(_Tp &x){
    13. char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
    14. x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
    15. if(f)x=-x;
    16. }
    17. template <typename _Tp,typename... Args>void input(_Tp &t,Args &...args){input(t);input(args...);}
    18. int dij[101][101];
    19. int path[101][101];
    20. int a[101][101];
    21. signed main(){
    22. int n;
    23. input(n);
    24. fer(i,0,n-1)
    25. fer(j,0,n-1){
    26. int x,y;
    27. input(x,y);
    28. if(x!=-1){
    29. dij[i][j] = x;
    30. path[i][j] = y;
    31. }
    32. }
    33. int x,y;
    34. input(x,y);
    35. int p = path[x][y];
    36. stack<int>s;
    37. s.push(y);
    38. while (p!=x)
    39. {
    40. s.push(p);
    41. p = path[x][p];
    42. }
    43. s.push(x);
    44. cout<'\n';
    45. while(s.size()){
    46. auto t=s.top();
    47. cout<" ";
    48. s.pop();
    49. }
    50. }

  • 相关阅读:
    opencv滤波技术
    php脚本执行timeout
    vscode配置调用visual studio的编译和调试环境
    公钥密码(非对称加密)
    java计算机毕业设计教材订购系统源码+mysql数据库+系统+lw文档+部署
    神经网络的整个过程包括,神经网络的实现过程
    【Python计算机视觉】Python全栈体系(二十六)
    1373. 二叉搜索子树的最大键值和
    MySQL四种备份表的方式
    数据包伪造替换、会话劫持、https劫持之探索和测试
  • 原文地址:https://blog.csdn.net/m0_61735576/article/details/128046163