|
楼主 |
发表于 2021-6-14 11:00:35
|
显示全部楼层
本帖最后由 Hank 于 2021-6-14 11:17 编辑
First:MST
首先呢,我们一看这题:
图+最短+铺线=MST
(至于MST是吗,BDBS)
所以模板一上,一打,诶,完了!
Second:code
- #include<iostream>
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- #include<string>
- #include<cmath>
- #include<algorithm>
- #include<iomanip>
- using namespace std;
- #define MAX 1000
- int forest[MAX], n, tot;
- struct Edge
- {
- int from;
- int to;
- int w;
- }edges[MAX * MAX];
- int find(int x)
- {
- if(forest[x] != x)
- {
- return forest[x] = find(forest[x]);
- }
- else
- {
- return x;
- }
- }
- void join(int a, int b)
- {
- forest[find(a)] = find(b);
- }
- bool cmp(Edge a, Edge b)
- {
- return a.w < b.w;
- }
- int main()
- {
- cin >> n;
-
- int r = 0;
-
- for(int i = 0;i < n;++i)
- {
- forest[i] = i;
- }
-
- for(int i = 0;i < n;++i)
- {
- for(int j = 0;j < n;++j)
- {
- int v;
-
- cin >> v;
-
- if(v)
- {
- r++;
-
- edges[r].from = i + 1;
- edges[r].to = j + 1;
- edges[r].w = v;
- }
- }
- }
-
- sort(edges, edges + r, cmp);
-
- int e = 0;
-
- for(int i = 0;i < r;++i)
- {
- if(find(edges[i].from) != find(edges[i].to))
- {
- join(edges[i].from, edges[i].to);
- tot += edges[i].w;
- e++;
- }
-
- if(e == n - 1)
- {
- break;
- }
- }
-
- cout << tot;
-
- return 0;
-
- }
复制代码
Third:彩蛋
歌曲必听,你必须听一听,而且每次听都不一样,Trust me!
code时听有奇效
|
|