Cod sursa(job #1803215)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 11 noiembrie 2016 09:40:10
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>

using namespace std;

ifstream f ("sate.in");
ofstream t ("sate.out");

struct edges{
int dest,cost;};

vector < vector <edges> > v(30010);
int n,m;
bool vaz[30010];

int bfs(int nod,int target){int curent,cost=0;
    queue <int> q;
    q.push(nod);
    while (!q.empty()){
    start:
    curent=q.front();
    vaz[curent]=true;
    if (curent==target) return cost;
    q.pop();
        for (unsigned i=0;i<v[curent].size();++i)
            if (!vaz[v[curent][i].dest]){
            q.push(v[curent][i].dest);
            if (v[curent][i].dest>curent) cost+=v[curent][i].cost;
            else cost-=v[curent][i].cost;
            goto start;
            }
    }
    return 0;
}

int main()
{
    int x,y,t1,t2,c;
    f>>n>>m>>x>>y;
    for (int i=0;i<m;++i)
        f>>t1>>t2>>c,v[t1].push_back({t2,c}),v[t2].push_back({t1,c});
    t<<bfs(x,y);
    return 0;
}