Cod sursa(job #2380375)

Utilizator AndreiDeltaBalanici Andrei Daniel AndreiDelta Data 14 martie 2019 20:54:33
Problema Sortare topologica Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.51 kb
#include <bits/stdc++.h>
#define Dim 15005
using namespace std;
ifstream f("easygraph.in");
ofstream g("easygraph.out");
int N,M,A[Dim],T,t,a,b;
long Ans[20][Dim],ad;
bool viz[20][Dim];

struct cmp
{
    bool operator()(int X,int Y)
    {
        if(Ans[t][X]<Ans[t][Y]) return 1;
        else return 0;
    }
};

vector <int> V[20][Dim];
priority_queue < int,vector<int>,cmp > maxheap[20];

void Dijkstra()
{
    Ans[t][1]=A[1];
    maxheap[t].push(1);
    viz[t][1]=1;
    while(!maxheap[t].empty())
    {
        int nod=maxheap[t].top();
        maxheap[t].pop();
        viz[t][nod]=0;
        for(unsigned int i=0;i<V[t][nod].size();i++)
        {
            int vecin=V[t][nod][i];
            if(Ans[t][nod]<=0) ad=0;
            else ad=Ans[t][nod];
            if(ad+A[vecin]>=Ans[t][vecin])
            {
                Ans[t][vecin]=A[vecin]+ad;
                if(!viz[t][vecin])
                {
                    viz[t][vecin]=1;
                    maxheap[t].push(vecin);
                }
            }
        }
    }
}

int main()
{
    f>>T;
    for(t=1;t<=T;t++)
    {
        f>>N>>M;
        long ras=0;
        for(int i=1;i<=N;i++) f>>A[i];
        for(int i=1;i<=M;i++)
        {
            f>>a>>b;
            V[t][a].push_back(b);
        }
        Dijkstra();
        for(int i=1;i<=N;i++) {
                ras=max(ras,Ans[t][i]);
                //cout<<i<<" "<<Ans[t][i]<<'\n';
        }
        g<<ras<<'\n';
    }
    return 0;
}