zoukankan      html  css  js  c++  java
  • HDU

    HDU - 4009:http://acm.hdu.edu.cn/showproblem.php?pid=4009

    题意:

        有n户人家住在山上,现在每户人家(x,y,z)都要解决供水的问题,他可以自己挖井,也可以从特定的别人那里调水。问所有人家都接上水后最小的花费。

    思路:

        据说是一道最小生成树的模版题,我觉得在建图上还是比较难的。每个人家从别人那里调水的关系是明确的,直接建图就行。那自己挖井,我们可以建立一个虚拟的源点,向每个点连一条边,边的权值就是挖井所要的费用。建完图后,就可以跑一遍最小树形图了。显然答案是一定存在的;

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    using namespace std;
    //#pragma GCC optimize(3)
    //#pragma comment(linker, "/STACK:102400000,102400000")  //c++
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define OKC ios::sync_with_stdio(false);cin.tie(0)
    #define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
    #define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
    //priority_queue<int ,vector<int>, greater<int> >que;
    
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;       
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 10007;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    
    /*-----------------------showtime----------------------*/
                const int maxm = 1e6+9;
                const int maxn = 1e3+9;
                struct edge
                {
                    int from,to,c;
                }e[maxm];
                int X,Y,Z,n,tot;
                struct node
                {
                    int x,y,z;
                }p[maxn];
                int in[maxn],vis[maxn],pre[maxn],id[maxn];
                int rtt;
                    int zhuliu(int root,int n,int m){
                        int res = 0;
                        while(true){
                            memset(in, inf, sizeof(in));
                            for(int i=1; i<=m ;i++){
                                if(e[i].from != e[i].to && e[i].c < in[e[i].to]){
                                    pre[e[i].to] = e[i].from;
                                    in[e[i].to] = e[i].c;
                                }
                            }
                            for(int i=1; i<=n; i++){
                                if(i!=root && in[i] == inf)
                                    return -1;
                            }
                            int tn = 0,v;
                            memset(id,-1,sizeof(id));
                            memset(vis,-1,sizeof(vis));
                            in[root] = 0;
                            for(int i=1; i<=n; i++){
                                res += in[i];
                                v = i;
                                while(v!=root && id[v] == -1 && vis[v] != i){
                                    vis[v] = i;
                                    v = pre[v];
                                }
                                if(v!=root && id[v] == -1){
                                    id[v] = ++tn;
                                    for(int u = pre[v] ; u!=v; u = pre[u]){
                                        id[u] = tn;
                                    }
                                }
                            }
                            if(tn == 0)break;
                            for(int i=1; i<=n; i++){
                                if(id[i] == -1)id[i] = ++tn;
                            }
    
                            for(int i=1; i<=m; i++){
                                int v = e[i].to;
                                e[i].to = id[e[i].to];
                                e[i].from = id[e[i].from];
                                if(e[i].to != e[i].from){
                                    e[i].c -= in[v];
                                }
                            }
                            n = tn;root = id[root];
                        }
                        return res;
                    }
                    int get(int u,int v){
                        int ans = abs(p[u].x - p[v].x) +abs(p[u].y - p[v].y)+abs(p[u].z - p[v].z); 
                        ans = ans * Y;
                        if(p[v].z > p[u].z)ans += Z;
                        return ans;
                    }
    int main(){
                    while(~scanf("%d%d%d%d", &n, &X,&Y,&Z) && n+X+Y+Z){
                        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 x,k;scanf("%d", &k);
                            for(int j=1; j<=k; j++)
                            {
                                scanf("%d", &x);
                                e[++tot].from = i;
                                e[tot].to = x;
                                e[tot].c = get(i,x);
                            }
                        }
    
                        for(int i=1; i<=n; i++){
                            e[++tot].from = 0;
                            e[tot].to = i;
                            e[tot].c = X * p[i].z;
                        }   
    
                        int ans = zhuliu(0,n,tot);
                        printf("%d
    ", ans);
                    }
                    return 0;
    }
    HDU - 4009
  • 相关阅读:
    python全栈开发_day48_bootstrap
    python全栈开发_day47_js,jQuery
    python全栈开发_day46_html文本标签
    抽象集合
    ViewState的用法
    sql(join中on与where区别)
    微软的技术文档链接地址
    微软的帮助文档,详细的介绍VS开发工具的使用
    [摘录]解决C# DateTime去掉时分秒几种方法
    验证(摘录)
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/9666900.html
Copyright © 2011-2022 走看看