Cod sursa(job #2556535)

Utilizator cyg_vladioanBirsan Vlad cyg_vladioan Data 24 februarie 2020 23:22:00
Problema Sate Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.37 kb
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
const int NMAX = 30000;
vector <pair <int , int> > G[NMAX + 5];
int d[NMAX + 5];
bool viz[NMAX + 5];
string s;
queue <int> q;
void bfs(int u)
{
    viz[u] = 1;
    q.push(u);
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        for(int j = 0 ; j < G[u].size() ; j ++)
        {
            int v = G[u][j].first;
            if(viz[v] == 0)
            {
                d[v] = d[u] + G[u][j].second;
                viz[v] = 1;
                q.push(v);
            }
        }
    }
}
int main()
{
    freopen("sate.in" , "r" , stdin);
    freopen("sate.out" , "w" , stdout);
    int n , m , start , finish , x , y , z , i , j;
    scanf("%d%d%d%d\n" , &n , &m , &start , &finish);
    for(i = 1 ; i <= m ; i ++)
    {
        getline(cin , s);
        s.push_back(' ');
        x = y = z = j = 0;
        while(s[j] != ' ')
            x = x * 10 + s[j ++] - '0';
        j ++;
        while(s[j] != ' ')
            y = y * 10 + s[j ++] - '0';
        j ++;
        while(s[j] != ' ')
            z = z * 10 + s[j ++] - '0';
        G[x].push_back(make_pair(y , z));
        G[y].push_back(make_pair(x , -z));
    }
    bfs(start);
    printf("%d\n" , d[finish]);
    return 0;
}