1 条题解

  • 0
    @ 2025-12-12 19:12:34
    #include<bits/stdc++.h>
    using namespace std;
    #define PII pair<int,int>
    const int N=1e5+10;
    vector<PII>G[N];
    int f[N],v[N],n,m,d,p,st;
    int main()
    {
    	cin>>d>>m>>n>>p>>st;
    	for(int i=1;i<=m;i++)
    	{
    		int x,y;cin>>x>>y;
    		G[x].push_back({y,d});
    	}
    	for(int i=1;i<=p;i++)
    	{
    		int x,y,t;cin>>x>>y>>t;
    		G[x].push_back({y,d-t});
    	}
    	deque<int>q;q.push_back(st);int ans=d;f[st]=d;
    	while(!q.empty())
    	{
    		int x=q.front();q.pop_front();
    		v[x]=0;
    		if(ans>n*d)
    		{
    			cout<<-1;
    			return 0;
    		}
    		for(auto i:G[x])
    		{
    			int y=i.first,w=i.second;
    			if(f[y]<f[x]+w)
    			{
    				f[y]=f[x]+w;ans=max(ans,f[y]);
    				if(!v[y])q.push_back(y),v[y]=1;
    			}
    		}
    	}
    	cout<<ans;
    	return 0;
    }
    
    • 1

    信息

    ID
    1737
    时间
    1000ms
    内存
    128MiB
    难度
    8
    标签
    递交数
    19
    已通过
    6
    上传者