Cod sursa(job #1857309)

Utilizator NinjaCubeMihai Radovici NinjaCube Data 26 ianuarie 2017 00:37:18
Problema Sate Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.18 kb
#include<bits/stdc++.h>
#define dim 1000005
using namespace std;
typedef struct tip
{
    int a,b,d;
};
tip v[200035];
char buff[dim+5];
int poz=0;
void citeste(int &numar)
{
    numar=0;
    while(buff[poz]<'0' || buff[poz]>'9')
    {
        poz++;
        if(poz==dim)
        {
            poz=0;
            fread(buff,1,dim,stdin);
        }
    }
    while(buff[poz]>='0' && buff[poz]<='9')
    {
        numar=numar*10+buff[poz]-'0';
        poz++;
        if(poz==dim)
        {
            poz=0;
            fread(buff,1,dim,stdin);
        }
    }
}
bool comp(tip a,tip b)
{
    if(a.a<b.a) return 1;
    if(a.a==b.a && a.b<b.b) return 1;
    return 0;
}
deque<int> q;
int n,m,x,y,z,st,dr,sol,mid;
int cost[30005];
int main()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    fread(buff,1,dim,stdin);
    citeste(n);
    citeste(m);
    citeste(x);
    citeste(y);
    //scanf("%d%d%d%d",&n,&m,&x,&y);
    for(int i=1;i<=m;i++)
    {
      //  scanf("%d%d%d",&v[i].a,&v[i].b,&v[i].d);
      citeste(v[i].a);
      citeste(v[i].b);
      citeste(v[i].d);
    }
    for(int i=1;i<=m;i++)
    {
        v[i+m].a=v[i].b;
        v[i+m].b=v[i].a;
        v[i+m].d=-v[i].d;
    }
    sort(v+1,v+(2*m)+1,comp);
    cost[v[1].a]=1;
    for(int i=1;i<=n;i++)
    {
    if(cost[x] && cost[y]) break;
    if(!cost[i]) continue;
    {
    q.push_back(i);
    while(!q.empty())
    {
        z=q.front();
        st=1;
        dr=2*m;
        sol=0;
        while(st<=dr)
        {
            mid=(st+dr)>>1;
            if(v[mid].a==z)
            {
                sol=mid;
                dr=mid-1;
            }
                else
            if(v[mid].a>z)
            {
                dr=mid-1;
            }
                else
                st=mid+1;
        }
        while(sol<=(2*m) && v[sol].a==z)
        {
            if(!cost[v[sol].b])
            {
                cost[v[sol].b]=cost[z]+v[sol].d;
                q.push_back(v[sol].b);
            }
            sol++;
        }
        q.pop_front();
    }
    }
    }
    printf("%d\n",cost[y]-cost[x]);
    return 0;
}