Cod sursa(job #679165)

Utilizator rootsroots1 roots Data 12 februarie 2012 20:30:22
Problema Gather Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>

#define maxN 751
#define maxK 16

#define INF 0x7fffffff

using namespace std;

struct gr
{
    int nod,cod;
};

vector <int> g[maxN];
vector <gr> z[maxK];

int dist[maxN][maxN];
int cnt[maxN][maxN];
int dm[maxK][maxK];
int d[maxN];
int h[maxN];

int v[maxK];

int K,N;

inline bool cmp(const int &a,const int &b)
{
    return d[a]>d[b];
}

inline void dij(int nod,int num)
{
    memset(h,0,sizeof(h));
    for(int i=1;i<=N;++i) d[i]=INF;

    h[0]=1;
    h[1]=nod;
    d[nod]=0;

    for(;h[0];)
    {
        nod=h[1];
        pop_heap(h+1,h+h[0]+1,cmp);
        --h[0];

        for(vector <int>::iterator it=g[nod].begin();it!=g[nod].end();++it)
            if(d[*it]>d[nod]+dist[nod][*it]&&num<=cnt[nod][*it])
            {
                d[*it]=d[nod]+dist[nod][*it];
                h[++h[0]]=*it;
                push_heap(h+1,h+h[0]+1,cmp);
            }
    }
}

ifstream in;
ofstream out;

int main()
{
    int M,x,y;

    in.open("gather.in");

    in>>K>>N>>M;
    for(int i=1;i<=K;++i) in>>v[i];
    for(;M--;)
    {
        in>>x>>y;
        in>>dist[x][y];
        in>>cnt[x][y];

        cnt[y][x]=cnt[x][y];
        dist[y][x]=dist[x][y];

        g[x].push_back(y);
        g[y].push_back(x);
    }

    in.close();

    out.open("gather.out");

    for(int i=1;i<K;++i)
        for(int j=1;j<=K;++j)
        {
            dij(v[j],i);

            for(int k=1;k<=K;++k)
                out<<d[v[k]]<<' ';
            out<<'\n';
        }

    out.close();

    return 0;
}