Cod sursa(job #3240685)

Utilizator PitigoiOlteanEmanuelPitigoi Oltean Emanuel PitigoiOlteanEmanuel Data 19 august 2024 21:25:25
Problema Algoritmul Bellman-Ford Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;
ifstream cin("bellmanford.in");
ofstream cout("bellmanford.out");


int sum=0;
int n,m;
int a ,b,co;
int drum[50005];

struct pe
{
    vector <int> p,c;

}v[50001];

int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b>>co;
        v[a].p.push_back(b);

        v[a].c.push_back(co);


    }

    for(int i=1;i<=n;i++)
    {
        drum[i]=(1<<30);
    }
drum[1]=0;
    for(int j=1;j<=n-1;j++)
    {
        for(int i=1;i<=n;i++)
        {
            if(drum[i]==(1<<30))
            {
                continue;
            }
            for(int k=0;k<v[i].p.size();k++)
            {

                if(drum[v[i].p[k]]>drum[i]+v[i].c[k]  )
                {
                    drum[v[i].p[k]]=drum[i]+v[i].c[k];

                }
            }


        }





    }
    for(int i=2;i<=n;i++)
    {
        cout<<drum[i]<<" ";
    }


    return 0;
}