Cod sursa(job #1470098)

Utilizator blasterzMircea Dima blasterz Data 10 august 2015 13:31:01
Problema Cerere Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.68 kb
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define N 16001

using namespace std;

typedef struct edge {
    int end, cost;
} edge;

vector<edge*> edges[2 * N];
queue<int> q;
unsigned int costs[N], forts[N];

unsigned int n, m, k, i, f, s, e, c;

inline edge *newedge(int end, int cost) {
    edge *ed;
    ed = (edge*) malloc(sizeof(edge));
    ed->end = end;
    ed->cost = cost;
    return ed;
}

void solve() {
    int curr, end, cost;
    while(! q.empty()) {
        curr = q.front();
        q.pop();
        for(vector<edge*>::iterator it = edges[curr].begin(); it != edges[curr].end(); it++) {
            end = (*it)->end; cost = (*it)->cost;
            if (costs[curr] + cost < costs[end]) {
                costs[end] = costs[curr] + cost;
                forts[end] = forts[curr];
                q.push(end);
             } else if (costs[curr] + cost == costs[end] && forts[curr] < forts[end]) {
                 forts[end] = forts[curr];
             }
        }
    }
}

int main() {
    FILE *fi = freopen("catun.in", "r", stdin);
    FILE *fo = freopen("catun.out", "w", stdout);
    cin >> n >> m >> k;
    memset(costs, 0x3f3f3f3f, N);
    for (i = 0; i < k; i++) {
        cin >> f;
        q.push(f);
        forts[f] = f;
        costs[f] = 0;
    }
    for (i = 0; i < m; i++) {
        cin >> s >> e >> c;
        edges[s].push_back(newedge(e, c));
        edges[e].push_back(newedge(s, c));
    }
    solve();
    for (i = 1; i <= n; i++) {
        cout << (forts[i] == i? 0: forts[i]) << " ";
    }
    fclose(fi);
    fclose(fo);
    return 0;
}
[root@arch-mercury ~]# vim t.cpp
[root@arch-mercury ~]#
[root@arch-mercury ~]# ls
_activate_proxy.sh  bin       catun.out      config              core  downloads  mongoscripts  t.cpp   x
_disable_proxy.sh   catun.in  composer.json  consumers-start.sh  cpp   html       r.php         vendor  yt
[root@arch-mercury ~]# mkdi9r cerere
-bash: mkdi9r: command not found
[root@arch-mercury ~]# mkdir cerere
[root@arch-mercury ~]# cd cerere/
[root@arch-mercury cerere]# vim cerere.cpp
[root@arch-mercury cerere]# g++ -O2 -o x cerere.cpp
[root@arch-mercury cerere]# vim cerere.in
[root@arch-mercury cerere]# ./x
0 1 0 1 2 0 1 1 2 1
[root@arch-mercury cerere]#
[root@arch-mercury cerere]# vim cerere.
[root@arch-mercury cerere]# vim cerere.cpp
[root@arch-mercury cerere]# g++ -O2 -o x cerere.cpp
[root@arch-mercury cerere]# ./x
[root@arch-mercury cerere]# cat cerere.out
0 1 0 1 2 0 1 1 2 1
[root@arch-mercury cerere]# cat cerere.cpp
#include <cstdio>
#include <vector>

using namespace std;

#define N 100001

vector<int> a[N];

int parent[N];

int st[N];
int n;

int g[N];
int root = 0;
bool used[N];
int nst;

int sol[N];

void dfs(int u) {
    used[u] = true;
    st[++nst] = u;

    if (nst - parent[u] >= 1 && parent[u] != 0)
        sol[u] = sol[ st[ nst - parent[u] ] ] + 1;

    for (vector<int>::iterator it = a[u].begin(); it != a[u].end(); ++it)
        if (!used[*it]) {
            dfs(*it);
        }

    --nst;
}


int main() {

    freopen ("cerere.in", "r", stdin);
    freopen ("cerere.out", "w", stdout);
    scanf ("%d\n", &n);
    int i;
    for (i = 1; i <= n; ++i)
        scanf ("%d ", &parent[i]);

    int p, q;

    for (i = 1; i < n; ++i) {
        scanf ("%d %d\n", &p, &q);
        a[p].push_back(q);
        //a[q].push_back(p);
        g[q]++;
    }

    for (i =1 ;i <= n; ++i)
        if (!g[i])
            root = i;

    dfs(root);

    for (i = 1; i <= n; ++i)
        printf ("%d ", sol[i]);
    printf ("\n");
}