#include<iostream> using namespace std; const int nn=302; const int kk=402; //int PP[nn][kk]; //int Plicz[nn]; int NN[nn][kk]; int Nlicz[nn]; int DlSciezek[nn]; int LiczDlugoscSciezkiBezKilkuWezlow(int n, int k1, int k2, int k3, int k4){ int i; int j; int MaxDlugosc=0; int tmp; for(i=n;i>0;i--){ if ( (k1!=i) && (k2!=i) && (k3!=i) && (k4!=i) ) { DlSciezek[i]=0; for(j=0;j<Nlicz[i];j++){ if((k1!=NN[i][j]) && (k2!=NN[i][j]) && (k3!=NN[i][j]) && (k4!=NN[i][j])){ tmp=DlSciezek[NN[i][j]]; if(tmp>DlSciezek[i]) DlSciezek[i]=tmp; } } (DlSciezek[i])++; //cout << "Sciezka od wezla" << i << " ma dlugosc " << DlSciezek[i] << endl; if(MaxDlugosc<DlSciezek[i]) MaxDlugosc=DlSciezek[i]; } } return MaxDlugosc; } int main () { int n; int m; int k; int x; int y; int i; int j; int dl; int tmp; int k1; int k2; int k3; int k4; //int k1w=-1; //int k2w=-1; //int k3w=-1; //int k4w=-1; for(i=0;i<nn;i++){ //Plicz[i]=0; Nlicz[i]=0; } cin >> n >> m >>k; for(i=0;i<m;i++){ cin >> x >> y; NN[x][Nlicz[x]]=y; //PP[y][Plicz[y]]=x; (Nlicz[x])++; //(Plicz[y])++; } /* cout << "Nasrepnicy" << endl; for(i=1;i<=n;i++){ cout << i << " " << Nlicz[i] << " "; for(j=0;j<Nlicz[i];j++) cout << NN[i][j] << " "; cout << endl; } */ if(k==0){ //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << LiczDlugoscSciezkiBezKilkuWezlow(n,-1,-1,-1,-1) << endl; dl=LiczDlugoscSciezkiBezKilkuWezlow(n,-1,-1,-1,-1); } else if(k==1){ dl=999; for(k1=1;k1<=n;k1++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, -1, -1, -1); if(dl>tmp) { dl=tmp; //k1w=k1; } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } else if(k==2){ dl=999; for(k1=1;k1<=n-1;k1++){ for(k2=k1+1;k2<=n;k2++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, k2, -1, -1); if(dl>tmp) { dl=tmp; //k1w=k1; k2w=k2; } } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } else if(k==3){ dl=999; for(k1=1;k1<=n-2;k1++){ for(k2=k1+1;k2<=n-1;k2++){ for(k3=k2+1;k3<=n;k3++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, k2, k3, -1); if(dl>tmp) { dl=tmp; //k1w=k1; k2w=k2; k3w=k3; } } } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } else if(k==4){ dl=999; for(k1=1;k1<=n-3;k1++){ for(k2=k1+1;k2<=n-2;k2++){ for(k3=k2+1;k3<=n-1;k3++){ for(k4=k3+1;k4<=n;k4++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, k2, k3, k4); if(dl>tmp) { dl=tmp; //k1w=k1; k2w=k2; k3w=k3; k4w=k4; } } } } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } cout << dl; 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 | #include<iostream> using namespace std; const int nn=302; const int kk=402; //int PP[nn][kk]; //int Plicz[nn]; int NN[nn][kk]; int Nlicz[nn]; int DlSciezek[nn]; int LiczDlugoscSciezkiBezKilkuWezlow(int n, int k1, int k2, int k3, int k4){ int i; int j; int MaxDlugosc=0; int tmp; for(i=n;i>0;i--){ if ( (k1!=i) && (k2!=i) && (k3!=i) && (k4!=i) ) { DlSciezek[i]=0; for(j=0;j<Nlicz[i];j++){ if((k1!=NN[i][j]) && (k2!=NN[i][j]) && (k3!=NN[i][j]) && (k4!=NN[i][j])){ tmp=DlSciezek[NN[i][j]]; if(tmp>DlSciezek[i]) DlSciezek[i]=tmp; } } (DlSciezek[i])++; //cout << "Sciezka od wezla" << i << " ma dlugosc " << DlSciezek[i] << endl; if(MaxDlugosc<DlSciezek[i]) MaxDlugosc=DlSciezek[i]; } } return MaxDlugosc; } int main () { int n; int m; int k; int x; int y; int i; int j; int dl; int tmp; int k1; int k2; int k3; int k4; //int k1w=-1; //int k2w=-1; //int k3w=-1; //int k4w=-1; for(i=0;i<nn;i++){ //Plicz[i]=0; Nlicz[i]=0; } cin >> n >> m >>k; for(i=0;i<m;i++){ cin >> x >> y; NN[x][Nlicz[x]]=y; //PP[y][Plicz[y]]=x; (Nlicz[x])++; //(Plicz[y])++; } /* cout << "Nasrepnicy" << endl; for(i=1;i<=n;i++){ cout << i << " " << Nlicz[i] << " "; for(j=0;j<Nlicz[i];j++) cout << NN[i][j] << " "; cout << endl; } */ if(k==0){ //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << LiczDlugoscSciezkiBezKilkuWezlow(n,-1,-1,-1,-1) << endl; dl=LiczDlugoscSciezkiBezKilkuWezlow(n,-1,-1,-1,-1); } else if(k==1){ dl=999; for(k1=1;k1<=n;k1++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, -1, -1, -1); if(dl>tmp) { dl=tmp; //k1w=k1; } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } else if(k==2){ dl=999; for(k1=1;k1<=n-1;k1++){ for(k2=k1+1;k2<=n;k2++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, k2, -1, -1); if(dl>tmp) { dl=tmp; //k1w=k1; k2w=k2; } } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } else if(k==3){ dl=999; for(k1=1;k1<=n-2;k1++){ for(k2=k1+1;k2<=n-1;k2++){ for(k3=k2+1;k3<=n;k3++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, k2, k3, -1); if(dl>tmp) { dl=tmp; //k1w=k1; k2w=k2; k3w=k3; } } } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } else if(k==4){ dl=999; for(k1=1;k1<=n-3;k1++){ for(k2=k1+1;k2<=n-2;k2++){ for(k3=k2+1;k3<=n-1;k3++){ for(k4=k3+1;k4<=n;k4++){ tmp=LiczDlugoscSciezkiBezKilkuWezlow(n, k1, k2, k3, k4); if(dl>tmp) { dl=tmp; //k1w=k1; k2w=k2; k3w=k3; k4w=k4; } } } } } //cout << "Najdluzsza scieszka w grafie" << " bez wezlow " << k1w << " " << k2w << " " << k3w << " " << k4w << " to " << dl << endl; } cout << dl; return 0; } |