poj 1639 Picnic Planning(最小度限制產生樹)

來源:互聯網
上載者:User

連結:

http://poj.org/problem?id=1639

題目:

Picnic Planning
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 7780   Accepted: 2726

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

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

Sample Output

Total miles driven: 183

Source

East Central North America 2000

題目大意:

馬戲團的小丑們有一個特異功能,無論一個車子有多小,他們都能鑽進去,也就是說,一輛車子能夠容納無限個小丑。

現在小丑們要去一個公園野餐,他們住在不同的地方,為了節約路費(石油),要使得所有車子加起來走的路程最小,那麼,小丑A

可以直接開車到公園,或者開到小丑B家,然後把車停在B家,搭B的車一起去公園。

小丑家的停車位是有限制的,但是公園的停車位是有限制的,公園最多隻能停k輛車。一旦某個小丑開車到了公園,那麼就必須停在公園,不能在回去載其他小丑了。

求所有小丑開車的最短總路程。


分析與總結:

最小產生樹的拓展問題,經典的最小度限制產生樹問題。

從昨晚搞到了現在,思想比較容易理解,但是代碼實現起來比較複雜,而且還不是獨立完成的。

參考資料:

1.這個ppt真心不錯,看了基本上懂了:

     http://wenku.baidu.com/view/70ef0e00eff9aef8941e06db.html

2.IOI2004國家集訓隊論文--王汀《最小產生樹問題的擴充》

    http://wenku.baidu.com/view/41800d66ddccda38376bafac.html

3.黑書, P300~303,比較難懂

4.代碼參考:

    http://www.cnblogs.com/ylfdrib/archive/2010/08/21/1805505.html

代碼:

/*******************************************最小度限制產生樹演算法架構:    1. 先求出最小m度限制產生樹;    2. 由最小m度限制產生樹得到最小m+1度限制產生樹;    3. 當dT(v0)=k時停止(即當V0的度為k的時候停止);********************************************/#include<algorithm>#include<iostream>#include<string>#include<map>#include<cstdio>#include<cstring>using namespace std;map<string, int>mp;const int VN  = 30;    // 點的數量const int EN  = VN*VN; // 邊的數量const int INF = 0x7fffffff;int limit;template<typename Type>class Prim{public:    void init(int _n){        for(int i=1; i<VN-1; ++i){            w[i][i] = INF;            for(int j=i+1; j<VN; ++j)                w[i][j]=w[j][i]=INF;        }    }    void setVertexNum(int x){        n = x;    }    void insert(int u, int v, Type weight){        if(w[u][v]>weight) w[u][v] = weight; //注意可能有重複邊    }    Type minDegreeST(int v0, int k){ // v0是限制度的點, k是限制的度數        memset(father, -1, sizeof(father));        memset(vis, 0, sizeof(vis));        memset(edge, 0, sizeof(edge));        vis[v0] = true;        int m = 0;  // 連通分支的個數        mst = 0;         // 所求答案        /* 步驟1: 先求出m限制樹 */        for(int i=1; i<=n; ++i)if(!vis[i]){            ++m;            mst += prim(i, v0);        }        /* 步驟2: 由m限制樹得到m+1限制樹 */        int minAdd, a, b, tmp;        int change;  // 迴路上權值最大的邊,用於交換        for(int i=m+1; i<=k&&i<=n; ++i){            memset(best, -1, sizeof(best));            for(int j=1; j<=n; ++j)if(best[j]==-1 && father[j]!=v0){                Best(j, v0);            }             minAdd = INF;            for(int j=1; j<=n; ++j)if(w[v0][j]!=INF && father[j]!=v0){ //遍曆所有邊                a = best[j];                b = father[best[j]];                tmp = w[v0][j] - w[a][b];                if(tmp < minAdd){                    minAdd=tmp;                    change = j;                }            }            if(minAdd >= 0) break; //用於度數不大於k的限制,如果k限制,就不用break了            mst += minAdd;            a = best[change];            b = father[change];            w[a][b] = w[b][a] = INF;            father[a] = b = v0;            w[a][b] = w[b][a] = w[change][v0];            w[v0][change] = w[change][v0] = INF;        }         return mst;    }   private:    // 拉成有根樹    void dfs(int cur){        for(int i=1; i<=n; ++i)if(mark[i] && edge[i][cur]){            father[i] = cur;            mark[i] = 0;            dfs(i);        }    }    // 記憶化搜尋,求x到V0路徑上權值最大的邊    int Best(int x, int V0){        if(father[x]==V0) return -1;        if(best[x] != -1){            return best[x];        }        int tmp = Best(father[x], V0);        if(tmp!=-1 && w[tmp][father[tmp]] > w[father[x]][x])            best[x] = tmp;        else            best[x] = x;        return best[x];    }    /* 求去掉與V0相連的邊之後的連通分量的最小產生樹 */    Type prim(int s, int V0){        memset(mark, false, sizeof(mark));        vis[s] = mark[s] = true;        for(int i=1; i<=n; ++i){            key[i] = w[s][i]; pre[i] = s;         }        int sum=0;        for(int i=1; i<n; ++i){            int u=-1;            for(int j=1; j<=n; ++j)if(!vis[j]&&!mark[j]){                if(u==-1||key[j]<key[u]) u=j;            }            if(u==-1) break;            vis[u] = mark[u] = true;            edge[pre[u]][u] = edge[u][pre[u]] = true;            sum += w[pre[u]][u];            for(int j=1; j<=n; ++j)if(!vis[j]&&!mark[j]){                if(key[j]>w[u][j]){                    key[j] = w[u][j]; pre[j] = u;                }            }        }        int Min = INF;        int root = -1;  // 樹根        for(int i=1; i<=n; ++i)if(mark[i] && w[i][V0]<Min){            Min = w[i][V0];            root = i;        }        // 拉成有根樹,即把當前這個連通分量用一條到V0權值最小的邊串連起來        // 並且構成一棵樹,利用father數組儲存父結點        mark[root] = 0;        dfs(root);        father[root] = V0;        return sum + Min;    }private:    int n;             // 結點個數    int pre[VN];       // 父結點     int father[VN];    // 產生樹中的父結點    bool edge[VN][VN]; // edg[i][j] = true 表示邊[i,j]已在產生樹中    int best[VN];      // best[i]儲存V0到i之間權值最大的邊    bool vis[VN];      // vis[i]表示點i是否以加入產生樹    bool mark[VN];     // 用於求連通分量最小產生樹的標記    Type mst;          // 儲存答案     Type w[VN][VN], key[VN];};Prim<int>G;  int main(){    int n, d, a, b;    string name1,name2;    mp["Park"] = 1;    scanf("%d",&n);    G.init(n);    int cnt=1;    for(int i=0; i<n; ++i){        cin >> name1 >> name2 >> d;        a=mp[name1];  b=mp[name2];        if(!a)a=mp[name1]=++cnt;        if(!b)b=mp[name2]=++cnt;        G.insert(a,b,d);        G.insert(b,a,d);    }    G.setVertexNum(cnt);    scanf("%d",&limit);    printf("Total miles driven: %d\n", G.minDegreeST(1, limit));    return 0;}

——  生命的意義,在於賦予它意義。

               原創 http://blog.csdn.net/shuangde800 , By   D_Double  (轉載請標明)


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.