Cod sursa(job #546405)

Utilizator DraStiKDragos Oprica DraStiK Data 4 martie 2011 21:36:22
Problema Team Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
#include <algorithm>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;

#define pb push_back
#define mp make_pair
#define sc second
#define fs first

#define INF 0x3f3f3f3f
#define DIM 505
#define MAX 55

ifstream fin ("team.in");
ofstream fout ("team.out");

vector <pair <int,int> > g[DIM];
int bst[MAX][MAX][MAX];
int D[MAX],dst[DIM];
int cst[MAX][MAX];
bitset <DIM> viz;
int N,M,P;

struct cmp
{
    bool operator () (const int &a,const int &b)
    {
        return D[a]>D[b];
    }
}; priority_queue <int,vector <int>,cmp> q;

void read ()
{
    int i,x,y,z;

    fin>>P>>N>>M;
    for (i=1; i<=M; ++i)
    {
        fin>>x>>y>>z;
        g[x].pb (mp (y,z));
        g[y].pb (mp (x,z));
    }

    D[0]=1;
    for (i=1; i<=N; ++i)
        fin>>D[i];
}


inline void dijkstra (int start)
{
    vector <pair <int,int> > :: iterator it;
    int nod;

    memset (dst,INF,sizeof (dst));
    dst[start]=0;

    for (q.push (start); !q.empty (); )
    {
        nod=q.top (); q.pop (); viz[nod]=0;
        for (it=g[nod].begin (); it!=g[nod].end (); ++it)
            if (dst[nod]+it->sc<dst[it->fs])
            {
                dst[it->fs]=dst[nod]+it->sc;
                if (!viz[it->fs])
                {
                    viz[it->fs]=1;
                    q.push (it->fs);
                }
            }
    }
}

inline int calc (int x,int y,int start)
{
    int nod;

    if (x>y)
        return 0;

    if (bst[x][y][start]!=INF)
        return bst[x][y][start];

    for (nod=x; nod<=y; ++nod)
        bst[x][y][start]=min (bst[x][y][start],calc (x,nod-1,nod)+calc (nod+1,y,nod)+cst[start][nod]);

    return bst[x][y][start];
}

void solve ()
{
    int i,j;

    for (i=0; i<=P; ++i)
    {
        dijkstra (D[i]);
        for (j=0; j<=P; ++j)
            cst[i][j]=dst[D[j]];
    }

    memset (bst,INF,sizeof (bst));
    fout<<calc (1,P,0);
}

int main ()
{
    read ();
    solve ();

    return 0;
}