Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

int溢出变成负数了,差错一下午才发现,魂淡阿

Posted by Ioencgc at 2023-08-09 23:39:08 on Problem 2175
#include<cstdio>
#include<climits>
#include<queue>
#include<cstring>
#include<cmath>
//using namespace std;
#define ll long long

const int MAX_V = 202, MAX_N = 101;
int X[MAX_N], Y[MAX_N], B[MAX_N], P[MAX_N], Q[MAX_N], C[MAX_N], E[MAX_N][MAX_N], g[MAX_V][MAX_V],
prev[MAX_V][MAX_V];
bool used[MAX_V];

int main()
{
    int n, m, V;
    scanf("%d%d", &n, &m);
    V = n + m + 1;
    for (int i = 0; i < n; ++i)
        scanf("%d%d%d", &X[i], &Y[i], &B[i]);
    for (int i = 0; i < m; ++i)
        scanf("%d%d%d", &P[i], &Q[i], &C[i]);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j)
            scanf("%d", &E[i][j]);
    std::fill(g[0], g[MAX_V], 5000);
    for (int i = 0; i < V; ++i)
        g[i][i] = 0;
    for (int j = 0; j < m; ++j)
    {
        int sum = 0;
        for (int i = 0; i < n; ++i)
        {
            int c = abs(X[i] - P[j]) + abs(Y[i] - Q[j]) + 1;
            g[i][n + j] = c;
            if (E[i][j] > 0)
                g[n + j][i] = -c;
            sum += E[i][j];
        }
        if (sum > 0)
            g[n + m][n + j] = 0;
        if (sum < C[j])
            g[n + j][n + m] = 0;
    }
    for (int i = 0; i < V; ++i)
        for (int j = 0; j < V; ++j)
            prev[i][j] = i;
    for (int k = 0; k < V; ++k)
    {
        for (int i = 0; i < V; ++i)
        {
            for (int j = 0; j < V; ++j)
            {
                if (g[i][j] > g[i][k] + g[k][j])
                {
                    g[i][j] = g[i][k] + g[k][j];
                    prev[i][j] = prev[k][j];
                    if (i == j && g[i][i] < 0)
                    {
                        memset(used, 0, sizeof(used));
                        for (int v = i; !used[v]; v = prev[i][v])
                        {
                            used[v] = true;
                            if (v != n + m && prev[i][v] != n + m)
                            {
                                if (v >= n)
                                    ++E[prev[i][v]][v - n];
                                else
                                    --E[v][prev[i][v] - n];
                            }
                        }
                        puts("SUBOPTIMAL");
                        for (int x = 0; x < n; ++x)
                            for (int y = 0; y < m; ++y)
                                printf("%d%c", E[x][y], y == m - 1 ? '\n' : ' ');
                        return 0;
                    }
                }
            }
        }
    }
    puts("OPTIMAL");

    return 0;
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator