Cod sursa(job #1580872)

Utilizator ionut98Bejenariu Ionut Daniel ionut98 Data 26 ianuarie 2016 11:18:18
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include<fstream>
#define inf 200000000
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,i,d[250005];
struct muchie
{
    int x,y,c;
}a[250005];
bool ok;
int main()
{
    f>>n>>m;
    for(i=1;i<=m;i++)
    {
        f>>a[i].x>>a[i].y>>a[i].c;
        if(a[i].x==1)
          d[a[i].y]=a[i].c;
    }
    for(i=1;i<=n;i++)
      if(d[i]==0)
        d[i]=inf;
    do
    {
        ok=1;
        for(i=1;i<=m;i++)
          if(d[a[i].y]>d[a[i].x]+a[i].c)
          {
              ok=0;
              d[a[i].y]=d[a[i].x]+a[i].c;
          }
    }while(ok==0);
    for(i=2;i<=n;i++)
      if(d[i]==inf)
        g<<0<<" ";
      else
        g<<d[i]<<" ";
    return 0;
}