#include <string> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <queue> #include <map> #include <cstdio> #include <iomanip> #include <sstream> #include <iostream> #include <cstring> #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define FOREACH(i,t) FORE(i,t) #define REMIN(x,y) (x)=min((x),(y)) #define REMAX(x,y) (x)=max((x),(y)) #define pb push_back #define sz size() #define mp make_pair #define fi first #define se second #define ll long long #define IN(x,y) ((y).find((x))!=(y).end()) #define un(v) v.erase(unique(ALL(v)),v.end()) #define LOLDBG #ifdef LOLDBG #define DBG(vari) cerr<<#vari<<" = "<<vari<<endl; #define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl; #else #define DBG(vari) #define DBG2(v1,v2) #endif #define CZ(x) scanf("%d",&(x)); #define CZ2(x,y) scanf("%d%d",&(x),&(y)); #define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z)); #define ALL(x) (x).begin(),(x).end() #define tests int dsdsf;cin>>dsdsf;while(dsdsf--) #define testss int dsdsf;CZ(dsdsf);while(dsdsf--) using namespace std; typedef pair<int,int> pii; typedef vector<int> vi; template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; } ///////////////////// ////// INPUT //////// //////////////////// typedef unsigned long long ull; class INPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *bufend; void grabBuffer(); public: INPUT() { grabBuffer(); } bool eof() { return bufend==buffer; } char nextChar() { return *bufpos; } inline char readChar(); inline void skipWS(); unsigned readUnsigned(); int readInt(); ull readULL(); }; char INPUT::buffer[INPUT::BUFSIZE]; void INPUT::grabBuffer() { bufpos = buffer; bufend = buffer + fread( buffer, 1,BUFSIZE,stdin); } char INPUT::readChar() { char res = *bufpos++; if(bufpos==bufend) grabBuffer(); return res; } inline bool myisspace(char c) { return c<=' '; } void INPUT::skipWS() { while(!eof() && myisspace(nextChar())) readChar(); } unsigned INPUT::readUnsigned() { skipWS(); unsigned res = 0; while(!eof() && isdigit(nextChar())) { res = 10u * res + (readChar()-'0'); } return res; } ull INPUT::readULL() { skipWS(); ull res = 0; while(!eof() && isdigit(nextChar())) { res = 10ULL * res + (readChar()-'0'); } return res; } int INPUT::readInt() { skipWS(); bool neg = false; if(!eof() && nextChar()=='-') { neg=true; readChar(); } int res = static_cast<int>(readUnsigned()); if(neg) res = -res; return res; } ///////////////////// ////// OUTPUT ////// //////////////////// class OUTPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *BUFLIMIT; void flushBuffer(); public: OUTPUT():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {} ~OUTPUT() { flushBuffer(); } inline void operator()(char c); inline void operator()(unsigned x); inline void operator()(int x); inline void operator()(unsigned long long int x); inline void operator()(long long int x); inline void operator()(const char*s); void operator()(const string&s) { operator()(s.c_str()); } template<class A,class B> void operator()(const A& a,const B& b) { operator()(a); operator()(b); } template<class A,class B,class C> void operator()(const A& a,const B& b,const C&c) { operator()(a); operator()(b); operator()(c); } template<class A,class B,class C,class D> void operator()(const A& a,const B& b,const C&c,const D&d) { operator()(a); operator()(b); operator()(c); operator()(d); } template<class A,class B,class C,class D,class E> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); } template<class A,class B,class C,class D,class E,class F> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e,const F&f) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); operator()(f); } }; char OUTPUT::buffer[OUTPUT::BUFSIZE]; void OUTPUT::flushBuffer() { char *p = buffer; while(p < bufpos) { p += fwrite( p,1, bufpos-p,stdout); } bufpos = buffer; } void OUTPUT::operator()(char c) { *bufpos = c; ++bufpos; if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(unsigned x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned>(x)); } void OUTPUT::operator()(unsigned long long int x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(long long int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned long long int>(x)); } void OUTPUT::operator()(const char*s) { while(*s) operator()(*s++); } INPUT input; OUTPUT output; int d[2222][2222]; int opt[2222]; #define INF 1000000002 int main() { int n=input.readInt(); //int n=2000; FOR(i,n) { REP(j,i+1,n) { int x=input.readInt(); //int x=rand()+30000*rand()+(1<<25); d[i][j]=d[j][i]=x; } } ll w=0; opt[0]=INF; REP(i,1,n)opt[i]=d[0][i]; REP(f,1,n) { int best=0; FOR(i,n+1) if (opt[i]<opt[best]) best=i; w+=opt[best]; opt[best]=INF; FOR(i,n+1) if (opt[i]!=INF) REMIN(opt[i],d[best][i]); } output(w); 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | #include <string> #include <vector> #include <algorithm> #include <cmath> #include <set> #include <queue> #include <map> #include <cstdio> #include <iomanip> #include <sstream> #include <iostream> #include <cstring> #define REP(i,x,v)for(int i=x;i<=v;i++) #define REPD(i,x,v)for(int i=x;i>=v;i--) #define FOR(i,v)for(int i=0;i<v;i++) #define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define FOREACH(i,t) FORE(i,t) #define REMIN(x,y) (x)=min((x),(y)) #define REMAX(x,y) (x)=max((x),(y)) #define pb push_back #define sz size() #define mp make_pair #define fi first #define se second #define ll long long #define IN(x,y) ((y).find((x))!=(y).end()) #define un(v) v.erase(unique(ALL(v)),v.end()) #define LOLDBG #ifdef LOLDBG #define DBG(vari) cerr<<#vari<<" = "<<vari<<endl; #define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl; #else #define DBG(vari) #define DBG2(v1,v2) #endif #define CZ(x) scanf("%d",&(x)); #define CZ2(x,y) scanf("%d%d",&(x),&(y)); #define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z)); #define ALL(x) (x).begin(),(x).end() #define tests int dsdsf;cin>>dsdsf;while(dsdsf--) #define testss int dsdsf;CZ(dsdsf);while(dsdsf--) using namespace std; typedef pair<int,int> pii; typedef vector<int> vi; template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; } ///////////////////// ////// INPUT //////// //////////////////// typedef unsigned long long ull; class INPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *bufend; void grabBuffer(); public: INPUT() { grabBuffer(); } bool eof() { return bufend==buffer; } char nextChar() { return *bufpos; } inline char readChar(); inline void skipWS(); unsigned readUnsigned(); int readInt(); ull readULL(); }; char INPUT::buffer[INPUT::BUFSIZE]; void INPUT::grabBuffer() { bufpos = buffer; bufend = buffer + fread( buffer, 1,BUFSIZE,stdin); } char INPUT::readChar() { char res = *bufpos++; if(bufpos==bufend) grabBuffer(); return res; } inline bool myisspace(char c) { return c<=' '; } void INPUT::skipWS() { while(!eof() && myisspace(nextChar())) readChar(); } unsigned INPUT::readUnsigned() { skipWS(); unsigned res = 0; while(!eof() && isdigit(nextChar())) { res = 10u * res + (readChar()-'0'); } return res; } ull INPUT::readULL() { skipWS(); ull res = 0; while(!eof() && isdigit(nextChar())) { res = 10ULL * res + (readChar()-'0'); } return res; } int INPUT::readInt() { skipWS(); bool neg = false; if(!eof() && nextChar()=='-') { neg=true; readChar(); } int res = static_cast<int>(readUnsigned()); if(neg) res = -res; return res; } ///////////////////// ////// OUTPUT ////// //////////////////// class OUTPUT { static const int BUFSIZE = 1<<16; static char buffer[]; char *bufpos; char *BUFLIMIT; void flushBuffer(); public: OUTPUT():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {} ~OUTPUT() { flushBuffer(); } inline void operator()(char c); inline void operator()(unsigned x); inline void operator()(int x); inline void operator()(unsigned long long int x); inline void operator()(long long int x); inline void operator()(const char*s); void operator()(const string&s) { operator()(s.c_str()); } template<class A,class B> void operator()(const A& a,const B& b) { operator()(a); operator()(b); } template<class A,class B,class C> void operator()(const A& a,const B& b,const C&c) { operator()(a); operator()(b); operator()(c); } template<class A,class B,class C,class D> void operator()(const A& a,const B& b,const C&c,const D&d) { operator()(a); operator()(b); operator()(c); operator()(d); } template<class A,class B,class C,class D,class E> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); } template<class A,class B,class C,class D,class E,class F> void operator()(const A& a,const B& b,const C&c,const D&d,const E&e,const F&f) { operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); operator()(f); } }; char OUTPUT::buffer[OUTPUT::BUFSIZE]; void OUTPUT::flushBuffer() { char *p = buffer; while(p < bufpos) { p += fwrite( p,1, bufpos-p,stdout); } bufpos = buffer; } void OUTPUT::operator()(char c) { *bufpos = c; ++bufpos; if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(unsigned x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned>(x)); } void OUTPUT::operator()(unsigned long long int x) { char *old = bufpos; do { *bufpos = char('0' + x % 10u); x /= 10u; ++bufpos; } while(x); reverse(old, bufpos); if(bufpos >= BUFLIMIT) flushBuffer(); } void OUTPUT::operator()(long long int x) { if(x<0) { operator()('-'); x = -x; } operator()(static_cast<unsigned long long int>(x)); } void OUTPUT::operator()(const char*s) { while(*s) operator()(*s++); } INPUT input; OUTPUT output; int d[2222][2222]; int opt[2222]; #define INF 1000000002 int main() { int n=input.readInt(); //int n=2000; FOR(i,n) { REP(j,i+1,n) { int x=input.readInt(); //int x=rand()+30000*rand()+(1<<25); d[i][j]=d[j][i]=x; } } ll w=0; opt[0]=INF; REP(i,1,n)opt[i]=d[0][i]; REP(f,1,n) { int best=0; FOR(i,n+1) if (opt[i]<opt[best]) best=i; w+=opt[best]; opt[best]=INF; FOR(i,n+1) if (opt[i]!=INF) REMIN(opt[i],d[best][i]); } output(w); return 0; } |