Cod sursa(job #1265731)

Utilizator alexsuciuAlex Suciu alexsuciu Data 17 noiembrie 2014 18:03:54
Problema Parcurgere DFS - componente conexe Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.8 kb
#include <algorithm>
#include <fstream>

using namespace std;

const int pinf = 1<<30;
ifstream f("camionas.in");
ofstream g("camionas.out");

int heap[50005],poz[50005],d[50005];
int n,m,i;

struct nod
{
    int cost,val;
    nod *next;
}*p,*l[50001];

void citire(int &n,int &m)
{
    int x,y,c,i,G;
    nod *p;

    f>>n>>m>>G;
    for(i=1;i<=m;i++)
    {
        f>>x>>y>>c;
        p=new nod;
        p->val=y;
        if(c<G)
        p->cost=1;
        else p->cost=0;
        p->next=l[x];
        l[x]=p;
        p=new nod;
        p->val=x;
        if(c<G)
        p->cost=1;
        else p->cost=0;
        p->next=l[y];
        l[y]=p;
    }
}

void urca(int pozi)
{
    while(pozi>1&& d[heap[pozi]]<=d[heap[pozi/2]])
    {
        swap(heap[pozi],heap[pozi/2]);
        poz[heap[pozi]]=pozi;
        poz[heap[pozi/2]]=pozi/2;
        pozi/=2;

    }
}

void coboara(int pozi)
{
 {
    int poz1=0;

    while(pozi!=poz1) {
        poz1=pozi;

        if(2*poz1<=n && d[heap[pozi]]>d[heap[2*poz1]])
            pozi=2*poz1;
        if(2*poz1+1<=n && d[heap[pozi]]>d[heap[2*poz1+1]])
            pozi=2*poz1+1;

        swap(heap[pozi],heap[poz1]);
        poz[heap[pozi]]=pozi;
        poz[heap[poz1]]=poz1;

        }
}}


int main()
{
    citire(n,m);
    int ni=n,x;
    for(i=1;i<=n;i++)
    {
        d[i]=pinf;
        heap[i]=poz[i]=i;
    }
    d[1]=0;
    for(i=1;i<=ni;i++)
    {
        x=heap[1];
        swap (heap[1],heap[n]);
        swap (poz[heap[1]],poz[heap[n]]);
        n--;
        coboara(1);
        for(p=l[x];p!=NULL;p=p->next)
            if(d[p->val]>d[x]+p->cost)
            {
            d[p->val]=d[x]+p->cost;
            urca(poz[p->val]);
            }
        }

    g<<d[ni];
    return 0;
}