Cod sursa(job #3279257)

Utilizator Dia3141Costea Diana Stefania Dia3141 Data 22 februarie 2025 12:01:53
Problema Amenzi Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>
#include <queue>
#include <vector>
#define int long long
#define tmax 3505
using namespace std;
ifstream cin("amenzi.in");
ofstream cout("amenzi.out");
int n,m,k,p,x,y,t,dp[151][tmax],a[151][tmax];
bool viz[151][tmax];
struct elem{
    int x,t;
};
vector<elem>v[151];
queue<elem>q;
void dijkstra(){
    for(int i=1;i<=n;i++)
        for(int j=0;j<tmax;j++)
            dp[i][j]=-1;
    dp[1][0]=0;
    viz[1][0]=1;
    q.push({1,0});
    while(!q.empty()){
        int nod=q.front().x,t=q.front().t;
        q.pop();
        viz[nod][t]=0;
        for(auto i:v[nod])
            if(t+i.t<tmax&&dp[i.x][t+i.t]<dp[nod][t]+a[i.x][t+i.t]){
                dp[i.x][t+i.t]=dp[nod][t]+a[i.x][t+i.t];
                if(!viz[i.x][t+i.t])
                    q.push({i.x,t+i.t}),viz[i.x][t+i.t]=1;
            }
    }
}
signed main()
{
    cin>>n>>m>>k>>p;
    for(int i=1;i<=m;i++){
        cin>>x>>y>>t;
        v[x].push_back({y,t});
        v[y].push_back({x,t});
    }
    for(int i=1;i<=n;i++)
        v[i].push_back({i,1});
    for(int i=1;i<=k;i++){
        cin>>x>>y>>t;
        a[x][y]+=t;
    }
    dijkstra();
    for(int i=1;i<=p;i++){
        cin>>x>>y;
        cout<<dp[x][y]<<'\n';
    }
    return 0;
}