Cod sursa(job #1601911)

Utilizator lupvasileLup Vasile lupvasile Data 16 februarie 2016 12:37:16
Problema Team Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.83 kb
#include <bits/stdc++.h>
using namespace std;
#ifdef INFOARENA
#define cout fout
#endif // INFOARENA
ifstream fin("team.in");
ofstream fout("team.out");
/// //////////////////////////////////////////

#define nmax 510
#define pmax 60
#define inf 0x3f3f3f3f

vector<pair<int,int>> G[nmax];
int p,n,m;
int d[pmax],dist[nmax],dd[pmax][pmax],dp[pmax][pmax][pmax];
priority_queue<pair<int,int>> q;

void read();
void dijkstra(int nod);
void make_dist()
{
    int i,j,k;
    d[0]=1;
    for(i=0; i<=p; ++i)
    {
        dijkstra(d[i]);
        for(j=0; j<=p; ++j)
            dd[i][j]=dist[d[j]];
    }
    for(i=0; i<=p; ++i)
        for(j=0; j<=p; ++j)
            for(k=0; k<=p; ++k)
                dp[i][j][k]=-1;
}

int get_dp(int k,int x,int y)
{
    if(x>y) return 0;
    if(x==y && x==k) return 0;
    if(dp[k][x][y]>=0) return dp[k][x][y];

    for(int h=x; h<=y; ++h)
        dp[k][x][y]=dp[k][x][y]>=0 ? min(dp[k][x][y],get_dp(h,x,h-1)+get_dp(h,h+1,y)+dd[k][h]):(get_dp(h,x,h-1)+get_dp(h,h+1,y)+dd[k][h]);

    return dp[k][x][y];
}

int main()
{
    read();
    make_dist();
    cout<<get_dp(0,1,p);
    return 0;
}
void read()
{
    int i,j,c;
    fin>>p>>n>>m;
    for(; m; --m)
    {
        fin>>i>>j>>c;
        G[i].push_back({j,c});
        G[j].push_back({i,c});

    }
    for(i=1; i<=n; ++i) fin>>d[i];
}

void dijkstra(int nod)
{
    int i;
    pair<int,int> top;
    for(i=1; i<=n; ++i) dist[i]=inf;
    dist[nod]=0;
    q.push({0,nod});
    while(!q.empty())
    {
        top=q.top();
        q.pop();
        for(auto vec:G[top.second])
        {
            if(dist[vec.first]>-top.first+vec.second)
            {
                dist[vec.first]=-top.first+vec.second;
                q.push({-dist[vec.first],vec.first});
            }
        }
    }
}