Pagini recente » Cod sursa (job #1848033) | Cod sursa (job #1333868) | Cod sursa (job #1038317) | Cod sursa (job #80112) | Cod sursa (job #45434)
Cod sursa(job #45434)
#include <stdio.h>
#include <string.h>
#define fin "amenzi.in"
#define fout "amenzi.out"
#define TMAX 3501
#define NMAX 151
#define MMAX 1500
struct muchie
{
int x1,x2,c; //x1-plecare x2-sosire c-timp
} A[MMAX];
FILE * f;
int s[TMAX][NMAX],amenda[TMAX][NMAX],N,M,K,P;
void readData()
{
f = fopen(fin,"r");
fscanf(f,"%d %d %d %d",&N,&M,&K,&P);
for(int i = 0;i<M;++i)
fscanf(f,"%d %d %d",&A[i].x1,&A[i].x2,&A[i].c);
for(int i = 0 ,x,y,c;i<K;++i)
{
fscanf(f,"%d %d %d",&x,&y,&c);
amenda[y][x] = c;
}
}
void init()
{
memset(s,-1,sizeof s);
}
void query()
{
FILE * g = fopen(fout,"w");
for(int i = 0,x,y; i < P ; ++i)
{
fscanf(f,"%d %d",&x,&y);
fprintf(g,"%d\n",s[y][x]);
}
fclose(f);
fclose(g);
}
void calc()
{
int time;
s[0][1] = 0;
for(int t = 1; t<TMAX ; ++t)
{
for(int i = 1 ;i<=N;++i)
s[t][i] = s[t-1][i];
for(int p=0;p<M;++p)
if( t-(time=A[p].c) >= 0 )
{
if(s[t-time][A[p].x1]!=-1)
{
if(s[t-time][A[p].x1] > s[t][A[p].x2])
s[t][A[p].x2] = s[t-time][A[p].x1];
}
if(s[t-time][A[p].x2]!=-1)
{
if(s[t-time][A[p].x2] > s[t][A[p].x1])
s[t][A[p].x1] = s[t-time][A[p].x2];
}
}
for(int i=1;i<=N;++i)
if(s[t][i]!=-1) s[t][i]+=amenda[t][i];
}
}
void afis()
{
FILE * g = fopen(fout,"w");
for(int i=0;i<TMAX;++i) {
for(int j=1;j<=N;++j)
fprintf(g,"%d ",s[i][j]);
fprintf(g,"\n");
}
}
int main()
{
init();
readData();
calc();
query();
return 0;
}