Cod sursa(job #295820)

Utilizator toni2007Pripoae Teodor Anton toni2007 Data 3 aprilie 2009 18:17:16
Problema Team Scor 0
Compilator cpp Status done
Runda Simulare Marime 2.13 kb
#include <cstdio>
#include <vector>
#include <set>

using namespace std;

#define maxP    52
#define maxN    512
#define oo      100000000
#define ff      first
#define ss      second
#define pb      push_back

vector <int> A[maxN], C[maxN];
int N, M, P, D[maxN], V[maxN], Sol, Pred[maxN], viz[maxN];
set < pair <int,int> > S;

void dijkstra () {
        S.insert(make_pair(0, 1));
        for (int i = 2; i <= N; ++ i)
                V[i] = oo;

        while (!S.empty()) {
                pair <int,int> now = * (S.begin()); S.erase(S.begin());
                for (int i = 0; i < A[now.ss].size(); ++ i)
                        if (V[A[now.ss][i]] > now.ff + C[now.ss][i]) {
                                V[A[now.ss][i]] = now.ff + C[now.ss][i];
                                Pred[A[now.ss][i]] = now.ss;
                                S.insert(make_pair(V[A[now.ss][i]], A[now.ss][i]));
                        }

        }
}

int main () {
        int i, a, b, c, x, j;

        freopen("team.in", "r", stdin);
        freopen("team.out", "w", stdout);

        scanf("%d%d%d", &P, &N, &M);

        for (i = 1; i <= M; ++ i) {
                scanf("%d%d%d", &a, &b, &c);
                A[a].pb(b);
                C[a].pb(c);
                A[b].pb(a);
                C[b].pb(c);
        }

        for (i = 1; i <= P; ++ i) {
                scanf("%d", &x);
                D[x] ++;
        }

        dijkstra ();

        for (i = 1; i <= N; ++ i)
                printf("%d ", Pred[i]);
        puts("");

        for (i = 2; i <= N; ++ i) {
                if (!viz[i] && D[i]) {
                        for (j = Pred[i]; j != 1; j = Pred[j])
                                if (viz[j]) {
                                        V[i] -= V[j];
                                        break ;
                                }
                                else
                                        viz[j] = 1;
                }
        }

        for (i = 1; i <= N; ++ i)
                printf("%d ", D[i]);
        puts("");

        for (i = 1; i <= N; ++ i)
                if (D[i])
                        Sol += V[i];
        printf("%d\n", Sol);
}