Cod sursa(job #2546840)

Utilizator OvidRata Ovidiu Ovid Data 14 februarie 2020 17:18:17
Problema Sate Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.93 kb
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ft first
#define sc second

ifstream fin("sate.in"); ofstream fout("sate.out");
int n, x, y, m ;

vector<vector< pair<int,int> > >g;


int d[30010];
bool done=false;

void bfs(){
queue<int>q;
int f;
q.push(x);
d[x]=0;
bool v[30010];
v[x]=true;

while(!q.empty()){
    f=q.front();
    q.pop();
for(int i=0; i<g[f].size(); i++){

    if(!v[g[f][i].ft]){
        v[g[f][i].ft]=true;
        d[g[f][i].ft ]=d[f]+g[f][i].sc;
        if(g[f][i].ft==y){done=true; return;}
        q.push(g[f][i].ft);
    }

}

}



}







//X-first, Y-second6 7 1 4
int main(){
fin>>n>>m>>x>>y;



g.resize(n+5);

for(int i=0; i<m; i++){
    int a, b, d;
    fin>>a>>b>>d;
    g[a].pb(mp(b, d)); g[b].pb(mp(a, -d) );
}



bfs();
cout<<done<<"\n";
if(done){
cout<<d[y];
fout<<d[y];}




return 0;
}