60分求助
查看原帖
60分求助
322792
AlexandreLea楼主2021/8/13 23:01
#include <iostream>
#include <iomanip>
#include <stack>
#define SIZE 2001
using namespace std;
int n,p;
int C[SIZE],W[SIZE][SIZE];
int indgree[SIZE],outdgree[SIZE];
void toposort(){
    stack<int> st;
    for(int i=1;i<=n;i++){
        cout<<indgree[i]<<" ";
    }
    cout<<endl;
    while(!st.empty()){
        while(!st.empty()){
            int j=st.top();
            for(int i=1;i<=n;i++){
                if(W[j][i]!=0){
                    C[i]+=W[j][i]*C[j];
                    indgree[i]--;
                }
            }
            indgree[j]--;
            st.pop();
        }
        for(int i=1;i<=n;i++){
            if(indgree[i]==0 && C[i]>0) st.push(i);
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>p;
    for(int i=1;i<=n;i++){
        int c,u;
        cin>>c>>u;
        if(c!=0) C[i]=c;
        else C[i]=-u;
    }
    for(int i=1;i<=p;i++){
        int d,e;
        cin>>d>>e>>W[d][e];
        indgree[e]++;
        outdgree[d]++;
    }
    //debug();
    toposort();
    //debug();
    bool havout=false;
    for(int i=1;i<=n;i++){
        if(outdgree[i]==0 && C[i]>=0){
            cout<<i<<" "<<C[i]<<endl;
            havout=true;
        }
    }
    if(!havout) cout<<"NULL"<<endl;
    return 0;
}
2021/8/13 23:01
加载中...