fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int c = 0,cost = 999999;
  4. int graph[21][21];
  5. void swap (int *x, int *y){
  6. int temp;
  7. temp = *x;
  8. *x = *y;
  9. *y = temp;
  10. }
  11. void copy_array(int *a, int n){
  12. int i, sum = 0;
  13. for(i = 0; i <= n; i++) sum += graph[a[i % 4]][a[(i + 1) % 4]];
  14. if (cost > sum) cost = sum;
  15. }
  16. void permute(int *a, int i, int n)
  17. {
  18. int j, k;
  19. if (i == n) copy_array(a, n);
  20. else{
  21. for (j = i; j <= n; j++){
  22. swap((a + i), (a + j));
  23. permute(a, i + 1, n);
  24. swap((a + i), (a + j));
  25. }
  26. }
  27. }
  28. int main(){
  29. int n;
  30. int a[n+5];
  31. cin>>n;
  32. for(int i=0;i<n;++i){
  33. a[i]=i;
  34. for(int j=0;j<n;++j) cin>>graph[i][j];
  35. }
  36.  
  37. permute(a,0,n-1);
  38. cout<<cost;
  39. //getch();
  40. }
Success #stdin #stdout 0.01s 5548KB
stdin
4
0 1 2 3
1 0 4 2
2 4 0 6
3 2 6 0
stdout
11