Cod sursa(job #2556523)

Utilizator cyg_vladioanBirsan Vlad cyg_vladioan Data 24 februarie 2020 23:09:27
Problema Sate Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
const int NMAX = 30000;
struct muchii
{
    int nod , d;
};
muchii temp;
vector <muchii> G[NMAX + 5];
int d[NMAX + 5] , viz[NMAX + 5];
string s;
void dfs(int u)
{
    int v , j;
    viz[u] = 1;
    for(j = 0 ; j < G[u].size() ; j ++)
    {
        v = G[u][j].nod;
        if(viz[v] == 0)
        {
            d[v] = d[u] + G[u][j].d;
            dfs(v);
        }
    }
}
int main()
{
    freopen("sate.in" , "r" , stdin);
    freopen("sate.out" , "w" , stdout);
    int n , m , start , finish , x , y , z , i , j , ok , l;
    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';
        temp.nod = y;
        temp.d = z;
        G[x].push_back(temp);
        temp.nod = x;
        temp.d = -z;
        G[y].push_back(temp);
    }
    dfs(start);
    printf("%d\n" , d[finish]);
    return 0;
}