搜索
热搜: NOIP OIer 神牛
查看: 335|回复: 1

1406: 最短网络

[复制链接]

主题

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2021-6-14 10:45:44 | 显示全部楼层 |阅读模式
11111111111111
回复

使用道具 举报

主题

帖子

0

积分

新手上路

Rank: 1

积分
0
 楼主| 发表于 2021-6-14 11:00:35 | 显示全部楼层
本帖最后由 Hank 于 2021-6-14 11:17 编辑

First:MST
首先呢,我们一看这题:
图+最短+铺线=MST
(至于MST是吗,BDBS)
所以模板一上,一打,诶,完了!
Second:code
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. #include<string>
  6. #include<cmath>
  7. #include<algorithm>
  8. #include<iomanip>
  9. using namespace std;

  10. #define MAX 1000

  11. int forest[MAX], n, tot;

  12. struct Edge
  13. {
  14.         int from;
  15.         int to;
  16.         int w;
  17. }edges[MAX * MAX];

  18. int find(int x)
  19. {
  20.         if(forest[x] != x)
  21.         {
  22.                 return forest[x] = find(forest[x]);
  23.         }
  24.         else
  25.         {
  26.                 return x;
  27.         }

  28. }

  29. void join(int a, int b)
  30. {
  31.         forest[find(a)] = find(b);
  32. }

  33. bool cmp(Edge a, Edge b)
  34. {
  35.         return a.w < b.w;
  36. }

  37. int main()
  38. {
  39.         cin >> n;
  40.         
  41.         int r = 0;
  42.         
  43.         for(int i = 0;i < n;++i)
  44.         {
  45.                 forest[i] = i;
  46.         }
  47.         
  48.         for(int i = 0;i < n;++i)
  49.         {
  50.                 for(int j = 0;j < n;++j)
  51.                 {
  52.                         int v;
  53.                         
  54.                         cin >> v;
  55.                         
  56.                         if(v)
  57.                         {
  58.                                 r++;
  59.                                 
  60.                                 edges[r].from = i + 1;
  61.                                 edges[r].to = j + 1;
  62.                                 edges[r].w = v;
  63.                         }
  64.                 }
  65.         }
  66.         
  67.         sort(edges, edges + r, cmp);
  68.         
  69.         int e = 0;
  70.         
  71.         for(int i = 0;i < r;++i)
  72.         {
  73.                 if(find(edges[i].from) != find(edges[i].to))
  74.                 {
  75.                         join(edges[i].from, edges[i].to);
  76.                         tot += edges[i].w;
  77.                         e++;
  78.                 }
  79.                
  80.                 if(e == n - 1)
  81.                 {
  82.                         break;
  83.                 }
  84.         }
  85.         
  86.         cout << tot;
  87.         
  88.         return 0;
  89.         
  90. }
复制代码



Third:彩蛋
歌曲必听你必须每次听都不一样Trust me!
code时听有奇效


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

津ICP备19006949号-1 | 津公网安备12010102000465号

快速回复 返回顶部 返回列表