Cod sursa(job #2061831)

Utilizator stefdascalescuStefan Dascalescu stefdascalescu Data 9 noiembrie 2017 19:10:41
Problema PScNv Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.58 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<int>RoseCost[250002];
int sz[250002],cost[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];
void bfs()
{
    deque<int>d;
    d.push_back(x);
    memset(viz,0,n+1);
    viz[x]=1;
    int ss=1;
    while(ss)
    {
        for(int i=0;i<sz[d[0]];++i)
            if(viz[RoziIsShit[d[0]][i]]==0 || max(cost[d[0]],RoseCost[d[0]][i])<cost[RoziIsShit[d[0]][i]]){
                viz[RoziIsShit[d[0]][i]]=1;
                d.push_back(RoziIsShit[d[0]][i]);
                cost[RoziIsShit[d[0]][i]]=max(cost[d[0]],RoseCost[d[0]][i]);
                ++ss;
            }
        --ss;
        d.pop_front();
    }
    g<<cost[y];
}
int main()
{
    f>>n>>m>>x>>y;
    int l=0;
    int a,b,cst;
    for(int i=1;i<=m;++i){
        f>>a>>b>>cst;
        if(a!=b)
            ++l,v[l].a=a,v[l].b=b,v[l].cost=cst;
    }
    sort(v+1,v+l+1,test);
    for(int i=1;i<=l;++i)
        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];
        }
    bfs();
    return 0;
}