Cod sursa(job #668449)

Utilizator popoiu.georgeGeorge Popoiu popoiu.george Data 24 ianuarie 2012 22:16:54
Problema Ubuntzei Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.08 kb
#include<stdio.h>
#include<vector>
#define inf "ubuntzei.in"
#define outf "ubuntzei.out"
#define KMax 15
#define NMax 2001
#define INF 0x3f3f3f3f
using namespace std;

int N, M, K;
int dp[1<<KMax][NMax];
vector< pair<int,int> > G[NMax];

int H[NMax], poz[NMax], hdim;
int C[KMax];
int D[NMax], dist[KMax][NMax];

void read()
{
    scanf("%d%d", &N, &M);

    scanf("%d", &K);
    for(int i=0; i<K; i++) scanf("%d", &C[i]);

    int x, y, z;
    for(int i=1; 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) );
    }
}

void upheap(int n, int *d) {
    int f;
    while( n>1 ) {
        f = n>>1;
        if( d[ H[n] ] < d[ H[f] ] ) {
            swap( poz[ H[n] ], poz[ H[f] ] ); swap( H[n], H[f] );
            n = f;
        }
        else return;
    }
}

void downheap(int n, int *d) {
    int s;
    while( n<hdim ) {
        s = n<<1;
        if( s<=hdim ) {
            if( s+1<=hdim && d[ H[s+1] ] < d[ H[s] ] ) s++;
            if( d[ H[s] ] < d[ H[n] ] ) {
                swap( poz[ H[s] ], poz[ H[n] ] ); swap( H[s], H[n] );
                n = s;
            }
            else return;
        }
        else return;
    }
}

void dijkstra(int s, int *d) {
    for(int i=1; i<=N; i++) d[i] = INF, poz[i] = 0;

    hdim = 1; H[1] = s; poz[s] = 1; d[s] = 0;
    while( hdim ) {
        int u = H[1];
        H[1] = H[hdim]; poz[ H[hdim] ] = 1; hdim--; downheap(1, d);

        for(int i=0; i<G[u].size(); i++) {
            int v = G[u][i].first, w = G[u][i].second;
            if( d[v] > d[u] + w ) {
                d[v] = d[u] + w;
                if( poz[v] ) upheap( poz[v], d );
                else {
                    hdim++; H[hdim] = v; poz[v] = hdim;
                    upheap(hdim, d);
                }
            }
        }
    }
}

void solve()
{
    dijkstra(1, D);

    if( K==0 ) {
        printf("%d", D[N]);
        return;
    }
}

int main()
{
	freopen(inf,"r",stdin); freopen(outf,"w",stdout);
	read(); solve();
	return 0;
}