#include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; #define FOR(i,n) for(int i=0;i<n;++i) #define FORB(i,b,n) for(int i=b;i<n;++i) #define REV(i,n) for(int i=n;i>=0;--i) #define FOREACH(T, it, v) for(T::iterator it = v.begin(); it !=v.end(); ++it) #define print(STRING,INT) printf("%s= %d\n", STRING, INT) #define scan(INT) scanf("%d", &INT) typedef unsigned int u32; const int INF = (0u - 11)/2; typedef long long ll; typedef set<int> SI; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef set<long long> SL; typedef map<int, int> MII; int DEBUG_ = 1; void log(char* S, long long a) { if(DEBUG_) { printf("%s= %I64d\n", S, a); } } ll tab[2005][2005]; int main() { set<ll> S; int n, tmp; scan(n); FOR(i,n) { FORB(j,i,n) { scan(tmp); tab[i][i] = INF; tab[i][j+1]=tmp; tab[j+1][i]=tmp; // A.insert(make_pair<int, int>(tmp, i * 2048 + j+1)); } } n++; vector<int> V(n, 0); ll OFFSET = 2048; V[0] = 1; FOR(i,n) { if(!V[i]) { S.insert(tab[0][i]*OFFSET + i); } } int a, b; ll out = 0; FORB(i,1,n) { // cout << i << " " << n << endl; set<ll>::iterator it = S.begin(); while(true) { a = (*it)/OFFSET; b = (*it)%OFFSET; S.erase(it++); if(!V[b]) { out +=a; V[b] = 1; FOR(j,n) if(!V[j]) { S.insert(tab[b][j]*OFFSET + j); } break; } } } cout << out << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | #include <vector> #include <list> #include <map> #include <set> #include <queue> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> using namespace std; #define FOR(i,n) for(int i=0;i<n;++i) #define FORB(i,b,n) for(int i=b;i<n;++i) #define REV(i,n) for(int i=n;i>=0;--i) #define FOREACH(T, it, v) for(T::iterator it = v.begin(); it !=v.end(); ++it) #define print(STRING,INT) printf("%s= %d\n", STRING, INT) #define scan(INT) scanf("%d", &INT) typedef unsigned int u32; const int INF = (0u - 11)/2; typedef long long ll; typedef set<int> SI; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef set<long long> SL; typedef map<int, int> MII; int DEBUG_ = 1; void log(char* S, long long a) { if(DEBUG_) { printf("%s= %I64d\n", S, a); } } ll tab[2005][2005]; int main() { set<ll> S; int n, tmp; scan(n); FOR(i,n) { FORB(j,i,n) { scan(tmp); tab[i][i] = INF; tab[i][j+1]=tmp; tab[j+1][i]=tmp; // A.insert(make_pair<int, int>(tmp, i * 2048 + j+1)); } } n++; vector<int> V(n, 0); ll OFFSET = 2048; V[0] = 1; FOR(i,n) { if(!V[i]) { S.insert(tab[0][i]*OFFSET + i); } } int a, b; ll out = 0; FORB(i,1,n) { // cout << i << " " << n << endl; set<ll>::iterator it = S.begin(); while(true) { a = (*it)/OFFSET; b = (*it)%OFFSET; S.erase(it++); if(!V[b]) { out +=a; V[b] = 1; FOR(j,n) if(!V[j]) { S.insert(tab[b][j]*OFFSET + j); } break; } } } cout << out << endl; return 0; } |