Cod sursa(job #9817)

Utilizator ciprifilipasFilipas Ciprian ciprifilipas Data 27 ianuarie 2007 17:00:08
Problema Amenzi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.68 kb
//amenzi
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator> 

using namespace std;

typedef struct jaf{
        int vf, t, a;
};

typedef struct wife{
        int vf, t;
};
        
vector< vector<int> > L;
vector<jaf> h;
vector<wife> w;
vector<bool> sel;

int n, m, k, p, amenda;

void Read();
int Timp(int time, int nod);
void Dinamica(int costm, int nod);

ofstream fout("amenzi.out");

int main()
{
    Read();
    for(int i = 1; i <= p; i++)
    {
           fout << Timp(w[i].t, w[i].vf) << "\n";
           amenda = 0;
    }
    fout.close();
    return 0;
}

void Read()
{
     ifstream fin("amenzi.in");
     fin >> n >> m >> k >> p;
     int v1, v2, c;
     int i;
     
     w.resize(p+1);
     sel.resize(k+1);
     L.resize(n+1);
     h.resize(k+1);

     for(i = 1; i <= n; i++)
     {
           L[i].resize(n+1);
           L[i].assign(n+1, 320000);
     }
     
     for(i = 1; i <= m; i++)
     {
           fin >> v1 >> v2 >> c;
           L[v1][v2] = c;
           L[v2][v1] = c;
     }
     for(i = 1; i <= k ;i++)
     {
           fin >> h[i].vf >> h[i].t >> h[i].a;
     }
     for(i = 1; i <= p; i++)
     {
           fin >> w[i].vf >> w[i].t;
     }
     fin.close();
}


int Timp(int time, int nod)
{
    int i, j, r;
    for(r = 1; r <= n; r++)
          for(i = 1; i <= n; i++)
                for(j = 1; j <= n; j++)
                {
                      if(L[i][j] > L[i][r] + L[r][j]) L[i][j] = L[r][j] + L[i][r];
                }
    Dinamica(time, nod);
    return amenda;
}
    
void Dinamica(int costm, int nod)
{
     int vf = 1;
     int maxim = 0;
     int poz;
     int modif;
     while(modif == 1)
     {
             modif = 0;
             maxim = 0;
             for(int j = 1; j <= k; j++)
             {
                     if(h[j].a > maxim && sel[j] == false) 
                     {
                               maxim = h[j].a;
                               poz = j; 
                     }
             }
             if(maxim == 0) modif = 0;
             if(L[vf][h[poz].vf] <= h[poz].t && L[vf][h[poz].vf] + L[h[poz].vf][nod] <= costm) 
             {
                                 costm -= L[vf][h[poz].vf];
                                 vf = h[poz].vf;
                                 amenda += h[poz].a;
                                 sel[poz] = true;
                                 modif = 1;
             }
             else
             {
                 if(L[vf][h[poz].vf] + L[h[poz].vf][nod] > costm) sel[poz] = true;
                 modif = 1;
             }
     }
}