Cod sursa(job #2377025)

Utilizator flee123Flee Bringa flee123 Data 8 martie 2019 21:00:42
Problema Algoritmul lui Dijkstra Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.91 kb
#include <bits/stdc++.h>
#define nmax 100002
#define infinity 1200000000
using namespace std;
class blahblahin {
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:
	blahblahin(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}
	blahblahin& 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;
	}
	blahblahin& 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;
	}
};
class blahblahout {
private:
    FILE *fout;
    char *buff;
    int sp;
    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }
public:
    blahblahout(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~blahblahout() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }
    blahblahout& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }
    blahblahout& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }
    blahblahout& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    blahblahout& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

int n, m, p;
int cost_of_distance[nmax], sols[nmax];
bool checked[nmax];
struct dublu{
    int dest, cost;
};

struct comp{
    bool operator()(int x, int y)
    {
        return cost_of_distance[x]  > cost_of_distance[y];
    }
};
vector<dublu> graphs[nmax];
priority_queue <int, vector<int>, comp> prq;
void init()
{
    int i;
    for(i = 1; i <= n; i++)
        cost_of_distance[i] = infinity;
}

void dijkstra(int nod_start)
{
    int nod, i, len, nod2;
    cost_of_distance[nod_start] = 0;
    prq.push(nod_start);
    while(!prq.empty())
    {
        nod = prq.top();
        prq.pop();
        checked[nod] = false;
        len = graphs[nod].size();
        for(i = 0; i < len; i++)
            if(cost_of_distance[graphs[nod][i].dest] > cost_of_distance[nod] + graphs[nod][i].cost)
            {
                cost_of_distance[graphs[nod][i].dest] = cost_of_distance[nod] + graphs[nod][i].cost;
                if(!checked[graphs[nod][i].dest])
                {
                    prq.push(graphs[nod][i].dest);
                    checked[graphs[nod][i].dest] = 1;
                }
            }
    }
}

int main()
{
    blahblahin fin("dijkstra.in");
    blahblahout fout("dijkstra.out");
    int i, a, b, c;
    dublu var;
    fin >> n >> m;
    init();
    for(i = 1; i <= m; i++)
    {
        fin >> a >> b >> var.cost;
        var.dest = b;
        graphs[a].push_back(var);
        var.dest = a;
        graphs[b].push_back(var);
    }
    dijkstra(1);
    for(i = 2; i <= n; i++)
    {
        if(cost_of_distance[i] == infinity)
            fout << -1 << ' ';
        else fout << cost_of_distance[i] << ' ';
    }
    return 0;
}