#include <algorithm> #include <iostream> #include <cassert> #include <vector> #include <cstdio> #include <array> #include <deque> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> namespace { using namespace std; typedef vector<int> vint; typedef long long unsigned ulo; typedef long long lint; void __attribute__((unused)) spacje(int k) { int magic; static stack<int*> stos; while (!stos.empty() && stos.top() <= &magic) stos.pop(); stos.push(&magic); for (int n = k * stos.size(); n--;) printf(" "); } #define BOTH(x) for (int x = 0; x < 2; ++x) #define UPlt(xx, yy) (xx) = min(xx, yy) #define UPgt(xx, yy) (xx) = max(xx, yy) struct in_t { template<class T> operator T () { static bool __attribute__((unused)) dummy = ios_base::sync_with_stdio(false); T x; cin >> x; return x; } }; in_t __attribute__((unused)) in; class Print { ostream & os; bool nfirst; public: Print(ostream & os = cout) : os(os), nfirst(false) {} ~Print() { os << endl; } template<class T> Print & operator , (T const& e) { if (nfirst) os << " "; os << e; nfirst = true; return *this; } }; #define print Print{cout}, #define prerr Print{cerr}, template <class T> struct Range { struct Intit { T x; Intit(T x) : x(x) {} T & operator * () { return x; }; bool operator != (Intit other) { return x < other.x; } void operator ++ () { ++x; } }; T b, e; explicit Range(T n) : b(0), e(n) {} Range(T b, T e) : b(b), e(e) {} Intit begin() { return b; } Intit end() { return e; } }; template <class T> Range<T> range(T n) { return Range<T>(n); } template <class T> Range<T> range(T b, T e) { return Range<T>(b, e); } template <class T> Range<T> inclusive(T b, T e) { return Range<T>(b, e + 1); } #define _ __attribute__((unused)) _ }; #define dprintf if (1) spacje(2), printf struct P { short beg; short end; }; struct PK { int cost; short beg; short end; bool operator < (PK const& pk) const { return tie(cost, beg, end) < tie(pk.cost, pk.beg, pk.end); } }; int main() { lint suma = 0; vector<vint> costs; set<PK> mincosts; short n = in; costs.resize(n+1); for (short beg : range(n+1)) { costs[beg].resize(beg+1); for (short end : inclusive(short(beg+1), n)) { int c = in; costs[beg].push_back(c); mincosts.insert({c, beg, short(end)}); } } vector<vector<bool>> known(n+1, vector<bool>(n+1)); for (short i : range(n+1)) known[i][i] = true; while (!mincosts.empty()) { PK ten = *mincosts.begin(); mincosts.erase(mincosts.begin()); suma += ten.cost; vector<P> tomark = {{ten.beg, ten.end}}; known[ten.beg][ten.end] = true; known[ten.end][ten.beg] = true; while (!tomark.empty()) { vector<P> nowe; auto markadd = [&](short a, short b) { if (a > b) swap(a, b); known[a][b] = true; known[b][a] = true; nowe.push_back({a,b}); mincosts.erase(mincosts.find({costs[a][b], a, b})); }; for (P p : tomark) { vector<bool> & knop = known[p.beg]; vector<bool> & knok = known[p.end]; for (short x : range(n+1)) { bool knopx = knop[x]; bool knokx = knok[x]; if (knopx && !knokx) markadd(p.end, x); if (knokx && !knopx) markadd(p.beg, x); } } tomark = move(nowe); } } print suma; 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | #include <algorithm> #include <iostream> #include <cassert> #include <vector> #include <cstdio> #include <array> #include <deque> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> namespace { using namespace std; typedef vector<int> vint; typedef long long unsigned ulo; typedef long long lint; void __attribute__((unused)) spacje(int k) { int magic; static stack<int*> stos; while (!stos.empty() && stos.top() <= &magic) stos.pop(); stos.push(&magic); for (int n = k * stos.size(); n--;) printf(" "); } #define BOTH(x) for (int x = 0; x < 2; ++x) #define UPlt(xx, yy) (xx) = min(xx, yy) #define UPgt(xx, yy) (xx) = max(xx, yy) struct in_t { template<class T> operator T () { static bool __attribute__((unused)) dummy = ios_base::sync_with_stdio(false); T x; cin >> x; return x; } }; in_t __attribute__((unused)) in; class Print { ostream & os; bool nfirst; public: Print(ostream & os = cout) : os(os), nfirst(false) {} ~Print() { os << endl; } template<class T> Print & operator , (T const& e) { if (nfirst) os << " "; os << e; nfirst = true; return *this; } }; #define print Print{cout}, #define prerr Print{cerr}, template <class T> struct Range { struct Intit { T x; Intit(T x) : x(x) {} T & operator * () { return x; }; bool operator != (Intit other) { return x < other.x; } void operator ++ () { ++x; } }; T b, e; explicit Range(T n) : b(0), e(n) {} Range(T b, T e) : b(b), e(e) {} Intit begin() { return b; } Intit end() { return e; } }; template <class T> Range<T> range(T n) { return Range<T>(n); } template <class T> Range<T> range(T b, T e) { return Range<T>(b, e); } template <class T> Range<T> inclusive(T b, T e) { return Range<T>(b, e + 1); } #define _ __attribute__((unused)) _ }; #define dprintf if (1) spacje(2), printf struct P { short beg; short end; }; struct PK { int cost; short beg; short end; bool operator < (PK const& pk) const { return tie(cost, beg, end) < tie(pk.cost, pk.beg, pk.end); } }; int main() { lint suma = 0; vector<vint> costs; set<PK> mincosts; short n = in; costs.resize(n+1); for (short beg : range(n+1)) { costs[beg].resize(beg+1); for (short end : inclusive(short(beg+1), n)) { int c = in; costs[beg].push_back(c); mincosts.insert({c, beg, short(end)}); } } vector<vector<bool>> known(n+1, vector<bool>(n+1)); for (short i : range(n+1)) known[i][i] = true; while (!mincosts.empty()) { PK ten = *mincosts.begin(); mincosts.erase(mincosts.begin()); suma += ten.cost; vector<P> tomark = {{ten.beg, ten.end}}; known[ten.beg][ten.end] = true; known[ten.end][ten.beg] = true; while (!tomark.empty()) { vector<P> nowe; auto markadd = [&](short a, short b) { if (a > b) swap(a, b); known[a][b] = true; known[b][a] = true; nowe.push_back({a,b}); mincosts.erase(mincosts.find({costs[a][b], a, b})); }; for (P p : tomark) { vector<bool> & knop = known[p.beg]; vector<bool> & knok = known[p.end]; for (short x : range(n+1)) { bool knopx = knop[x]; bool knokx = knok[x]; if (knopx && !knokx) markadd(p.end, x); if (knokx && !knopx) markadd(p.beg, x); } } tomark = move(nowe); } } print suma; return 0; } |