Cod sursa(job #2210963)

Utilizator AndreiCroitoruAndrei Croitoru AndreiCroitoru Data 8 iunie 2018 20:11:23
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <bits/stdc++.h>

using namespace std;
ifstream in("sate.in");
ofstream out("sate.out");
vector < pair < int , int > > G[30001];

int viz[30001],n,x,m,y;
void dfs(int i)
{
    int j;
    for(j = 0 ;j< G[i].size(); j++)
    {
        int w=G[i][j].first;
        int cost=G[i][j].second;
        if(!viz[w])
        {
            viz[w]=viz[i]+cost;
            dfs(w);
        }
    }
}
int main()
{
    int a,b;
    in>>n>>m>>a>>b;
    if(b<a)
        swap(a,b);
    for(int i = 1; i <= m; i++)
    {
        int x,y,c;
        in>>x>>y>>c;
        G[x].push_back(make_pair(y,c));
        G[y].push_back(make_pair(x,-c));
    }
    dfs(a);
    out<<viz[b];
    return 0;
}