Cod sursa(job #2061814)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 9 noiembrie 2017 18:49:53
Problema PScNv Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.65 kb
#include<bits/stdc++.h>
using namespace std;
class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
}f("pscnv.in");
ofstream g("pscnv.out");
int n,m,x,y;
vector<int>RoziIsShit[250002];
vector<short>RoseCost[250002];
int sz[250002];
struct nod
{
    int a,b,cost;
};
nod v[500001];
bool test(nod a, nod b)
{
    if(a.a==b.a && a.b==b.b)
        return a.cost<b.cost;
    if(a.a==b.a)
        return a.b<b.b;
    return a.a<b.a;
}
bool viz[250002];
bool bfs(int dist)
{
    deque<int>d;
    d.push_back(x);
    memset(viz,0,n+1);
    viz[x]=1;
    while(!d.empty())
    {
        for(int i=0;i<sz[d.front()];++i)
            if(viz[RoziIsShit[d.front()][i]]==0 && RoseCost[d.front()][i]<=dist){
                viz[RoziIsShit[d.front()][i]]=1,d.push_back(RoziIsShit[d.front()][i]);
                if(RoziIsShit[d.front()][i]==y)
                    return 1;
            }
        d.pop_front();
    }
    return 0;
}
int main()
{
    f>>n>>m>>x>>y;
    for(int i=1;i<=m;++i)
        f>>v[i].a>>v[i].b>>v[i].cost;
    sort(v+1,v+m+1,test);
    for(int i=1;i<=m;++i)
        if(v[i].a!=v[i].b)
            if(v[i].a!=v[i-1].a || v[i].b!=v[i-1].b){
                RoziIsShit[v[i].a].push_back(v[i].b);
                RoseCost[v[i].a].push_back(v[i].cost);
                ++sz[v[i].a];
            }
    int b=1;
    int e=1000;
    int cmin=1001;
    while(b<=e)
    {
        int m=(b+e)/2;
        if(bfs(m)==1)
            cmin=m,e=m-1;
        else
            b=m+1;
    }
    g<<cmin;
    return 0;
}