Cod sursa(job #892541)

Utilizator marianfFocsa Marian marianf Data 26 februarie 2013 10:28:23
Problema Ubuntzei Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include<cstdio>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
struct coord
{
    int c,x;
};
vector <coord> L[2010];
queue <int> q;
int n,m,loc[20],nloc,d[16][2001];

void Citire()
{
    int x,y,c;
    coord aux;
    freopen("ubuntzei.in","r",stdin);
    scanf("%d%d%d",&n,&m,&nloc);
    for(int i=1;i<=nloc;i++)
        scanf("%d",&loc[i]);
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&c);
        aux.x = y;
        aux.c = c;
        L[x].push_back(aux);
        aux.x = x;
        L[y].push_back(aux);
    }
}
void Bellmanford(int x)
{
    int k,i;
    for(i=1;i<=n;i++)
        d[x][i] = 2000000000;
    d[x][x] = 0;

    coord aux;
    q.push(x);
    while(!q.empty())
    {
        k=q.front();
        q.pop();
        int N=L[k].size();
        for(i=0;i<N;i++)
        {
            aux=L[k][i];
            if(d[x][aux.x] > d[x][k] + aux.c)
            {
                d[x][aux.x]=d[x][k]+aux.c;
                q.push(aux.x);
            }
        }
    }
}

void Solve()
{
    Bellmanford(1);
    if(nloc==0)
    {
        freopen("ubuntzei.out","w",stdout);
        printf("%d",d[1][n]);
        return ;
    }

    int i;
    for(i=1;i<=nloc;i++)
        Bellmanford(loc[i]);


}
int main()
{
    Citire();
    Solve();
    return 0;
}