Cod sursa(job #879656)

Utilizator okros_alexandruOkros Alexandru okros_alexandru Data 15 februarie 2013 18:53:19
Problema Amenzi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <fstream>
#include <vector>
#define nmax 151
#define tmax 3501
#define Vecin G[Nod][i].first
#define Dist G[Nod][i].second
using namespace std;

vector < pair<int,int> > G[nmax];
int N,P,A[tmax][nmax],DP[tmax][nmax];

void solve() {
	
	int i,T,Nod;
	
	DP[0][1]=1;
	
	for(T=1;T<tmax;T++)
		for(Nod=1;Nod<=N;Nod++) {
			
			DP[T][Nod]=DP[T-1][Nod];
			
			for(i=0;i<G[Nod].size();i++)
				if(T-Dist>=0 && DP[T-Dist][Vecin]>DP[T][Nod])
					DP[T][Nod]=DP[T-Dist][Vecin];
				
			if(DP[T][Nod])
				DP[T][Nod]+=A[T][Nod];
			
			}
	
}
void read(ifstream & in) {
	
	int x,y,d,M,K;
	
	in>>N>>M>>K>>P;
	
	while(M--) {
		
		in>>x>>y>>d;
		
		G[x].push_back(make_pair(y,d));
		G[y].push_back(make_pair(x,d));
		
		}
	
	while(K--) {
		
		in>>x>>y>>d;
		A[y][x]+=d;
		
		}
	
}
int main() {
	
	int x,y;
	ifstream in("amenzi.in");
	ofstream out("amenzi.out");
	
	read(in);
	solve();
	
	while(P--) {
		
		in>>x>>y;
		out<<(DP[y][x]-1)<<'\n';
		
		}
	
	in.close();
	out.close();
	
	return 0;
	
}