#include<stdio.h> #include<iostream> #include<vector> //#include<string> //#include<map> //#include<complex> //#include<stack> //#include<list> //#include<set> //#include<bitset> //#include<set> //#include<iterator> #include<cmath> //#include<queue> //#include<time.h> //#include<string.h> //#include<fstream> //#include<sstream> #include<algorithm> using namespace std; #define REP( x,y ) for( int x=0; x<(y); x++ ) #define FORD( x,y,z ) for( int x=y; x>=(z); x-- ) #define FOR(x,b,e) for( int x = b; x <= (e); ++x ) #define SIZE(v) (int)v.size() #define ALL(c) c.begin(),c.end() #define VAR(v,n) __typeof(n) v=(n) #define FOREACH(i,c) for( VAR(i,c.begin());i!=c.end();++i ) #define PB push_back #define MP make_pair #define ST first #define ND second #define WRITE( V ){ FOREACH(it,V) cout << *it << ", "; cout << endl; } #define WRITE_ALL(V,s,t) { cout << s << endl; REP( i,SIZE(V) ){ cout << i << " ---- "; FOREACH(it,V[i]) cout << *it+t << ", "; cout << endl; } } #define WRP(p) p.ST << " " << p.ND #define CLEAR( dst,quant ) memset( dst,0, (quant)*sizeof( __typeof(*dst) ) ); #define WAR if( show_help ) #define ENDL(x) REP(crow,(x)) cout << endl; typedef long long LL; const bool show_help = 1; const LL INF = 10000000000001; LL N,M,K,a,b,c,y,t,w,l_zest; const long double EPS = 1e-11; typedef vector<double> VD; typedef pair<double,LL> PDI; typedef pair<double, double> PDD; typedef vector<bool> VB; typedef vector<LL> VI; typedef vector< VI > VVI; typedef pair<LL,LL> PII; //typedef map< PII, bool > MPIIB; typedef pair<LL,LL> PLL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<PII> VPII; struct FAU{ int *p,*w; FAU( int n ) : p(new int[n]), w(new int[n]) { REP(x,n) p[x] = w[x] = -1; } ~FAU(){ delete[] p; delete[] w; } int Find( int x ){ return ( p[x]<0 ) ? x : p[x] = Find( p[x] ); } void Union( int x, int y ){ if( (x = Find(x)) == (y = Find(y)) ) return; if( w[x] > w[y] ) p[y] = x; else p[x] = y; if( w[x] == w[y] ) w[y]++; } }; typedef LL wtype; typedef vector<wtype> VWT; typedef vector<VWT> VVWT; VVI V; VVWT W; struct Edge{ Edge( int a1, int b1, wtype w1 ) : a(a1), b(b1), w(w1){} int a,b; wtype w; bool operator<( const Edge & e1 )const{ return w < e1.w; } }; // funkcja przyjmuje jako parametr graf, a zwraca graf(minimalne drzewo rozpinajace) :) pair<VVI,VVWT> KruskalMst( VVI & V, VVWT & W ){ VVI v(SIZE(V)); VVWT w(SIZE(W)); FAU fau(SIZE(V)); vector<Edge> ed; REP(i,SIZE(V)) REP(k,SIZE(V[i])) ed.PB( Edge( i,V[i][k], W[i][k] ) ); sort( ALL(ed) ); int x,y; REP(i,SIZE(ed)){ if( (x = fau.Find( ed[i].a ) ) != ( y = fau.Find( ed[i].b ) ) ){ fau.Union( ed[i].a, ed[i].b ); v[ed[i].a].PB(ed[i].b); v[ed[i].b].PB(ed[i].a); w[ed[i].a].PB(ed[i].w); w[ed[i].b].PB(ed[i].w); } } return MP( v,w ); } int main(){ ios_base::sync_with_stdio(0); cin >> N; LL **w = new LL*[N+1]; REP(i,N+1) w[i] = new LL[N+1]; REP(i,N+1) REP(k,N+1) w[i][k] = INF; FOR(i,1,N) FOR(k,i,N) cin >> w[i][k]; V = VVI(N+1); REP(i,N+1) REP(k,N+1) V[i].PB(k); // tworze graf pełny W = VVWT(N+1, VWT(N+1,INF)); // towrze wagi w grafie pełnym FOR(i,1,N+1) FOR(k,i,N) W[k][i-1] = ( W[i-1][k] = w[i][k] ); REP(i,N+1){ V[i].erase( V[i].begin() + i ); W[i].erase( W[i].begin() + i ); } // WRITE_ALL(V,"Graf Pełny:",0); // WRITE_ALL(W,"Wagi w grafie pełnym",0); pair<VVI, VVWT> P = KruskalMst( V,W ); LL suma = 0; VVI V2 = P.ST; VVWT W2 = P.ND; REP(i,SIZE(V2)) REP(k,SIZE(V2[i])) suma += W2[ i ][ k ]; suma /= 2; cout << suma << 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 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 | #include<stdio.h> #include<iostream> #include<vector> //#include<string> //#include<map> //#include<complex> //#include<stack> //#include<list> //#include<set> //#include<bitset> //#include<set> //#include<iterator> #include<cmath> //#include<queue> //#include<time.h> //#include<string.h> //#include<fstream> //#include<sstream> #include<algorithm> using namespace std; #define REP( x,y ) for( int x=0; x<(y); x++ ) #define FORD( x,y,z ) for( int x=y; x>=(z); x-- ) #define FOR(x,b,e) for( int x = b; x <= (e); ++x ) #define SIZE(v) (int)v.size() #define ALL(c) c.begin(),c.end() #define VAR(v,n) __typeof(n) v=(n) #define FOREACH(i,c) for( VAR(i,c.begin());i!=c.end();++i ) #define PB push_back #define MP make_pair #define ST first #define ND second #define WRITE( V ){ FOREACH(it,V) cout << *it << ", "; cout << endl; } #define WRITE_ALL(V,s,t) { cout << s << endl; REP( i,SIZE(V) ){ cout << i << " ---- "; FOREACH(it,V[i]) cout << *it+t << ", "; cout << endl; } } #define WRP(p) p.ST << " " << p.ND #define CLEAR( dst,quant ) memset( dst,0, (quant)*sizeof( __typeof(*dst) ) ); #define WAR if( show_help ) #define ENDL(x) REP(crow,(x)) cout << endl; typedef long long LL; const bool show_help = 1; const LL INF = 10000000000001; LL N,M,K,a,b,c,y,t,w,l_zest; const long double EPS = 1e-11; typedef vector<double> VD; typedef pair<double,LL> PDI; typedef pair<double, double> PDD; typedef vector<bool> VB; typedef vector<LL> VI; typedef vector< VI > VVI; typedef pair<LL,LL> PII; //typedef map< PII, bool > MPIIB; typedef pair<LL,LL> PLL; typedef vector<LL> VLL; typedef vector<VLL> VVLL; typedef vector<PII> VPII; struct FAU{ int *p,*w; FAU( int n ) : p(new int[n]), w(new int[n]) { REP(x,n) p[x] = w[x] = -1; } ~FAU(){ delete[] p; delete[] w; } int Find( int x ){ return ( p[x]<0 ) ? x : p[x] = Find( p[x] ); } void Union( int x, int y ){ if( (x = Find(x)) == (y = Find(y)) ) return; if( w[x] > w[y] ) p[y] = x; else p[x] = y; if( w[x] == w[y] ) w[y]++; } }; typedef LL wtype; typedef vector<wtype> VWT; typedef vector<VWT> VVWT; VVI V; VVWT W; struct Edge{ Edge( int a1, int b1, wtype w1 ) : a(a1), b(b1), w(w1){} int a,b; wtype w; bool operator<( const Edge & e1 )const{ return w < e1.w; } }; // funkcja przyjmuje jako parametr graf, a zwraca graf(minimalne drzewo rozpinajace) :) pair<VVI,VVWT> KruskalMst( VVI & V, VVWT & W ){ VVI v(SIZE(V)); VVWT w(SIZE(W)); FAU fau(SIZE(V)); vector<Edge> ed; REP(i,SIZE(V)) REP(k,SIZE(V[i])) ed.PB( Edge( i,V[i][k], W[i][k] ) ); sort( ALL(ed) ); int x,y; REP(i,SIZE(ed)){ if( (x = fau.Find( ed[i].a ) ) != ( y = fau.Find( ed[i].b ) ) ){ fau.Union( ed[i].a, ed[i].b ); v[ed[i].a].PB(ed[i].b); v[ed[i].b].PB(ed[i].a); w[ed[i].a].PB(ed[i].w); w[ed[i].b].PB(ed[i].w); } } return MP( v,w ); } int main(){ ios_base::sync_with_stdio(0); cin >> N; LL **w = new LL*[N+1]; REP(i,N+1) w[i] = new LL[N+1]; REP(i,N+1) REP(k,N+1) w[i][k] = INF; FOR(i,1,N) FOR(k,i,N) cin >> w[i][k]; V = VVI(N+1); REP(i,N+1) REP(k,N+1) V[i].PB(k); // tworze graf pełny W = VVWT(N+1, VWT(N+1,INF)); // towrze wagi w grafie pełnym FOR(i,1,N+1) FOR(k,i,N) W[k][i-1] = ( W[i-1][k] = w[i][k] ); REP(i,N+1){ V[i].erase( V[i].begin() + i ); W[i].erase( W[i].begin() + i ); } // WRITE_ALL(V,"Graf Pełny:",0); // WRITE_ALL(W,"Wagi w grafie pełnym",0); pair<VVI, VVWT> P = KruskalMst( V,W ); LL suma = 0; VVI V2 = P.ST; VVWT W2 = P.ND; REP(i,SIZE(V2)) REP(k,SIZE(V2[i])) suma += W2[ i ][ k ]; suma /= 2; cout << suma << endl; return 0; } |