Cod sursa(job #1861926)

Utilizator ImGeluGelu Ungur ImGelu Data 29 ianuarie 2017 13:35:22
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda becreative5 Marime 1.36 kb
#include <fstream>
#include <vector>
#include <iostream>
#include <limits.h>

using namespace std;

int n, m, k;
int ns[17];
vector<int> graf[2001];
vector<int> cost[2001];
int dist[17][2001];
int distanta;

const int oo = 0x3f3f3f3f;

void bellman_ford(int p, int s)
{
    bool ok = true;
    for (int i = 1; i <= n; i++)
        dist[p][i] = oo;
    dist[p][s] = 0;
    for (int i = 1; (i <= n) && (ok == true); i++)
    {
        ok = false;
        for (int j = 1; j <= n; j++)
        {
            for (int k = 0; k < graf[j].size(); k++)
            {
                if (dist[p][graf[j][k]] > (dist[p][j] + cost[j][k]))
                {
                    dist[p][graf[j][k]] = dist[p][j] + cost[j][k];
                    ok = true;
                }
            }
        }
    }
}
inline void rezolvare()
{
    ns[0]=1;
    ns[k+1]=n;
    for(int i=0; i<=k; i++) bellman_ford(i, ns[i]);
    distanta = oo;
}
int main()
{
    int a, b, c;
    ifstream fin("ubuntzei.in");
    ofstream fout("ubuntzei.out");

    fin>>n>>m>>k;
    for(int i=1; i<=k; i++) fin>>ns[i];
    for (int i = 0; i < m; i++)
    {
        fin>>a>>b>>c;
        graf[a].push_back(b);
        graf[b].push_back(a);
        cost[a].push_back(c);
        cost[b].push_back(c);
    }

    rezolvare();

    fout<<distanta<<'\n';
    return 0;
}