Cod sursa(job #4686)

Utilizator vlad_DVlad Dumitriu vlad_D Data 6 ianuarie 2007 10:52:31
Problema Cc Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int n, v[112][112];
int i, j, k;
int sol[111];
int bsol[111];
int bcost;
int cost;
int main() {
freopen("cc.in", "r", stdin);
freopen("cc.out", "w", stdout);
srand(time(0)%174);
scanf("%d", &n);
for (i=1; i<=n; i++) for (j=1; j<=n; j++) scanf("%d", &v[i][j]);
for (j=1; j<=n; j++) sol[j] = j;
for (int run=1; run<=10000; run++){
while (1) {
int ok = 0;
//swap
int a, b, aux;
a=rand()%n, b=rand()%n;
while (a!=b){
      a = rand()%n;
      }
a++; b++;
aux; aux = sol[a]; sol[a] = sol[b] ; sol[b] = aux;
a=rand()%n, b=rand()%n;
while (a!=b){
      a = rand()%n;
      }
a++; b++;
aux; aux = sol[a]; sol[a] = sol[b] ; sol[b] = aux;

a=rand()%n, b=rand()%n;
while (a!=b){
      a = rand()%n;
      }
a++; b++;
aux; aux = sol[a]; sol[a] = sol[b] ; sol[b] = aux;
a=rand()%n, b=rand()%n;
while (a!=b){
      a = rand()%n;
      }
a++; b++;
aux; aux = sol[a]; sol[a] = sol[b] ; sol[b] = aux;
a=rand()%n, b=rand()%n;
while (a!=b){
      a = rand()%n;
      }
a++; b++;
aux; aux = sol[a]; sol[a] = sol[b] ; sol[b] = aux;

a=rand()%n, b=rand()%n;
while (a!=b){
      a = rand()%n;
      }
a++; b++;
aux; aux = sol[a]; sol[a] = sol[b] ; sol[b] = aux;

for (j=1; j<=n; j++)
for (k=1; k<=n; k++) if (j!=k) if (v[j][sol[j]] + v[k][sol[k]] > v[j][sol[k]] + v[k][sol[j]]) {
    int aux = sol[j]; sol[j] = sol[k]; sol[k] = aux;
    ok = 1;
    }
if (ok == 0) break;
}
cost = 0;
for (i=1; i<=n; i++) cost+=v[i][sol[i]];
if (run == 1)
   bcost = cost;
if (cost < bcost) bcost = cost;
}
printf("%d\n", bcost);
return 0;
}