Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 1000 /* Array limit */
#define oo 1000000000 /* "infinite" */
int adj[MAX][MAX];
int percurso[MAX];
int cara=0,coroa=0;
int maxpeso=-1000000000; /* another infinite */
int in;
int ja_ta=0;
int somei=0;
void poe_na_fila(int x)
{
percurso[coroa]=x;
coroa++;
}
int somador(int percurso[],int nos)
{
int i=0,soma=0;
somei=1;
for(i=0; i< coroa-1;i++)
soma += adj[percurso[i]][percurso[i+1]];
return soma;
}
void max_peso(int source, int sink, int nodes, int j)
{
int num;
int u = 0, n = 0;
if ( ja_ta ) return;
if (j > nodes && source == in)
{
ja_ta = 1;
return;
}
else
{
if (source == sink) {
poe_na_fila(source);
}
else
{
if (j > nodes)
{
percurso[coroa] = 0;
coroa -= 1;
max_peso(percurso[coroa], sink, nodes, source + 1);
}
else
{
if (adj[source][j] == oo) {
max_peso(source, sink, nodes, j + 1);
}
else
{
poe_na_fila(source);
max_peso(j, sink, nodes, 0);
}
}
}
if ( ja_ta ) return;
else
{
n = somador(percurso, nodes);
if (n > maxpeso)
maxpeso = n;
coroa = coroa - 1;
percurso[coroa] = 0;
coroa = coroa - 1;
max_peso(percurso[coroa], sink, nodes, source + 1);
}
}
}
main (int argc, char *argv[])
{
int noi,nof,peso,i,u,j,arcos,nos,inicio,fim;
inicio=fim=noi=nof=i=j=peso=arcos=nos=u=0;
FILE *fp;
fp=fopen(argv[1],"r");
if (fp==NULL)
printf("Impossível abrir o ficheiro %s", argv[1]);
else
{
fscanf(fp,"%d",&nos);
fscanf(fp,"%d",&arcos);
for(u=0;u<=nos;u++)
{
percurso[u]=0;
}
for(i=0; i<=nos;i++)
{
for(j=0;j<=nos;j++)
adj[i][j]=oo;
}
for(i=0;i<arcos;i++)
{
fscanf(fp,"%d %d %d", &noi,&nof,&peso);
adj[noi][nof]=peso;
}
fscanf(fp,"%d %d", &inicio, &fim);
in=inicio;
max_peso(inicio,fim,nos,0);
if(somei)
printf("%d\n",maxpeso);
else
printf("NA\n");
}
}
It works perfectly,but my teachers are a bit nuts and will put inputs with more than 15Mb!The problem is i have a limit for my arrays or matrices. For example, for the adj matrice i need 2000000 bytes and most of them (half) are not used. I saw an example with 16 millions(!) possible combinations, that would mean a 16 million * 16 million matrice!My question is simple: