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


php

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\n", MCMF());
    }
}
int main(){
    work();
    return 0;
}
HDU 2448
相關文章
相關標籤/搜索