Cod sursa(job #922634)

Utilizator alexandru93moraru alexandru sebastian alexandru93 Data 22 martie 2013 15:58:54
Problema Gather Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include<fstream>
#include<algorithm>
#include<queue>
#include<vector>
#define maxn 50010
const int inf = 0x3f3f3f3f;
using namespace std;

struct muchie{
    int y,c,d;
};

vector<muchie> e[maxn];

bool test[maxn];
queue<int> Q;
int D,N,M;
int nrD;
long d[maxn];

ifstream f("gather.in");
ofstream g("gather.out");

int bellman(int S){
    for(int i=1;i<=N;i++)
        d[i]=inf;
    d[S]=0;
    Q.push(S);
    while(!Q.empty()){
        int nod=Q.front();
        Q.pop();
        test[nod]=false;
        for(vector< muchie >::iterator it=e[nod].begin();it!=e[nod].end();++it){
            if(d[it->y]>d[nod]+it->c&&nrD<=it->d){
                d[it->y]=d[nod]+(nrD+1)*it->c;
                Q.push(it->y);
                if(test[it->y]){
                    nrD++;
                    if(nrD==D)
                    return d[it->y];
                }
            }
        }
    }
    return 0;
}

void citire(){
    muchie m;
    int x,y;
    f>>D>>N>>M;
    for(int i=0;i<D;i++){
        f>>x;
        test[x]=true;
    }
    for(int i=1;i<=M;i++){
        f>>x>>y>>m.c>>m.d;
        m.y=y;
        e[x].push_back(m);
        m.y=x;
        e[y].push_back(m);
    }
}

int main(){
    citire();
    g<<bellman(1);
    /*for(int i=2;i<=N;i++)
        if(d[i]==inf)
            g<<0<<' ';
        else
        g<<d[i]<<' ';
        */
    g<<'\n';
    g.close();
    return 0;
}