Cod sursa(job #2395220)

Utilizator BogdanAlexandruBogdan-Alexandru Dig BogdanAlexandru Data 2 aprilie 2019 12:38:49
Problema Obiective Scor 35
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <stdio.h>
#include <vector>
#define oo 32010
#include <deque>
#define mp make_pair
using namespace std;
FILE *f,*g;
struct Graf
{
    int node,cost;
};
vector <Graf> gph[32010];
deque <int> coada;
int path[32010],N,M,Poz[32010];
void Read()
{
    int i,X,Y;
    fscanf(f,"%d %d",&N,&M);
    for(i=1;i<=M;++i)
    {
        fscanf(f,"%d %d",&X,&Y);
        gph[X].push_back({Y,0});
        gph[Y].push_back({X,1});
    }
}
void UpdatePath()
{
    int i;
    for(i=1;i<=N;++i)path[i]=oo;
}
int X,Y;
void BFS(int Node)
{
    coada.push_back(Node);
    int vone,i,t,typ,sens,Sol=oo;
    path[Node]=0;
    while(!coada.empty())
    {
        Node=coada.front();coada.pop_front();
        t=gph[Node].size();
        if(path[Node]>Sol)continue;
        for(i=0;i<t;++i)
        {
            vone=gph[Node][i].node,sens=gph[Node][i].cost;
            if(path[Node]+sens<path[vone])
            {
                path[vone]=path[Node]+sens;
                if(vone==Y&&Sol>path[vone])Sol=path[vone];
                coada.push_back(vone);
            }
        }
    }
}
void Solve()
{
    int T;
    fscanf(f,"%d",&T);
    while(T)
    {
        --T;
        fscanf(f,"%d %d",&X,&Y);
        UpdatePath();
        BFS(X);
        fprintf(g,"%d\n",path[Y]);
    }
}
int main()
{
    f=fopen("obiective.in","r");g=fopen("obiective.out","w");
    Read();
    Solve();
    fclose(f);fclose(g);
    return 0;
}