Tuesday, June 8, 2010

Can't Carry Code(C cube)

What a discouragement they cannot even carry ma code. They have cut part of the code what a pity

This is the correct dijstra's algorithm. I just figured it out.

#include
using namespace std;

int main()
{
    FILE *fin;
    FILE *fout;
    fin=fopen("dij.in","r");
    fout=fopen("dij.out","w");
    int size,tot=0,index;
    fscanf(fin,"%d",&size);
    int man[size][size];
    int vis[size];
    int dis[size];
    int row,col,wei;
    int min=0,plus=0;
    for (int i=0;i
    {
        vis[i]=0;
        dis[i]=1000;
        for(int j=0;j
        man[i][j]=1000;
    }
    int sv=0;
    index=0;
    dis[0]=0;
    while(fscanf(fin,"%d %d %d",&row,&col,&wei) && !feof(fin))
    {
        man[row][col]=wei;
    }
    for (int i=index;tot!=size-1;i++)
    {  
        i=index;
        vis[index]=1;  
        min=1000;
        for(int j=0;j
        {
            if(man[i][j]
            {
                min=man[i][j];
                index=j;
            }
        }
        printf("plus is: %d,min is %d\n",plus,min);
        for(int j=0;j
        {
            if(man[i][j]<1000)
            {
                if(vis[j]==0 && dis[j]>man[i][j]+plus)
                {
                    dis[j]=man[i][j]+plus;
                }
            }
        }
        plus=plus+min;
        tot=tot+1;
    }
    for(int v=0;v
    {
        fprintf(fout,"%d  ",dis[v]);
    }
return 0;
}

Trying dijstra'a algorithm in code ...............arwghrr!!.

#include
using namespace std;

int main()
{
    FILE *fin;
    FILE *fout;
    fin=fopen("dij.in","r");
    fout=fopen("dij.out","w");
    int size;
    fscanf(fin,"%d",&size);
    int man[size][size];
    int vis[size];
    int dis[size];
    int row,col,wei;
    int min;
    for (int i=0;i
    {
        vis[i]=0;
        dis[i]=1000;
        for(int j=0;j
        man[i][j]=1000;
    }
    int sv=0;
    dis[0]=0;
    while(fscanf(fin,"%d %d %d",&row,&col,&wei) && !feof(fin))
    {
        man[row][col]=wei;
    }
    int svi=0,index;
    for (int i=0;i
    {   
        min=1000;
        for(int j=0;j
        {
            if(man[i][j]<1000 && (man[i][j]+sv)            {
                dis[j]=man[i][j]+sv;
                if(min>man[i][j]+sv)
                {
                    min=man[i][j]+sv;
                    printf("j is %d",i);
                }
            }
        }
        printf("The minimum is: %d at row %d\n",min,i);
        sv=min;
        svi=index;
    }
    fprintf(fout,"This is the solution: \n");
    for (int i=0;i
    {
        fprintf(fout,"%d  ",dis[i]);
    }
return 0;
}



This code is tryint to misbehave c'mon work so that i can have a break;