제출 #555859

#제출 시각아이디문제언어결과실행 시간메모리
555859mihnea_tudorRestore Array (RMI19_restore)C++17
7 / 100
1084 ms788 KiB
#include <bits/stdc++.h>

#define NMAX ((int)5e3)
#define INF  ((int)1e9)

using namespace std;

struct edge {
    int dest, cost;
};

int n, m;
vector<edge> a[NMAX + 1];

int dist[NMAX + 1];
int pred[NMAX + 1];
bool in_queue[NMAX + 1];
int cnt_in_queue[NMAX + 1];

bool bellmanford() {
    for (int i = 0; i <= n; ++i) {
        dist[i] = INF;
        pred[i] = -1;
    }

    dist[0] = 0;

    queue<int> q;

    q.push(0);
    in_queue[0] = true;

    while (!q.empty()) {
        int node = q.front();
        q.pop();
        in_queue[node] = false;

        for (auto it = a[node].begin(); it != a[node].end(); ++it) {
            if (dist[node] < INF) {
                edge next = *it;
                if (dist[next.dest] > dist[node] + next.cost) {
                    dist[next.dest] = dist[node] + next.cost;

                    if (!in_queue[next.dest]) {
                        if (cnt_in_queue[next.dest] > n + 1) {
                            return false;
                        } else {
                            q.push(next.dest);
                            in_queue[next.dest] = true;
                            ++cnt_in_queue[next.dest];
                        }
                    }
                }
            }
        }
    }

    return true;
}

void add_constraint(int left, int right, int k, int val) {
    if (val == 0) {
        a[left - 1].push_back({right, (right - left + 1) - k});
    } else {
        a[right].push_back({left - 1, -((right - left + 1) - (k - 1))});
    }
}

int main(void) {
    scanf("%d %d", &n, &m);
    for (int i = 0; i < m; ++i) {
        int left, right, k, val;
        scanf("%d %d %d %d", &left, &right, &k, &val);

        ++left; ++right;
        add_constraint(left, right, k, val);
    }

    for (int i = 1; i <= n; ++i) {
        a[i - 1].push_back({i, 1});
        a[i].push_back({i - 1, 0});
    }

    if (!bellmanford()) {
        printf("-1\n");
        return 0;
    }

    for (int i = 1; i <= n; ++i) {
        printf("%d ", dist[i] - dist[i - 1]);
    }

    printf("\n");

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

restore.cpp: In function 'int main()':
restore.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
restore.cpp:73:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   73 |         scanf("%d %d %d %d", &left, &right, &k, &val);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...