30分求助
  • 板块P2648 赚钱
  • 楼主guyanxi11
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/12/5 20:38
  • 上次更新2023/12/5 22:51:24
查看原帖
30分求助
1090453
guyanxi11楼主2023/12/5 20:38
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 10000;

int h[N],e[N*N],ne[N*N], w[N*N],idx, ans = -0x3f3f3f3f;
int dist[N],cnt[N];
bool st[N];
int D,P,C,F;

void add(int a,int b,int c){
    e[idx] = b, ne[idx] = h[a], w[idx] = c,h[a] = idx++;
    
}

bool spfa(int x){
    memset(dist, 0x3f , sizeof dist);
    memset(st, 0 , sizeof st);
    memset(cnt, 0 , sizeof cnt);
    dist[x] = -D;
    queue<int> q;
    q.push(x);
    st[x] = true;
    
    while(q.size()){
        int k = q.front();
        q.pop();
        
        for(int i = h[k]; i != -1; i = ne[i]){
            int j = e[i];
            
            if(dist[j] >  dist[k] + w[i]){
                dist[j] = dist[k] + w[i];
                
                cnt[j] = cnt[k] + 1;
                if(cnt[j] >= C) return false;
                
                if(!st[j]){
                    
                    q.push(j);
                    st[j] = true;
                    
                }
            }
        }
    }
    
    return true;
    
    
}
int main(){
    cin >> D >> P >> C >> F;
    memset(h, -1, sizeof h);
    
    while(P--){
        int a,b;
        cin >> a >> b;
        add(a,b,-D);
        
    }
    
    while(F--){
        int a,b,c;
        cin >> a >> b >> c;
        add(a,b,c - D);
        
    }
    
    for (int i = 1; i <= C; i++) {
    int t = spfa(i);
    if (!t) {
        puts("orz");
        return 0;
    }

        for (int j = 1; j <= C; j++) {
            ans = max(ans, -dist[j]);
        }
    }
    
    cout << ans << endl;
    
    return 0;
    
}

2023/12/5 20:38
加载中...