zoukankan      html  css  js  c++  java
  • Teamwork[HDU4494]

    Teamwork

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 497 Accepted Submission(s): 258


    Problem Description
    Some locations in city A has been destroyed in the fierce battle. So the government decides to send some workers to repair these locations. There are m kinds of workers that were trained for different skills. Each location need some number of some kinds of workers and has a schedule that at what time can the repair begins, and the time cost of repair. Any job cannot begin until all the workers required arrived.
    For example, location 1 needs 2 workers of type 1 and 3 workers of type 2, and the beginning time and time cost is 100 minute and 90 minute correspondingly, then 5 workers that satisfy the requirement should arrive before 100 minute, start working at 100 minute and get the job done at 190 minute. Notice that two different types of workers cannot replace each other, so with 3 workers of type 1 and only 2 workers of type 2, this job cannot be done.
    Workers can go from one location to another after their jobs are done. You can take the Euclidean distance between locations as the time workers need to travel between them. Each worker should be sent from a depot initially at 0 minute. Now your task is to determine the minimum number of workers needed to be sent from depot so that all the jobs can be done.

    Input
    There are multiple test cases, the integer on the first line T (T<25) indicates the number of test cases.
    Each test case begins with two integers n (<=150), the number of location(including the depot) and m(<=5), the number of different skills.
    The next line gives two integers x0, y0 indicates the coordinate of depot.
    Then follows n - 1 lines begins with 4 integer numbers: xi, yi, bi(bi>0), pi(pi>0), (xi, yi) gives the coordinate of the i-th location, bi gives the beginning time and pi gives the time cost. The rest of the line gives m non-negative integers v1, v2, ..., vm, of which the i-th number indicates the the number of workers of type i needed (for all vi, 0<=vi<10, each location at least requires one worker).
    All integers are less than 1000000 (106).

    Output
    For each test cases output one line, the minimum workers to be sent. It is guaranteed that there's always a feasible solution that all the jobs can be done.

    Sample Input
    2
    4 1
    0 0
    0 1 1 1 3
    1 1 3 3 4
    1 0 10 1 5
    4 1
    0 0
    0 1 1 1 3
    1 1 3 3 4
    1 0 3 1 5

    Sample Output
    5
    9

    一开始不会,在网上找了一下,看到有一个博客里是这么说的:

    没看明白他是什么意思,为什么要把一个点分成3个点。我拿第一组示例输入画了一下图:

      1是源点,3n+1是汇点,中间的3列是拆分之后的3组点。关键看第三列的点,它的流量有两个来源,一是从源点派出,费用是1,代表这部分工人是另行派出的;二是从其他节点派出,费用是0,代表这部分工人是完成其他任务后转来的。

      顺便,从图里可以看出,确实不用分成3个点,第一列完全没必要。这道题整理了一个最小费用流的模板--->

    #include<iostream>
    #include<cstring>
    #include<cstring>
    #include<cmath>
    #include<cstdio>
    using namespace std;
    const int Maxn = 600;
    int n, m, k;
    int sta[Maxn], en[Maxn], ty[Maxn][20];
    double d[Maxn][Maxn];
    struct Point {
        double x, y;
    } p[Maxn];
    double DIS(Point a, Point b) {
        return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
    }
    //ALGORITHM MINCOSTFLOW ->
    #define ALGORITHM_MINCOSTFLOW_MAXN 600
    #define ALGORITHM_MINCOSTFLOW_MAXM 360000
    #define ALGORITHM_MINCOSTFLOW_INF 0X7FFFFFFF
    struct ALGORITHM_MINCOSTFLOW_Edge {
        int v;
        int val;
        int cost;
        int next;
    } ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_MAXM];
    int ALGORITHM_MINCOSTFLOW_head[ALGORITHM_MINCOSTFLOW_MAXN];
    int ALGORITHM_MINCOSTFLOW_countedge;
    int ALGORITHM_MINCOSTFLOW_pre[ALGORITHM_MINCOSTFLOW_MAXN];
    int ALGORITHM_MINCOSTFLOW_pos[ALGORITHM_MINCOSTFLOW_MAXN];
    int ALGORITHM_MINCOSTFLOW_dis[ALGORITHM_MINCOSTFLOW_MAXN];
    int ALGORITHM_MINCOSTFLOW_que[ALGORITHM_MINCOSTFLOW_MAXM];
    bool ALGORITHM_MINCOSTFLOW_vis[ALGORITHM_MINCOSTFLOW_MAXN];
    void ALGORITHM_MINCOSTFLOW_addedge(int u, int v, int val, int cost) {
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].v = v;
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].val = val;
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].cost = cost;
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].next = ALGORITHM_MINCOSTFLOW_head[u];
        ALGORITHM_MINCOSTFLOW_head[u] = ALGORITHM_MINCOSTFLOW_countedge++;
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].v = u;
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].val = 0;
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].cost = -cost;
        ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_countedge].next = ALGORITHM_MINCOSTFLOW_head[v];
        ALGORITHM_MINCOSTFLOW_head[v] = ALGORITHM_MINCOSTFLOW_countedge++;
    }
    void ALGORITHM_MINCOSTFLOW_clear() {
        memset(ALGORITHM_MINCOSTFLOW_head, -1, sizeof(ALGORITHM_MINCOSTFLOW_head));
        ALGORITHM_MINCOSTFLOW_countedge = 0;
    }
    bool ALGORITHM_MINCOSTFLOW_spfa(int s, int t) {
        memset(ALGORITHM_MINCOSTFLOW_pre, -1, sizeof(ALGORITHM_MINCOSTFLOW_pre));
        memset(ALGORITHM_MINCOSTFLOW_vis, 0, sizeof(ALGORITHM_MINCOSTFLOW_vis));
        int Head, tail;
        Head = tail = 0;
        for (int i = 0; i < ALGORITHM_MINCOSTFLOW_MAXN; i++) {
            ALGORITHM_MINCOSTFLOW_dis[i] = ALGORITHM_MINCOSTFLOW_INF;
        }
        ALGORITHM_MINCOSTFLOW_que[tail++] = s;
        ALGORITHM_MINCOSTFLOW_pre[s] = s;
        ALGORITHM_MINCOSTFLOW_dis[s] = 0;
        ALGORITHM_MINCOSTFLOW_vis[s] = 1;
        while (Head != tail) {
            int now = ALGORITHM_MINCOSTFLOW_que[Head++];
            ALGORITHM_MINCOSTFLOW_vis[now] = 0;
            for (int i = ALGORITHM_MINCOSTFLOW_head[now]; i != -1; i = ALGORITHM_MINCOSTFLOW_edge[i].next) {
                int adj = ALGORITHM_MINCOSTFLOW_edge[i].v;
                if (ALGORITHM_MINCOSTFLOW_edge[i].val > 0 && ALGORITHM_MINCOSTFLOW_dis[now] + ALGORITHM_MINCOSTFLOW_edge[i].cost < ALGORITHM_MINCOSTFLOW_dis[adj]) {
                    ALGORITHM_MINCOSTFLOW_dis[adj] = ALGORITHM_MINCOSTFLOW_dis[now] + ALGORITHM_MINCOSTFLOW_edge[i].cost;
                    ALGORITHM_MINCOSTFLOW_pre[adj] = now;
                    ALGORITHM_MINCOSTFLOW_pos[adj] = i;
                    if (!ALGORITHM_MINCOSTFLOW_vis[adj]) {
                        ALGORITHM_MINCOSTFLOW_vis[adj] = 1;
                        ALGORITHM_MINCOSTFLOW_que[tail++] = adj;
                    }
                }
            }
        }
        return ALGORITHM_MINCOSTFLOW_pre[t] != -1;
    }
    int ALGORITHM_MINCOSTFLOW_MinCostFlow(int s, int t) {
        int cost = 0, flow = 0;
        while (ALGORITHM_MINCOSTFLOW_spfa(s, t)) {
            int f = ALGORITHM_MINCOSTFLOW_INF;
            for (int i = t; i != s; i = ALGORITHM_MINCOSTFLOW_pre[i])
                if (ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_pos[i]].val < f) {
                    f = ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_pos[i]].val;
                }
            flow += f;
            cost += ALGORITHM_MINCOSTFLOW_dis[t] * f;
            for (int i = t; i != s; i = ALGORITHM_MINCOSTFLOW_pre[i]) {
                ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_pos[i]].val -= f;
                ALGORITHM_MINCOSTFLOW_edge[ALGORITHM_MINCOSTFLOW_pos[i] ^ 1].val += f;
            }
        }
        return cost;
    }
    // <- ALGORITHM MINCOSTFLOW
    void build(int type) {
        int i, j;
        ALGORITHM_MINCOSTFLOW_clear();
        for (i = 2; i <= n; i++) {
            ALGORITHM_MINCOSTFLOW_addedge(1, i + 2 * n, ty[i][type], 1);
            ALGORITHM_MINCOSTFLOW_addedge(1, i + n, ty[i][type], 0);
            ALGORITHM_MINCOSTFLOW_addedge(i + 2 * n, 3 * n + 1, ty[i][type], 0);
            for (j = 2; j <= n; j++) {
                if (sta[i] + en[i] + d[i][j] <= sta[j]) {
                    ALGORITHM_MINCOSTFLOW_addedge(i + n, j + 2 * n, ty[i][type], 0);
                }
            }
        }
    }
    int solve() {
        int i, j, u, v;
        int ans = 0;
        for (i = 1; i <= m; i++) {
            build(i);
            ans += ALGORITHM_MINCOSTFLOW_MinCostFlow(1, 3 * n + 1);
        }
        return ans;
    }
    int main() {
        int i, j, u, v, c, t;
        scanf("%d", &t);
        while (t--) {
            ALGORITHM_MINCOSTFLOW_clear();
            scanf("%d%d", &n, &m);
            scanf("%lf%lf", &p[1].x, &p[1].y);
            for (i = 2; i <= n; i++) {
                scanf("%lf%lf%d%d", &p[i].x, &p[i].y, &sta[i], &en[i]);
                for (j = 1; j <= m; j++) {
                    scanf("%d", &ty[i][j]);
                }
            }
            for (i = 1; i <= n; i++) {
                for (j = i + 1; j <= n; j++) {
                    d[i][j] = d[j][i] = DIS(p[i], p[j]);
                }
            }
            printf("%d
    ", solve());
        }
        return 0;
    }
    View Code

     

  • 相关阅读:
    POJ1521 最优哈夫曼编码树 贪心算法的有效应用
    hdu4911逆序对+mergesort
    hdu1735 字数统计 贪心算法
    最大不相交区间数+最少区间数覆盖问题 贪心思想
    洛谷1010 幂次方 分治算法+掩码的应用
    POJ 2082 三种解法(暴力+树状数组+线段树)
    POJ3134 Power Calculus IDA*搜索的一个应用
    hdu2648 STL map的简单应用
    Delphi 数据类型与Windows 数据类型 对照
    Delphi Window Messages 消息
  • 原文地址:https://www.cnblogs.com/dramstadt/p/6126195.html
Copyright © 2011-2022 走看看