Cod sursa(job #641562)

Utilizator TheShadowsAlexandru Cristian TheShadows Data 28 noiembrie 2011 20:31:11
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
const int N=30001;
struct stuff{
    int y,d;};
vector <stuff> a[N];
queue <stuff> v;
stuff h;
int lmin,n,m,poz[N];
void lee(int start,int x)
{
    int u,d,g;
    poz[start] = -1;
    while(!v.empty())
    {
        h=v.front();
        v.pop();
        u=h.y;
        d=h.d;
        for(int i=0;i<a[u].size();i++)
        {
            g=a[u][i].y;
            if(poz[g]==0)
            {
                poz[g]=-1;
                h.y=g;
                if(u<=g)
                    h.d=d+a[u][i].d;
                else
                    h.d=d-a[u][i].d;
                if(g!=x)
                    v.push(h);
                else
                {
                    lmin = h.d;
                    return;
                }
            }
        }
    }
}
int main()
{
    ifstream f("sate.in");
    ofstream g("sate.out");
    int x,y,k1,k2,d,i;
    f>>n>>m>>x>>y;
    for(i=1;i<=m;i++)
    {
        f>>k1>>k2>>d;
        h.y=k2;
        h.d=d;
        a[k1].push_back(h);
        h.y=k1;
        a[k2].push_back(h);
    }
    /*
    for(i=1 ; i<=n ; i++)
    {
        printf("din %d:\t",i);
        for(int j=0 ; j<a[i].size() ; j++) printf("(%d,%d) ",a[i][j].y,a[i][j].d);
        printf("\n");
    }
    */
    h.y=x;
    h.d=0;
    v.push(h);
    lmin=999999;
    lee(x,y);
    g<<lmin<<endl;
    return 0;
}