Cod sursa(job #592060)

Utilizator APOCALYPTODragos APOCALYPTO Data 26 mai 2011 17:18:43
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.23 kb
using namespace std;
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
/*
1 ≤ N ≤ 50 000
1 ≤ M ≤ 250 000
*/
#define oo 0x3f3f3f3f
#define nmax 50005
#define pb push_back
struct nod{
int lg,c;};
vector<nod> G[nmax];
queue<int> q;
int N,M;
bool isinq[nmax];
int dp[nmax];
ofstream fout("dijkstra.out");

void solve()
{
    int i;
    q.push(1);
    for(i=1;i<=N;i++)
      dp[i]=oo;
    isinq[1]=1;
    dp[1]=0;
    vector<nod>::iterator it;
    int u;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        isinq[u]=0;
        for(it=G[u].begin();it<G[u].end();it++)
        {
            if(dp[it->lg]>dp[u]+it->c)
            {
                dp[it->lg]=dp[u]+it->c;
                if(!isinq[it->lg])
                {
                    q.push(it->lg);
                    isinq[it->lg]=1;

                }
            }
        }
    }
    for(i=2;i<=N;i++)
    {
        fout<<dp[i]<<" ";
    }
    fout<<"\n";

}
void cit()
{
    ifstream fin("dijkstra.in");
    fin>>N>>M;
    int i,x,y,z;
    for(i=1;i<=M;i++)
    {
        fin>>x>>y>>z;
        G[x].pb((nod){y,z});
        G[y].pb((nod){x,z});
    }

    fin.close();
}

int main()
{

    cit();
    solve();
    fout.close();
    return 0;
}