Cod sursa(job #907033)

Utilizator alexalbu95Albu Alexandru alexalbu95 Data 7 martie 2013 16:04:56
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#include <vector>
#include <queue>
#define mp make_pair
#define pb push_back

using namespace std;

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

const int inf= (1<<30)-1;
const int maxn=30005;

int n, m, start, stop, i;
vector< pair<int, int > > v[maxn];
vector< pair<int, int > > :: iterator it;
queue<int> q;
int d[maxn];
bool block[maxn];

void read()
{
    int x, y, z, i;
    f>>n>>m>>start>>stop;
    for(; m; --m)
    {
        f>>x>>y>>z;
        v[x].pb(mp(y,z));
        v[y].pb(mp(x,-z));
    }
    for(i=1; i<=n; ++i) d[i]=inf;
}

void dijkstra()
{
    int x;

    d[start]=0;
    q.push(start);
    block[start]=1;

    while(!q.empty())
    {
        x=q.front();
        q.pop();
        block[x]=0;

        for(it=v[x].begin(); it!=v[x].end(); ++it)
            if(it->second + d[x] < d[it->first])
            {
                d[it->first] = d[x] + it->second;
                if(!block[it->first])
                {
                    q.push(it->first);
                    block[it->first]=0;
                }
            }
    }

}

int main()
{
    read();
    dijkstra();
    g<<d[stop]<<"\n";

    return 0;
}