首页 > 代码库 > 作业。。

作业。。

  1 A:  2 #include <iostream>  3 #include <cstdio>  4 #include <cstring>  5 #include <algorithm>  6 using namespace std;  7   8 const int maxn = 105;  9 struct Node { 10     int a, b; 11     int val; 12 }node[maxn * maxn]; 13  14 bool cmp(Node n1, Node n2) { 15     return n1.val < n2.val; 16 } 17  18 int fa[maxn]; 19 void init(int x) { 20     for(int i = 0; i <= x; i++) { 21         fa[i] = i; 22     } 23 } 24  25 int find(int x) { 26     if(fa[x] == x) { 27         return fa[x]; 28     } 29     return fa[x] = find(fa[x]); 30 } 31  32 int sum; 33 void unin(int x, int y, int z) { 34     int fx = find(x), fy = find(y); 35     if(fx != fy) { 36         sum += z; 37         fa[fx] = fy; 38     } 39 } 40  41 int main() { 42     int n, m; 43     while(EOF != scanf("%d %d",&n, &m) ) { 44         init(n); 45         for(int i = 0; i < m; i++){ 46             scanf("%d %d %d",&node[i].a, &node[i].b, &node[i].val); 47         } 48         sort(node, node + m, cmp); 49         sum = 0; 50         for(int i = 0; i < m; i++) { 51             unin(node[i].a, node[i].b, node[i].val); 52         } 53         printf("%d\n", sum); 54     } 55     return 0; 56 } 57  58  59 B: 60 #include <iostream> 61 #include <cstdio> 62 #include <cstring> 63 using namespace std; 64  65 const int maxn = 105; 66 int mat[maxn][maxn]; 67  68 int main() { 69     int n, m; 70     int u, v, w; 71     while(EOF != scanf("%d %d",&n, &m) ) { 72         memset(mat, 0x3f, sizeof(mat)); 73         for(int i = 1; i <= m; i++) { 74             scanf("%d %d %d",&u, &v, &w); 75             mat[u][v] = min(mat[u][v], w); 76             mat[v][u] = mat[u][v]; 77         } 78         for(int k = 1; k <= n; k++) { 79             for(int i = 1; i <= n; i++) { 80                 for(int j = 1; j <= n; j++) { 81                     if(k == i || k == j) continue; 82                     if(mat[i][j] > mat[i][k] + mat[k][j]) { 83                         mat[i][j] = mat[i][k] + mat[k][j]; 84                     } 85                 } 86             } 87         } 88         printf("%d\n", mat[1][n]); 89     } 90     return 0; 91 } 92  93  94 C: 95 #include <iostream> 96 #include <cstdio> 97 #include <cstring> 98 using namespace std; 99 100 const int maxn = 1030;101 int a[maxn];102 int n;103 104 void pr(int x) {105     if(x > n) return ;106     printf("%d ", x);107     pr(x << 1);108     pr(x << 1 | 1);109 }110 int main() {111     while(EOF != scanf("%d",&n) ) {112         for(int i = 1; i <= n; i++) {113             a[i] = i;114         }115         pr(1); puts("");116     }117     return 0;118 }119 120 121 D:122 #include <iostream>123 #include <cstdio>124 #include <cstring>125 using namespace std;126 127 const int maxn = 1030;128 int a[maxn];129 int n;130 131 void pr(int x) {132     if(x > n) return ;133     pr(x << 1);134     printf("%d ", x);135     pr(x << 1 | 1);136 }137 int main() {138     while(EOF != scanf("%d",&n) ) {139         for(int i = 1; i <= n; i++) {140             a[i] = i;141         }142         pr(1); puts("");143     }144     return 0;145 }146 147 148 149 E:150 #include <iostream>151 #include <cstdio>152 #include <cstring>153 using namespace std;154 155 const int maxn = 1030;156 int a[maxn];157 int n;158 159 void pr(int x) {160     if(x > n) return ;161     pr(x << 1);162     pr(x << 1 | 1);163     printf("%d ", x);164 }165 int main() {166     while(EOF != scanf("%d",&n) ) {167         for(int i = 1; i <= n; i++) {168             a[i] = i;169         }170         pr(1); puts("");171     }172     return 0;173 }
View Code

 

作业。。