zoukankan      html  css  js  c++  java
  • HDU 2448 Mining Station on the Sea 费用流

    Mining Station on the Sea

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2123    Accepted Submission(s): 642


    Problem Description
    The ocean is a treasure house of resources and the development of human society comes to depend more and more on it. In order to develop and utilize marine resources, it is necessary to build mining stations on the sea. However, due to seabed mineral resources, the radio signal in the sea is often so weak that not all the mining stations can carry out direct communication. However communication is indispensable, every two mining stations must be able to communicate with each other (either directly or through other one or more mining stations). To meet the need of transporting the exploited resources up to the land to get put into use, there build n ports correspondently along the coast and every port can communicate with one or more mining stations directly.

    Due to the fact that some mining stations can not communicate with each other directly, for the safety of the navigation for ships, ships are only allowed to sail between mining stations which can communicate with each other directly.

    The mining is arduous and people do this job need proper rest (that is, to allow the ship to return to the port). But what a coincidence! This time, n vessels for mining take their turns to take a rest at the same time. They are scattered in different stations and now they have to go back to the port, in addition, a port can only accommodate one vessel. Now all the vessels will start to return, how to choose their navigation routes to make the total sum of their sailing routes minimal.

    Notice that once the ship entered the port, it will not come out!
     
    Input
    There are several test cases. Every test case begins with four integers in one line, n (1 = <n <= 100), m (n <= m <= 200), k and p. n indicates n vessels and n ports, m indicates m mining stations, k indicates k edges, each edge corresponding to the link between a mining station and another one, p indicates p edges, each edge indicating the link between a port and a mining station. The following line is n integers, each one indicating one station that one vessel belongs to. Then there follows k lines, each line including 3 integers a, b and c, indicating the fact that there exists direct communication between mining stations a and b and the distance between them is c. Finally, there follows another p lines, each line including 3 integers d, e and f, indicating the fact that there exists direct communication between port d and mining station e and the distance between them is f. In addition, mining stations are represented by numbers from 1 to m, and ports 1 to n. Input is terminated by end of file.

     
    Output
    Each test case outputs the minimal total sum of their sailing routes.
     
    Sample Input
    3 5 5 6 1 2 4 1 3 3 1 4 4 1 5 5 2 5 3 2 4 3 1 1 5 1 5 3 2 5 3 2 4 6 3 1 4 3 2 2
     
    Sample Output
    13
     
    Source
     
    题目分析:给你n个船和n个港口,m个采矿点,k条矿点之间的边,p条港口和矿井的边。问n条船与n个港口一一匹配的最小路程。将路程转化为费用,用最小费用流模型求解。
    设矿点的编号为1 ~ m,港口的编号m + 1 ~ m + n对所有的船 i 和起点建边(s,i, 1, 0);对所有的港口 x 和终点建边(x + m, t, 1, 0);对有路的矿点 u 和矿点 v建边(u,v,oo,w),(v,u,oo,w),因为路可以重复走,所以容量设为oo;最后对有路的矿点 u 和港口 v 建边(v,u + m,1,w)。
    值得一提的是,一开始没有发现边可以重复走。。。蛙了几次,还有比较蛋疼的是没有给明白数据范围,导致边组开小了QUQ。。我了个去- -
    代码如下:
     
    #include <stdio.h>
    #include <string.h>
    #include <memory.h>
    #define min(a, b) ((a) < (b) ? (a) : (b))
    #define REP(i, n) for(int i = 0; i < n; ++i)
    const int maxE = 1000000;
    const int maxN = 305;
    const int oo = 0x3f3f3f3f;
    struct Edge{
        int v, c, w, n;
    };
    Edge edge[maxE];
    int adj[maxN], l;
    int d[maxN], cur[maxN], a[maxN];
    int inq[maxN], Q[maxE], head, tail;
    int n, m, k, p;
    int cost, flow, s, t;
    void addedge(int u, int v, int c, int w){
        edge[l].v = v; edge[l].c = c; edge[l].w =  w; edge[l].n = adj[u]; adj[u] = l++;
        edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;
    }
    int SPFA(){
        memset(d, oo, sizeof d);
        memset(inq, 0, sizeof inq);
        head = tail = 0;
        d[s] = 0;
        a[s] = oo;
        cur[s] = -1;
        Q[tail++] = s;
        while(head != tail){
            int u =  Q[head++];
            inq[u] = 0;
            for(int i = adj[u]; ~i; i = edge[i].n){
                int v = edge[i].v;
                if(!edge[i].c || d[v] <= d[u] + edge[i].w) continue;
                d[v] = d[u] + edge[i].w;
                cur[v] = i;
                a[v] = min(edge[i].c, a[u]);
                if(inq[v]) continue;
                inq[v] = 1;
                Q[tail++] = v;
            }
        }
        if(d[t] == oo) return 0;
        flow += a[t];
        cost += a[t] * d[t];
        for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){
            edge[i].c -= a[t];
            edge[i ^ 1].c += a[t];
        }
        return 1;
    }
    int MCMF(){
        flow = cost = 0;
        while(SPFA());
        return cost;
    }
    void work(){
        int u, v, w;
        while(~scanf("%d%d%d%d", &n, &m, &k, &p)){
            memset(adj, -1, sizeof adj);
            l = 0;
            s = 0; t = n + m + 1;
            REP(i, n){
                scanf("%d", &u);
                addedge(s, u, 1, 0);
            }
            REP(i, n){
                addedge(m + 1 + i, t, 1, 0);
            }
            REP(i, k){
                scanf("%d%d%d", &u, &v, &w);
                addedge(u, v, oo, w);
                addedge(v, u, oo, w);
            }
            REP(i, p){
                scanf("%d%d%d", &u, &v, &w);
                addedge(v, u + m, 1, w);
            }
            printf("%d
    ", MCMF());
        }
    }
    int main(){
        work();
        return 0;
    }
    HDU 2448
  • 相关阅读:
    keepalived 结合mysql 自动切换
    haproxy 配置日志
    haproxy 配置日志
    keepalive的 nopreempt 非抢占
    keepalive的 nopreempt 非抢占
    keepavlied一些参数
    keepavlied一些参数
    mysql 结合keepalived测试
    【转载】[小红猪]11个物理难题,11种基本粒子 分类: 生活百科 2013-07-26 11:04 317人阅读 评论(0) 收藏
    【转载】上帝粒子证实存在宇宙末日来临?(图) 分类: 生活百科 2013-07-26 11:04 336人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/ac-luna/p/3757797.html
Copyright © 2011-2022 走看看