Cod sursa(job #612823)

Utilizator SpiderManSimoiu Robert SpiderMan Data 10 septembrie 2011 14:12:37
Problema Team Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.03 kb
# include <cstdio>
# include <cstring>
# include <queue>
# include <vector>
using namespace std;

const char *FIN = "team.in", *FOU = "team.out";
const int MAX_N = 505, MAX_P = 55, oo = 0x3f3f3f3f;

# define x first
# define y second

bool viz[MAX_N];
int N, M, P, dp[MAX_N], dist[MAX_P][MAX_P], d[MAX_P], sol[MAX_P][MAX_P][MAX_P];
vector < pair <int, int> > G[MAX_N];

struct comp {
    bool operator () (const int &A, const int &B) {
        return dp[A] > dp[B];
    }
};

priority_queue < int, vector <int>, comp > Q ;

inline void getmin (int &a, int b) {
    a = a < b ? a : b;
}

void dijkstra (int nod) {
    memset (dp, oo, sizeof (dp)), memset (viz, 0, sizeof (viz));
    for (dp[nod] = 0, viz[nod] = 1, Q.push (nod); ! Q.empty (); ) {
        int act = Q.top (); viz[act] = 0; Q.pop ();
        for (vector < pair <int, int> > :: iterator it = G[act].begin (); it != G[act].end (); ++it)
            if (dp[act] + it -> second < dp[it -> first]) {
                dp[it -> first] = dp[act] + it -> second;
                if (viz[it -> first] == 0) {
                    Q.push (it -> first);
                    viz[it -> first] = 1;
                }
            }
    }
}

int din (int x, int y, int z) {
    if (sol[x][y][z] != oo) return sol[x][y][z];
    if (x > y) return 0;
    for (int i = x; i <= y; ++i)
        getmin (sol[x][y][z], din (x, i - 1, i) + din (i + 1, y, i) + dist[z][i]);
    return sol[x][y][z];
}

int main (void) {
    freopen (FIN, "r", stdin);

    scanf ("%d %d %d", &P, &N, &M);
    for (int i = 1, x, y, z; i <= M; ++i) {
        scanf ("%d %d %d", &x, &y, &z);
        G[x].push_back (make_pair (y, z));
        G[y].push_back (make_pair (x, z));
    }
    for (int i = 0; i <= P; ++i)
        i ? scanf ("%d", d + i) : d[i] = 1;
    for (int i = 0; i <= P; ++i) {
        dijkstra (d[i]);
        for (int j = 0; j <= P; ++j)
            dist[i][j] = dp[d[j]];
    }
    memset (sol, oo, sizeof (sol));
    fprintf (fopen (FOU, "w"), "%d", din (1, P, 0));
}