Cod sursa(job #2242633)

Utilizator ciutanpCiuta Andrei Calin ciutanp Data 19 septembrie 2018 09:07:14
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("sate.in");
ofstream g("sate.out");
int n,m,start,finish,ras;
vector< pair<int,int> > G[30001];
queue<int> Q;
int dp[30001];
void BFS()
{
    Q.push(start);
    dp[start]=1;
    while(!Q.empty())
    {
        int node=Q.front();
        Q.pop();
        for(vector< pair <int, int> >::iterator it=G[node].begin();it<G[node].end();++it)
        {
            pair<int,int> i=*it;
            if(dp[i.first]==0)
            {
                dp[i.first]=dp[node]+i.second;
                Q.push(i.first);
            }
            if(i.first==finish)
                return;
        }
    }
}
int main()
{
    f>>n>>m>>start>>finish;
    while(m--)
    {
        int a,b,v;
        f>>a>>b>>v;
        G[a].push_back(make_pair(b,v));
        G[b].push_back(make_pair(a,-v));
    }
    BFS();
    g<<dp[finish]-1;
}