#include <iostream> #include <list> #include <vector> using namespace std; typedef struct droga { int do_miasta, dlugosc; struct droga *next; } droga; void find_times(droga *adj_list[], int num_miast, int from_cell, int min_drogi[], int MIASTA) { int done[MIASTA + 1]; int i, j, found; int min_time, min_time_index, old_time; droga *e; for (i = 1; i <= num_miast; i++) { done[i] = 0; min_drogi[i] = -1; } //cout<<" tu jestem"; min_drogi[from_cell] = 0; for (i = 0; i < num_miast; i++) { min_time = -1; found = 0; // cout <<" tu jestem 1\n"; for (j = 1; j <= num_miast; j++) { // cout<< " dla miasta j="<<j<<" done mamy"<<done[j]<<" min mamy ="<<min_drogi[j]<<"\n"; if (!done[j] && min_drogi[j] >= 0) { if (min_time == -1 || min_drogi[j] < min_time) { min_time = min_drogi[j]; min_time_index = j; found = 1; // cout<<"ustawiam min_tim" <<min_time<<" i indeks na min_time_indedex="<<min_time_index<<"\n"; } } } //cout<<" tu jestem 2\n"; if (!found) break; done[min_time_index] = 1; //cout<<" tu jestem 3\n"; e = adj_list[min_time_index]; while (e) { old_time = min_drogi[e->do_miasta]; if (old_time == -1 || old_time > min_time + e->dlugosc) { // cout<<" zmieni³em dla miasta ="<<e->do_miasta<<" wartosc na "<<min_time + e->dlugosc<<"\n"; min_drogi[e->do_miasta] = min_time + e->dlugosc;} e = e->next; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k; cin>>n; int min_droga[n+1]; int miny[n+1]; int miny_dos[n+1]; vector<vector<int>> odleglosci; droga *adj_list[n + 1]; int num_cases, case_num, i; int num_cells, exit_cell, time_limit, num_edges; int from_cell, to_cell, length; int total, min_time; int zamek; list<int> zamki; int sila; droga *e; for (i=1;i<=n;i++){ cin>>sila; miny[i]=sila; } for (i = 1; i <= n; i++) adj_list[i] = NULL; for (i = 0; i < n-1; i++) { //cout<<i<<"\n"; cin>>from_cell>>to_cell>>length; e = new (droga); if (e == NULL) { fprintf(stderr, "malloc - b³¹d przydzielania pamiêci\n"); exit(1); } e->do_miasta = from_cell; e->dlugosc = length; e->next = adj_list[to_cell]; adj_list[to_cell] = e; e = new (droga); if (e == NULL) { fprintf(stderr, "malloc - b³¹d przydzielania pamiêci\n"); exit(1); } e->do_miasta = to_cell; e->dlugosc = length; e->next = adj_list[from_cell]; adj_list[from_cell] = e; } //cout<< "szukam\n"; vector<int> aa; aa.push_back(0); odleglosci.push_back(aa); for (exit_cell=1;exit_cell<n+1;exit_cell++){ find_times(adj_list, n+1, exit_cell, min_droga,n+1); vector<int> row; row.push_back(0) ; //cout<<"\n"; for (i = 1; i <= n; i++) { // cout<<min_droga[i]<< " "; row.push_back(min_droga[i]);} //cout<<row.size()<<"\n"; odleglosci.push_back(row); } /* for(i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<odleglosci[i][j]<<"\t"; cout <<"\n"; } */ vector<int> dostepne_miny; for (i=1;i<=n;i++){ dostepne_miny.clear(); for (int j=1;j<=n;j++){ miny_dos[j]=miny[j]; } dostepne_miny.push_back(i); while(dostepne_miny.size()>0){ auto x=dostepne_miny.back(); int mina=x; dostepne_miny.pop_back(); // cout<<miny_dos[x]; if (miny_dos[x]>=0){ int sila= miny_dos[x]; miny_dos[x]=-1; for (int k =1 ;k<=n;k++){ if ((odleglosci[x][k]<=sila) and (miny_dos[k]>=0)){ dostepne_miny.push_back(k); //cout<< "mam mine x="<<x<<" dodaje mine k="<<k<<"\n"; } } } } int ile_min=0; for (int k=1;k<=n;k++){ if (miny_dos[k]==-1){ile_min++;} } cout<<ile_min<<" "; } }
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 | #include <iostream> #include <list> #include <vector> using namespace std; typedef struct droga { int do_miasta, dlugosc; struct droga *next; } droga; void find_times(droga *adj_list[], int num_miast, int from_cell, int min_drogi[], int MIASTA) { int done[MIASTA + 1]; int i, j, found; int min_time, min_time_index, old_time; droga *e; for (i = 1; i <= num_miast; i++) { done[i] = 0; min_drogi[i] = -1; } //cout<<" tu jestem"; min_drogi[from_cell] = 0; for (i = 0; i < num_miast; i++) { min_time = -1; found = 0; // cout <<" tu jestem 1\n"; for (j = 1; j <= num_miast; j++) { // cout<< " dla miasta j="<<j<<" done mamy"<<done[j]<<" min mamy ="<<min_drogi[j]<<"\n"; if (!done[j] && min_drogi[j] >= 0) { if (min_time == -1 || min_drogi[j] < min_time) { min_time = min_drogi[j]; min_time_index = j; found = 1; // cout<<"ustawiam min_tim" <<min_time<<" i indeks na min_time_indedex="<<min_time_index<<"\n"; } } } //cout<<" tu jestem 2\n"; if (!found) break; done[min_time_index] = 1; //cout<<" tu jestem 3\n"; e = adj_list[min_time_index]; while (e) { old_time = min_drogi[e->do_miasta]; if (old_time == -1 || old_time > min_time + e->dlugosc) { // cout<<" zmieni³em dla miasta ="<<e->do_miasta<<" wartosc na "<<min_time + e->dlugosc<<"\n"; min_drogi[e->do_miasta] = min_time + e->dlugosc;} e = e->next; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,k; cin>>n; int min_droga[n+1]; int miny[n+1]; int miny_dos[n+1]; vector<vector<int>> odleglosci; droga *adj_list[n + 1]; int num_cases, case_num, i; int num_cells, exit_cell, time_limit, num_edges; int from_cell, to_cell, length; int total, min_time; int zamek; list<int> zamki; int sila; droga *e; for (i=1;i<=n;i++){ cin>>sila; miny[i]=sila; } for (i = 1; i <= n; i++) adj_list[i] = NULL; for (i = 0; i < n-1; i++) { //cout<<i<<"\n"; cin>>from_cell>>to_cell>>length; e = new (droga); if (e == NULL) { fprintf(stderr, "malloc - b³¹d przydzielania pamiêci\n"); exit(1); } e->do_miasta = from_cell; e->dlugosc = length; e->next = adj_list[to_cell]; adj_list[to_cell] = e; e = new (droga); if (e == NULL) { fprintf(stderr, "malloc - b³¹d przydzielania pamiêci\n"); exit(1); } e->do_miasta = to_cell; e->dlugosc = length; e->next = adj_list[from_cell]; adj_list[from_cell] = e; } //cout<< "szukam\n"; vector<int> aa; aa.push_back(0); odleglosci.push_back(aa); for (exit_cell=1;exit_cell<n+1;exit_cell++){ find_times(adj_list, n+1, exit_cell, min_droga,n+1); vector<int> row; row.push_back(0) ; //cout<<"\n"; for (i = 1; i <= n; i++) { // cout<<min_droga[i]<< " "; row.push_back(min_droga[i]);} //cout<<row.size()<<"\n"; odleglosci.push_back(row); } /* for(i=1;i<=n;i++){ for(int j=1;j<=n;j++) cout<<odleglosci[i][j]<<"\t"; cout <<"\n"; } */ vector<int> dostepne_miny; for (i=1;i<=n;i++){ dostepne_miny.clear(); for (int j=1;j<=n;j++){ miny_dos[j]=miny[j]; } dostepne_miny.push_back(i); while(dostepne_miny.size()>0){ auto x=dostepne_miny.back(); int mina=x; dostepne_miny.pop_back(); // cout<<miny_dos[x]; if (miny_dos[x]>=0){ int sila= miny_dos[x]; miny_dos[x]=-1; for (int k =1 ;k<=n;k++){ if ((odleglosci[x][k]<=sila) and (miny_dos[k]>=0)){ dostepne_miny.push_back(k); //cout<< "mam mine x="<<x<<" dodaje mine k="<<k<<"\n"; } } } } int ile_min=0; for (int k=1;k<=n;k++){ if (miny_dos[k]==-1){ile_min++;} } cout<<ile_min<<" "; } } |