Pagini recente » Cod sursa (job #2912520) | Cod sursa (job #2221449) | Cod sursa (job #1638860) | Cod sursa (job #2652544) | Cod sursa (job #2350948)
#include <bits/stdc++.h>
#define Nmax 155
#define Tmax 3505
#define DIM 70000
#define Pmax 8005
using namespace std;
class Ifstream{
private:
FILE *fin;
char *buffer;
int cursor;
char read_ch(){
if(++cursor==DIM){
cursor=0;
fread(buffer,1,DIM,fin);
}
return buffer[cursor];
}
public:
Ifstream(const char *file_name){
fin=fopen(file_name,"r");
buffer=new char[DIM]();
cursor=DIM-1;
}
Ifstream &operator >> (int &n){
char c;
n=0;
while(!isdigit(c=read_ch()) and c!='-');
int sgn=1;
if(c=='-') sgn=-1;
else n=c-'0';
while(isdigit(c=read_ch()))
n=10*n+c-'0';
n*=sgn;
return *this;
}
};
class Ofstream{
private:
FILE *fout;
char *buffer;
int cursor;
void write_ch(char ch){
if(cursor==DIM){
fwrite(buffer,1,DIM,fout);
cursor=0;
}
buffer[cursor++]=ch;
}
public:
Ofstream(const char *file_name){
fout=fopen(file_name,"w");
buffer=new char[DIM]();
cursor=0;
}
~Ofstream(){
fwrite(buffer,1,cursor,fout);
fclose(fout);
}
Ofstream &operator << (int n){
if(n<0){
write_ch('-');
n=-n;
}
if(n<10) write_ch(n+'0');
else{
(*this)<<(n/10);
write_ch(n%10+'0');
}
return *this;
}
Ofstream &operator << (char c){
write_ch(c);
return *this;
}
};
Ifstream f("amenzi.in");
Ofstream g("amenzi.out");
int N,M,K,P,T;
list < pair <int,int> > G[Nmax];
pair <int,int> meeting[Pmax];
int dp[Nmax][Tmax];
int fine[Nmax][Tmax];
void read_data(){
f>>N>>M>>K>>P;
int x,y,z;
while(M--){
f>>x>>y>>z;
G[x].push_back({y,z});
G[y].push_back({x,z});
}
while(K--){
f>>x>>y>>z;
fine[x][y]+=z;
}
for(int i=1;i<=P;i++){
f>>meeting[i].first>>meeting[i].second;
T=max(T,meeting[i].second);
}
}
void compute_dp(){
for(int j,i=0;i<=N;i++)
for(j=0;j<=T;j++)
dp[i][j]=-1;
dp[1][0]=fine[1][0];
for(int node,t=0;t<=T;t++)
for(node=1;node<=N;node++)
if(dp[node][t]>-1){
dp[node][t+1]=max(dp[node][t+1],dp[node][t]+fine[node][t+1]);
for(const auto &it:G[node])
dp[it.first][t+it.second]=max(dp[it.first][t+it.second],dp[node][t]+fine[it.first][t+it.second]);
}
}
void print_ans(){
for(int i=1;i<=P;i++)
g<<dp[meeting[i].first][meeting[i].second]<<'\n';
}
int main(){
read_data();
compute_dp();
print_ans();
return 0;
}