Cod sursa(job #690984)

Utilizator R.A.RFMI Romila Remus Arthur R.A.R Data 26 februarie 2012 10:01:25
Problema Algoritmul lui Dijkstra Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#define NMAX 50002
#define LIM (1<<15)
#define oo 99999

using namespace std;

ifstream in("dijkstra.in");
ofstream out("dijkstra.out");

int D[NMAX],N,M;

char my_text[LIM+2];
int poz;

#define verifica ++poz==LIM ? in.read(my_text,LIM),poz = 0 : 0

inline void Citeste(int &nr)
{
    if(my_text[0]=='\0')in.read(my_text,LIM);
    else for(;my_text[poz]<'0'||my_text[poz]>'9';verifica);
    for(nr=0;my_text[poz]>='0'&&my_text[poz]<='9';nr=nr*10+my_text[poz]-'0',verifica);
}

class cmp
{
    public:
    inline bool operator()(const int&a,const int&b){return D[a]>D[b];}
};

vector<pair<int,int> > V[NMAX];
vector<pair<int,int> >::iterator it,sf;
priority_queue<int,vector<int>,cmp> Heap;

//bitset<NMAX> In_Heap;
bool In_Heap[NMAX];

int main()
{
    int x,y,c;

    Citeste(N),Citeste(M);

    while(M--)
    {
        Citeste(x),Citeste(y),Citeste(c);
        V[x].push_back(make_pair(y,c));
    }
    //plec din nodul 1
    fill(D+2,D+N+1,oo);
    for(Heap.push(1);!Heap.empty();Heap.pop())
    {
        x = Heap.top();
        In_Heap[x] = 0;
        for(it=V[x].begin(),sf=V[x].end();it<sf;++it)
        {
            y = it->first;
            c = it->second;
            if(D[y]>D[x]+c)
            {
                D[y] = D[x]+c;
                if(!In_Heap[y])
                    Heap.push(y),In_Heap[y] = 1;
            }
        }
    }
    for(x=2;x<=N;++x)out<<(D[x]==oo?0:D[x])<<' ';
    return 0;
}