Cod sursa(job #861221)

Utilizator galbenugalbenu dorin galbenu Data 21 ianuarie 2013 10:31:16
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 0.91 kb
#include<iostream>
#include<cstdio>
#define nmax 50050
#define mmax 250050
#define INFINIT 0x3f3f3f
using namespace std;
FILE *f=fopen("dijkstra.in","r"),*g=fopen("dijkstra.out","w");
struct muchie
{
    int inc,sf;
    short int cost;
};
muchie M[mmax];
int Cost[nmax];
int n,m;
void read()
{
    fscanf(f,"%d %d",&n,&m);
    for(int i=1;i<=m;i++)
        fscanf(f,"%d %d %hd",&M[i].inc,&M[i].sf,&M[i].cost);
}
void solve()
{   bool q=1;
    for(int i=2;i<=n;i++)
    Cost[i]=INFINIT;
    for(int i=1;i<=n && q;i++)
    {
        q=0;
        for(int j=1;j<=m;j++)
        if(Cost[M[j].inc]+M[j].cost<Cost[M[j].sf])
        {
            Cost[M[j].sf]=Cost[M[j].inc]+M[j].cost;
            q=1;
        }
    }
}
void show()
{
    for(int i=2;i<=n;i++)
    fprintf(g,"%d ",Cost[i]);
}
int main()
{
    read();
    solve();
    show();
    fclose(f);
    fclose(g);
    return 0;
}