//============================================================================ // Name : tests.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <list> using namespace std; class Changer { public: bool start; int value; Changer(int v, bool s){ start = s; value = v; } }; struct compare { bool operator()(Changer const * a, Changer const * b) { return a->value < b->value; } }; class Miasto { public: bool zagranico; int firstValue; int secondValue; bool odwiedzoned; std::list<Miasto*> sasiedzi; Miasto() { firstValue = 0; secondValue = 0; zagranico = 0; odwiedzoned = 0; } void addSasiad(Miasto* m) { sasiedzi.push_front(m); } long long getResult(Miasto* last) { if(zagranico || odwiedzoned) { return 0; } odwiedzoned = 1; long long result=0; std::list<Changer*> changers; long long currentResult = 0; long long bestResult = 0; int bestResultStart = 0; int long bestResultEnd = 0; int mniejsze =0; int wieksze =0; for(auto it=sasiedzi.begin(); it!= sasiedzi.end(); ++it) { result += (*it)->getResult(this); if((*it) != last) { Changer* changer1 = new Changer((*it)->firstValue,true); Changer* changer2 = new Changer((*it)->secondValue,false); changers.push_front(changer1); changers.push_front(changer2); wieksze++; currentResult += (*it)->firstValue; } } bestResult = currentResult; changers.sort(compare()); int lastValue=0; for (auto it=changers.begin(); it!= changers.end(); ++it) { //cout<<wieksze<<" "<<mniejsze<<" "<<(*it)->value<<" "<<lastValue<<endl; currentResult -= wieksze * ((*it)->value - lastValue); currentResult += mniejsze * ((*it)->value - lastValue); lastValue = (*it)->value; if(currentResult < bestResult) { bestResult = currentResult; bestResultStart = (*it)->value; bestResultEnd = (*it)->value; } else if(currentResult == bestResult) { bestResultEnd = (*it)->value; } if((*it)->start)wieksze--; else mniejsze++; } result+=bestResult; firstValue = bestResultStart; secondValue = bestResultEnd; return result; } }; Miasto** miasta; int main() { std::ios_base::sync_with_stdio(false); int n,m; cin>>n>>m; if(m==n) { cout<<0<<endl; return 0; } miasta = new Miasto*[n+1]; for(int k=1; k<=n;k++) { miasta[k] = new Miasto(); } int a, b; for(int k=0; k< n-1;k++) { cin>>a>>b; miasta[a]->addSasiad(miasta[b]); miasta[b]->addSasiad(miasta[a]); } int c; for(int k=1;k<=m;k++) { cin>>c; miasta[k]->firstValue = c; miasta[k]->secondValue = c; miasta[k]->zagranico = true; } long long result = miasta[m+1]->getResult(NULL); cout<<result<<endl; }
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 | //============================================================================ // Name : tests.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <list> using namespace std; class Changer { public: bool start; int value; Changer(int v, bool s){ start = s; value = v; } }; struct compare { bool operator()(Changer const * a, Changer const * b) { return a->value < b->value; } }; class Miasto { public: bool zagranico; int firstValue; int secondValue; bool odwiedzoned; std::list<Miasto*> sasiedzi; Miasto() { firstValue = 0; secondValue = 0; zagranico = 0; odwiedzoned = 0; } void addSasiad(Miasto* m) { sasiedzi.push_front(m); } long long getResult(Miasto* last) { if(zagranico || odwiedzoned) { return 0; } odwiedzoned = 1; long long result=0; std::list<Changer*> changers; long long currentResult = 0; long long bestResult = 0; int bestResultStart = 0; int long bestResultEnd = 0; int mniejsze =0; int wieksze =0; for(auto it=sasiedzi.begin(); it!= sasiedzi.end(); ++it) { result += (*it)->getResult(this); if((*it) != last) { Changer* changer1 = new Changer((*it)->firstValue,true); Changer* changer2 = new Changer((*it)->secondValue,false); changers.push_front(changer1); changers.push_front(changer2); wieksze++; currentResult += (*it)->firstValue; } } bestResult = currentResult; changers.sort(compare()); int lastValue=0; for (auto it=changers.begin(); it!= changers.end(); ++it) { //cout<<wieksze<<" "<<mniejsze<<" "<<(*it)->value<<" "<<lastValue<<endl; currentResult -= wieksze * ((*it)->value - lastValue); currentResult += mniejsze * ((*it)->value - lastValue); lastValue = (*it)->value; if(currentResult < bestResult) { bestResult = currentResult; bestResultStart = (*it)->value; bestResultEnd = (*it)->value; } else if(currentResult == bestResult) { bestResultEnd = (*it)->value; } if((*it)->start)wieksze--; else mniejsze++; } result+=bestResult; firstValue = bestResultStart; secondValue = bestResultEnd; return result; } }; Miasto** miasta; int main() { std::ios_base::sync_with_stdio(false); int n,m; cin>>n>>m; if(m==n) { cout<<0<<endl; return 0; } miasta = new Miasto*[n+1]; for(int k=1; k<=n;k++) { miasta[k] = new Miasto(); } int a, b; for(int k=0; k< n-1;k++) { cin>>a>>b; miasta[a]->addSasiad(miasta[b]); miasta[b]->addSasiad(miasta[a]); } int c; for(int k=1;k<=m;k++) { cin>>c; miasta[k]->firstValue = c; miasta[k]->secondValue = c; miasta[k]->zagranico = true; } long long result = miasta[m+1]->getResult(NULL); cout<<result<<endl; } |