Cod sursa(job #1601900)

Utilizator lupvasileLup Vasile lupvasile Data 16 februarie 2016 12:28:02
Problema Team Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 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 501
#define pmax 51
#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;
}
#define maxb 10000
int pos(maxb);
char buf[maxb];

int gi()
{
    int n=0;
    while(buf[pos]<'0' || buf[pos]>'9')
        if(++pos>=maxb) fin.read(buf,maxb),pos=0;

    while((buf[pos]>='0' && buf[pos]<='9'))
    {
        n=n*10+buf[pos]-'0';
        if(++pos>=maxb) fin.read(buf,maxb),pos=0;
    }
    return n;
}

void read()
{
    int i,j,c;
    p=gi();
    n=gi();
    m=gi();
    //fin>>p>>n>>m;
    for(; m; --m)
    {
        i=gi();
        j=gi();
        c=gi();
        //fin>>i>>j>>c;
        G[i].push_back({j,c});
        G[j].push_back({i,c});

    }
    for(i=1; i<=n; ++i)
        d[i]=gi();
        //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});
            }
        }
    }
}