1. 程式人生 > >HDU4009:Transfer water(有向圖的最小生成樹)

HDU4009:Transfer water(有向圖的最小生成樹)

ive print mini 有向圖 構造 algorithm bsp scribe build

Transfer water

Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 6126 Accepted Submission(s): 2181

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4009

Description:

XiaoA lives in a village. Last year flood rained the village. So they decide to move the whole village to the mountain nearby this year. There is no spring in the mountain, so each household could only dig a well or build a water line from other household. If the household decide to dig a well, the money for the well is the height of their house multiplies X dollar per meter. If the household decide to build a water line from other household, and if the height of which supply water is not lower than the one which get water, the money of one water line is the Manhattan distance of the two households multiplies Y dollar per meter. Or if the height of which supply water is lower than the one which get water, a water pump is needed except the water line. Z dollar should be paid for one water pump. In addition,therelation of the households must be considered. Some households may do not allow some other households build a water line from there house. Now given the 3‐dimensional position (a, b, c) of every household the c of which means height, can you calculate the minimal money the whole village need so that every household has water, or tell the leader if it can’t be done.

Input:

Multiple cases.
First line of each case contains 4 integers n (1<=n<=1000), the number of the households, X (1<=X<=1000), Y (1<=Y<=1000), Z (1<=Z<=1000).
Each of the next n lines contains 3 integers a, b, c means the position of the i‐th households, none of them will exceeded 1000.

Then next n lines describe the relation between the households. The n+i+1‐th line describes the relation of the i‐th household. The line will begin with an integer k, and the next k integers are the household numbers that can build a water line from the i‐th household.
If n=X=Y=Z=0, the input ends, and no output for that.

Output:

One integer in one line for each case, the minimal money the whole village need so that every household has water. If the plan does not exist, print “poor XiaoA” in one line.

Sample Input:

2 10 20 30
1 3 2
2 4 1
1 2
2 1 2
0 0 0 0

Sample Output:

30

題意:

現在給出n戶人家,每戶人家都有對應的海拔高度,現在每戶人家需要水,獲得水有兩個來源:自己挖井,從其它人家修建水渠。

假設從u到v修建水渠,如果u的海拔較高,那麽只需要支付水渠的費用;否則還要加上水泵的費用;如果自己挖井費用只和海拔有關。

問當所有人家都有水時,最小花費為多少。

題解:

這個題可以看成是有向圖的最小生成樹模型,畢竟是要用有向邊把圖連通嘛,這個題不存在不成功的情況(天災人禍除外 = =)。

還是建立一個虛點,然後直接向每戶人家連邊,邊權為打井的費用;之和再根據題目描述構造其它邊。

最後從虛點出發跑朱劉算法就行了。

代碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1005;
int n,x,y,z,tot;
struct Point{
    int x,y,z;
}p[N];
int dis(int a,int b){
    return abs(p[a].x-p[b].x)+abs(p[a].y-p[b].y)+abs(p[a].z-p[b].z);
}
struct Edge{
    int u,v,w;
}e[N*N];
int pre[N]; //記錄前驅.
int id[N],vis[N],in[N];
int dirMst(int root){
    int ans=0;
    while(1){
        memset(in,INF,sizeof(in));
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        for(int i=1;i<=tot;i++){
            int u=e[i].u,v=e[i].v,w=e[i].w;
            if(w<in[v] && v!=u){
                pre[v]=u;
                in[v]=w;
            }
        }           //求最小入邊集
        in[root]=0;
        pre[root]=root;
        for(int i=0;i<n;i++){
            if(in[i]==INF) return -1;
            ans+=in[i];
        }
        int idx = 0; //新標號
        for(int i=0;i<n;i++){
            if(vis[i] == -1 ){
                int u = i;
                while(vis[u] == -1){
                    vis[u] = i;
                    u = pre[u];
                }
                if(vis[u]!=i || u==root) continue;     //判斷是否形成環
                for(int v=pre[u];v!=u;v=pre[v] )
                    id[v]=idx;
                id[u] = idx++;
            }
        }
        if(idx==0) break;
        for(int i=0;i<n;i++){
            if(id[i]==-1) id[i]=idx++;
        }
        for(int i=1;i<=tot;i++){
            e[i].w-=in[e[i].v];
            e[i].u=id[e[i].u];
            e[i].v=id[e[i].v];
        }
        n = idx;
        root = id[root];//給根新的標號
    }
    return ans;
}
int main(){
    while(scanf("%d%d%d%d",&n,&x,&y,&z)!=EOF){
        if(n+x+y+z<=0) break ;
        tot=0;
        for(int i=1;i<=n;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
        for(int i=1;i<=n;i++){
            int k;
            scanf("%d",&k);
            for(int j=1;j<=k;j++){
                int id;
                scanf("%d",&id);
                e[++tot].u=i;e[tot].v=id;
                if(p[i].z>=p[id].z) e[tot].w=dis(i,id)*y;
                else e[tot].w=dis(i,id)*y+z;
            }
        }
        for(int i=1;i<=n;i++){
            e[++tot].u=0;e[tot].v=i;e[tot].w=p[i].z*x;
        }
        n++;
        int ans = dirMst(0);
        cout<<ans<<endl;
    }
    return 0;
}

HDU4009:Transfer water(有向圖的最小生成樹)