Cod sursa(job #1389749)

Utilizator ManuNNazare Emanuel ManuN Data 16 martie 2015 16:50:15
Problema Sate Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <iostream>
#include <vector>
#include <queue>
#include <fstream>
using namespace std;
int viz[30000],tata[30000],s=0;
void cauta(int a1,int a2)
{
    ifstream h("sate.in");
        int n1,m1,x1,y1,c1,i;
        h>>n1>>m1>>x1>>y1;
        for(i=1;i<=m1;i++)
        {
            h>>x1>>y1>>c1;
            if(((x1==a1)&&(a2==y1))||((y1==a1)&&(a2==x1)))
            {
                if(a1<a2)
                    s-=c1;
                else
                    s+=c1;
                break;
            }
        }
        h.close();
}
int main()
{
    int x,m,n,x1,y1,e,b,c,y,i,k=0,j;
    vector <int> v[30000];
    queue <int> q;
    ifstream f("sate.in");
    ofstream g("sate.out");
    f>>n>>m>>x>>y;
    for(i=1;i<=m;i++)
    {
        f>>e>>b>>c;
        v[e].push_back(b);
        v[b].push_back(e);
    }
    q.push(x);
    viz[x]=1;
    while(q.size()>0)
    {
        x=q.front();
        q.pop();
        for(i=0;i<v[x].size();i++)
        {
            if(viz[v[x][i]]==0)
            {
                q.push(v[x][i]);
                viz[v[x][i]]=1;
                tata[v[x][i]]=x;
            }
            if(v[x][i]==y)
            {
                k=1;
                break;
            }
        }
        if(k)
            break;
    }
    f.close();
    s=0;
    while(tata[y])
    {
        cauta(y,tata[y]);
        y=tata[y];
    }
    g<<s;
    g.close();
    return 0;
}