P1546-最短网络

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define pb push_back
 4 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 5 #define INF 1000000003
 6 #define ll long long
 7 inline ll read()
 8 {
 9     ll ans = 0;
10     char ch = getchar(), last = ' ';
11     while(!isdigit(ch)) last = ch, ch = getchar();
12     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
13     if(last == '-') ans = -ans;
14     return ans;
15 }
16 inline void write(ll x)
17 {
18     if(x < 0) x = -x, putchar('-');
19     if(x >= 10) write(x / 10);
20     putchar(x % 10 + '0');
21 }
22 int n;
23 int mincost[103];
24 bool used[103];
25 struct edge
26 {
27     int to,cost;
28 };
29 vector<edge> G[103];
30 int V;
31 int MST()
32 {
33     _for(i,0,V)
34     {
35         mincost[i] = INF;
36         used[i] = false;
37     }
38 
39     mincost[0] = 0;
40     int res = 0;
41 
42     while(1)
43     {
44         int v = -1;
45         _for(u,1,V+1)
46         if(!used[u] && (v==-1 || mincost[u] < mincost[v]))
47             v = u;
48 
49         if(v==-1) break;
50         used[v] = true;
51         res += mincost[v];
52 
53         _for(u,0,G[v].size())
54         mincost[G[v][u].to] = min(mincost[G[v][u].to],G[v][u].cost);
55     }
56     return res;
57 }
58 int main()
59 {
60     V = read();
61     _for(i,1,V+1)
62     {
63         _for(j,1,i)
64         {
65             int c = read();
66             G[i].push_back({j,c}),G[j].push_back({i,c});
67         }
68         _for(j,1,V-i+2)
69             read();
70     }
71     write(MST());
72     return 0;
73 }

 

posted @ 2019-09-15 15:39  Asurudo  阅读(169)  评论(0编辑  收藏  举报