Cod sursa(job #2556507)

Utilizator cyg_vladioanBirsan Vlad cyg_vladioan Data 24 februarie 2020 22:54:06
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <cstdio>
#include <vector>
#include <cstring>
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];
char s[105] , *p;
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 ++)
    {
        gets(s);
        x = y = z = 0;
        p = strtok(s , " ");
        while(*p != NULL)
        {
          x = x * 10 + (*p) - '0';
          p ++;
        }
        p = strtok(NULL , " ");
        while((*p) != NULL)
        {
          y = y * 10 + (*p) - '0';
          p ++;
        }
        p = strtok(NULL , " ");
        while((*p) != NULL)
        {
          z = z * 10 + (*p) - '0';
          p ++;
        }
        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;
}