• poj 1639 Picnic Planning


    Time Limit: 5000MS Memory Limit: 10000K
    Total Submissions: 11507 Accepted: 4136

    Description
    The Contortion Brothers are a famous set of circus clowns, known worldwide for their incredible ability to cram an unlimited number of themselves into even the smallest vehicle. During the off-season, the brothers like to get together for an Annual Contortionists Meeting at a local park. However, the brothers are not only tight with regard to cramped quarters, but with money as well, so they try to find the way to get everyone to the party which minimizes the number of miles put on everyone’s cars (thus saving gas, wear and tear, etc.). To this end they are willing to cram themselves into as few cars as necessary to minimize the total number of miles put on all their cars together. This often results in many brothers driving to one brother’s house, leaving all but one car there and piling into the remaining one. There is a constraint at the park, however: the parking lot at the picnic site can only hold a limited number of cars, so that must be factored into the overall miserly calculation. Also, due to an entrance fee to the park, once any brother’s car arrives at the park it is there to stay; he will not drop off his passengers and then leave to pick up other brothers. Now for your average circus clan, solving this problem is a challenge, so it is left to you to write a program to solve their milage minimization problem.

    Input
    Input will consist of one problem instance. The first line will contain a single integer n indicating the number of highway connections between brothers or between brothers and the park. The next n lines will contain one connection per line, of the form name1 name2 dist, where name1 and name2 are either the names of two brothers or the word Park and a brother’s name (in either order), and dist is the integer distance between them. These roads will all be 2-way roads, and dist will always be positive.The maximum number of brothers will be 20 and the maximumlength of any name will be 10 characters.Following these n lines will be one final line containing an integer s which specifies the number of cars which can fit in the parking lot of the picnic site. You may assume that there is a path from every brother’s house to the park and that a solution exists for each problem instance.

    Output
    Output should consist of one line of the form
    Total miles driven: xxx
    where xxx is the total number of miles driven by all the brothers’ cars.

    Sample Input

    10
    Alphonzo Bernardo 32
    Alphonzo Park 57
    Alphonzo Eduardo 43
    Bernardo Park 19
    Bernardo Clemenzi 82
    Clemenzi Park 65
    Clemenzi Herb 90
    Clemenzi Eduardo 109
    Park Herb 24
    Herb Eduardo 79
    3

    Sample Output

    Total miles driven: 183

    解题思路

    有度数限制的最小生成树,做法是将一号点挖去,得到几个联通块,在每个联通块里跑一遍最小生成树,只后选取每个联通块里最小的与1号点相连的边记录答案。假设有T个联通块,1的限制为S,那么还可以有S-T条边可以被改动。然后继续选择与1相连的点x,将1与x连起来会得到一个环,然后在环上找一条最大的边删去,枚举所有的点x执行以上操作。直到S==T或没有更优的边可以加为止。确定环上的边为了避免重复计算,用动态规划,dp[x]=max(dp[father[x]],val[x][father[x]] ) 。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<vector>
    #include<algorithm>
    #include<string>
    
    using namespace std;
    const int N = 105;
    const int inf = 0x3f3f3f3f;
    
    struct Edge{
        int fr,to,w;
    }dp[N];
    
    int n,fa[N],minedge[N];
    int num=0,val[N][N];
    int g[N][N],ans,S;
    int where[N],T;
    bool tree[N][N];
    map<string,int> mp;
    vector<Edge> edges;
    
    inline int find(int x){
        if(x==fa[x]) return x;
        return fa[x]=find(fa[x]);
    }
    
    inline bool cmp(Edge A,Edge B){
        return A.w<B.w;
    }
    
    inline void kruskal(){
        sort(edges.begin(),edges.end(),cmp);
        for(register int i=0;i<edges.size();i++){
            int u=find(edges[i].fr);
            int v=find(edges[i].to);
            if(u!=v && edges[i].fr!=1 && edges[i].to!=1) {
                fa[u]=v;
                tree[edges[i].fr][edges[i].to]=tree[edges[i].to][edges[i].fr]=1;
                ans+=edges[i].w;
            }
        }
    }
    
    inline void dfs(int x,int pre){
        for(register int i=2;i<=num;i++){
            if(i==pre || !tree[i][x]) continue;
            if(dp[i].w==-1){
                if(dp[x].w>g[i][x]) dp[i]=dp[x];
                else {
                    dp[i].w=g[i][x];
                    dp[i].fr=x;
                    dp[i].to=i;
                }
            }
            dfs(i,x);
        }
    }
    
    int main(){
        while(cin>>n){
            num=0;ans=0;T=0;
            mp.clear();edges.clear();
            memset(where,0,sizeof(where));
            memset(tree,0,sizeof(tree));
            memset(minedge,0x3f,sizeof(minedge));
            memset(g,0x3f,sizeof(g));mp["Park"]=++num;
            for(register int i=1;i<N;i++) fa[i]=i;
            for(register int i=1;i<=n;i++){
                string a,b;int c;
                cin>>a>>b>>c;
                if(!mp[a]) mp[a]=++num;
                if(!mp[b]) mp[b]=++num;
                int xxx=mp[a],yyy=mp[b];
                edges.push_back((Edge){xxx,yyy,c});
                g[xxx][yyy]=g[yyy][xxx]=min(g[xxx][yyy],c);
            }
            cin>>S;kruskal();
            for(register int i=2;i<=num;i++)
                if(g[i][1]!=inf){
                    int v=find(i);
                    if(minedge[v]>g[i][1]){
                        minedge[v]=g[i][1];
                        where[v]=i; 
                    }
                }
            for(register int i=1;i<=num;i++)
                if(minedge[i]!=inf){
                    ans+=g[1][where[i]];
                    tree[where[i]][1]=tree[1][where[i]]=1;
                    T++;
                }
            for(register int i=T+1;i<=S;i++){
                memset(dp,-1,sizeof(dp));
                dp[1].w=-inf;
                for(register int j=2;j<=num;j++)
                    if(tree[1][j]) dp[j].w=-inf;
                dfs(1,-1);
                int maxnum=-1,idx;
                for(register int j=2;j<=num;j++)
                    if(dp[j].w-g[1][j]>maxnum){
                        maxnum=dp[j].w-g[1][j];
                        idx=j;
                    }   
                if(maxnum<=0) break;
                ans-=dp[idx].w;ans+=g[1][idx];
                tree[idx][1]=tree[1][idx]=1;
                tree[dp[idx].fr][dp[idx].to]=tree[dp[idx].to][dp[idx].fr]=0;
            }
            printf("Total miles driven: %d
    ", ans);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    在一个字符串中找到第一个只出现一次的字符
    查找最小的k个数
    动规:最大上升子序列
    平衡二叉树
    【笔记】php和mysql结合 搞了一个表出来
    设计模式心得(既设计模式篇终章):描述设计模式时的通用公式
    分享系列 之 linux IO原理与几种零拷贝机制的实现
    近期分享:BIO 与 NIO 的实质区别到底是什么?
    源码阅读笔记 之 ThreadLocal —— 不复杂,却有点绕的一个 per thread API
    小脑袋瓜充满了问号:为什么AMQP可以叫做 Advanced?JMS就要low一等吗?
  • 原文地址:https://www.cnblogs.com/sdfzsyq/p/9676963.html
Copyright © 2020-2023  润新知