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

费用流水过(937ms)

Posted by Ioencgc at 2023-08-08 23:54:01 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;
struct edge
{
    int to, cap, cost, rev;
    edge (int t, int ca, int c, int r):
        to(t), cap(ca), cost(c), rev(r){}
};
vector<edge> G[MAX_V];
int h[MAX_V], dist[MAX_V], V, prevv[MAX_V], preve[MAX_V], P[101], Q[101], B[101], X[101], Y[101], C[101];
typedef pair<int, int> Pa;

inline void add_edge(int from, int to, int cap, int cost)
{
    G[from].push_back(edge(to, cap, cost, G[to].size()));
    G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}

int min_cost_flow(int s, int t, int f)
{
    int res = 0;
    priority_queue<Pa, vector<Pa>, greater<Pa> > que;
    while (f > 0)
    {
        fill(dist, dist + V + 1, INT_MAX);
        dist[s] = 0;
        que.push(Pa(0, s));
        for (int v, dis; !que.empty(); )
        {
            v = que.top().second,
            dis = que.top().first;
            que.pop();
            if (dis > dist[v])
                continue;
            for (int i = 0; i < G[v].size(); ++i)
            {
                edge& e = G[v][i];
                if (e.cap > 0 && dist[e.to] > dis + e.cost + h[v] - h[e.to])
                {
                    dist[e.to] = dis + e.cost + h[v] - h[e.to],
                    prevv[e.to] = v,
                    preve[e.to] = i;
                    que.push(Pa(dist[e.to], e.to));
                }
            }
        }
        if (dist[t] == INT_MAX)
            return -1;
        for (int v = 0; v <= V; ++v)
            h[v] += dist[v];
        int d = f;
        for (int v = t; v != s; v = prevv[v])
            d = min(d, G[prevv[v]][preve[v]].cap);
        f -= d,
        res += h[t] * d;
        for (int v = t; v != s; v = prevv[v])
        {
            edge& e = G[prevv[v]][preve[v]];
            e.cap -= d,
            G[v][e.rev].cap += d;
        }
    }
    return res;
}

int main()
{
    int n, m, s = 0, t, cost = 0, f = 0;
    scanf("%d%d", &n, &m);
    t = n + m + 1, V = t;
    for (int i = 1; i <= n; ++i)
        scanf("%d%d%d", &X[i], &Y[i], &B[i]);
    for (int i = 1; i <= m; ++i)
        scanf("%d%d%d", &P[i], &Q[i], &C[i]);
    for (int i = 1; i <= n; ++i)
    {
        for (int j = 1; j <= m; ++j)
        {
            int e, c = abs(X[i] - P[j]) + abs(Y[i] - Q[j]) + 1;
            scanf("%d", &e);
            add_edge(i, n + j, INT_MAX, c);
            cost += e * c;
        }
    }
    for (int i = 1; i <= n; ++i)
    {
        add_edge(s, i, B[i], 0);
        f += B[i];
    }
    for (int i = 1; i <= m; ++i)
    {
        add_edge(n + i, t, C[i], 0);
    }
    if (min_cost_flow(s, t, f) < cost)
    {
        puts("SUBOPTIMAL");
        for (int i = 0; i < n; ++i)
            for (int j = 1; j <= m; ++j)
                printf("%d%c", G[n + j][i].cap, j == m ? '\n' : ' ');
    }
    else
        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