Cod sursa(job #1857755)

Utilizator ASTELOTudor Enescu ASTELO Data 26 ianuarie 2017 16:57:36
Problema Algoritmul Bellman-Ford Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
queue<int> q;
vector<int> v[50001],vc[50001];
int a,i,n,j,m,vcc[50001];
int main ()
{
freopen("bellmanford.in","r",stdin);
freopen("bellmanford.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
    {
    int x,y,z;
    scanf("%d%d%d",&x,&y,&z);
    v[x].push_back(y);
    vc[x].push_back(z);
    }
for(i=2;i<=n;i++)
    vcc[i]=1000000000;
q.push(1);
int pp=0;
while(!q.empty())
    {
    int h=q.front();
    if(vcc[h]<=-100000)
        {
        pp=1;
        break;
        }
    for(i=0;i<v[h].size();i++)
        if(vcc[v[h][i]]>vcc[h]+vc[h][i])
            {
            q.push(v[h][i]);
            vcc[v[h][i]]=vcc[h]+vc[h][i];
            }
    q.pop();
    }
if(pp==1)
    printf("Ciclu negativ!");
else
    for(i=2;i<=n;i++)
        printf("%d ",vcc[i]);
return 0;
}