求调!!!!!!!!!!!!!!!!!!!1
查看原帖
求调!!!!!!!!!!!!!!!!!!!1
1114241
vectorxyz楼主2025/1/23 19:43

一直输出 -0x3f。。。

#include <bits/stdc++.h>

using namespace std;

const int N = (5e5 + 5) * 3;
int c[N];
struct node
{
    int v, w;
};
vector<node> g[N];
bool vis[N];
int dis[N];
void spfa(int s){
    queue<int> q;
    memset(dis, -0x3f, sizeof(dis));
    q.push(s);
    dis[s] = 0;
    vis[s] = 1;
    while(q.size()){
        int u = q.front(); q.pop();
        vis[u] = 0;
        for(int i = 0;i < g[u].size();i ++ ){
            int v = g[u][i].v, w = g[u][i].w;
            if(dis[u] + w > dis[v]){
                dis[v] = dis[u] + w;
                if(!vis[v]){
                    vis[v] = 1; q.push(v);
                }
            }
        }
    }
}
void work()
{
    int n, m; cin >> n >> m;
    for(int i = 1;i <= n;i ++ ) {
        cin >> c[i];
        g[i].push_back({i + n, -c[i]}), g[i + n].push_back({i + n * 2, c[i]});
    }
    for(int i = 1;i <= m;i ++ ){
        int x, y, z; cin >> x >> y >> z;
        if(z == 1){
            g[x].push_back({y, 0});
            g[x + n].push_back({y + n, 0});
            g[x + n * 2].push_back({y + n * 2, 0});
        }
        else {
            g[y].push_back({x, 0});
            g[y + n].push_back({x + n, 0});
            g[y + n * 2].push_back({x + n * 2, 0});
        }
    }
    spfa(1);
    cout << max(dis[n], dis[n * 3]) << endl;
}
    
int main(){ work(); }
2025/1/23 19:43
加载中...