Cod sursa(job #921376)

Utilizator ChallengeMurtaza Alexandru Challenge Data 20 martie 2013 22:43:04
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <fstream>

using namespace std;

const char InFile[]="amenzi.in";
const char OutFile[]="amenzi.out";
const int MaxN=155;
const int MaxT=3566;
const int INF=1<<29;

ifstream fin(InFile);
ofstream fout(OutFile);

int N,M,K,P,x,y,c,Cost[MaxN][MaxN],Best[MaxT][MaxN],Sum[MaxT][MaxN];

int main()
{
	fin>>N>>M>>K>>P;
	for(register int i=1;i<=N;++i)
	{
		for(register int j=1;j<=N;++j)
		{
			Cost[i][j]=INF;
		}
	}

	for(register int i=1;i<=M;++i)
	{
		fin>>x>>y>>c;
		Cost[x][y]=Cost[y][x]=min(Cost[x][y],c);
	}
	for(register int i=1;i<=K;++i)
	{
		fin>>x>>y>>c;
		Sum[y][x]+=c;
	}

	for(register int k=1;k<=N;++k)
	{
		for(register int i=1;i<=N;++i)
		{
			for(register int j=1;j<=N;++j)
			{
				int newCost=Cost[i][k]+Cost[k][j];
				if(newCost<Cost[i][j])
				{
					Cost[i][j]=newCost;
				}
			}
		}
	}
	
	for(register int t=0;t<MaxT;++t)
	{
		for(register int i=1;i<=N;++i)
		{
			Best[t][i]=-1;
		}
	}
	Best[0][1]=0;
	for(register int t=0;t<MaxT-1;++t)
	{
		for(register int i=1;i<=N;++i)
		{
			if(Best[t][i]==-1)
			{
				continue;
			}
			Best[t][i]+=Sum[t][i];
			if(Best[t][i]>Best[t+1][i])
			{
				Best[t+1][i]=Best[t][i];
			}

			if(Sum[t][i] || i==1)
			{
				for(register int j=1;j<=N;++j)
				{
					int newt=t+Cost[i][j];
					if(newt>=MaxT)
					{
						continue;
					}
					if(Best[t][i]>Best[newt][j])
					{
						Best[newt][j]=Best[t][i];
					}
				}
			}
		}
	}

	for(register int i=1;i<=P;++i)
	{
		fin>>x>>y;
		fout<<Best[y][x]<<"\n";
	}
	fin.close();
	fout.close();
	return 0;
}