poj_1639 Picnic Planning(度限制最小生成树)

来源:互联网 发布:淘宝店不开了怎么注销 编辑:程序博客网 时间:2024/05/21 06:00
Picnic Planning
Time Limit: 5000MS Memory Limit: 10000KTotal Submissions: 10431 Accepted: 3783

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
度限制最小生成树,也就是某个顶点有度数限制的生成树。
对这道题来说是名字为Park的顶点有度数限制,最大为k,我们把这个顶点设为v0好了。
首先需要知道生成树有一个回路性质:设C为图G中的一个回路,边e是C上权值最大的边,
则图G的所有生成树均不包含e。
然后我们能给出求度限制最小生成树的几个步骤:
(1)先在图G中删掉顶点v0,然后求剩下的图的最小生成树,得到图G1,此时可能会有若干个生成树,
它们分别都是图中的一个连通分量。
(2)然后让v0与这些连通分量都分别连上一条边,该边为图G中v0与某个连通分量中的所有顶点
的边中最短的那条,得到图G2,此时可以得到图G中出现生成树时v0的最小度数ki。
(3)目前我们已经得到v0度数等于ki时的最小生成树,接下来要得到v0度数小于等于k的最小生成树,
只要能找到求出v0度数等于ki+1的最小生成树的方法就可以了。这个方法需要用到上面的回路性质,
首先因为G2是一棵树,所以如果我们在图G2中给v0和其他顶点连上一条边(即增加了v0的度数),则
一定会出现一条回路,根据回路性质我们需要找到这条回路中权值最大的边(除了与v0相连的边),并
把它删去,这样就得到了ki+1的最小生成树。
----------------------------------------------------------------
算法不难理解,实现起来会有些复杂,比较重要的一个优化是找到回路中权值最大的边时先用dp求出
并保存v0到其他顶点的路径中权值最大的边,这样就只不用每次都遍历整个图了。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 50#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct Edge{    int u, v, cost;}edge[maxn*maxn];bool cmp(Edge a, Edge b){    return a.cost < b.cost;}int ans, n, m, k, fa[maxn];int G[maxn][maxn];bool ma[maxn][maxn];Edge dp[maxn]; //dp[v]为路径v0—v上与v0无关联且权值最大的边map<string, int> M;void init(){    ans = 0;    n = 1;    memset(ma, 0, sizeof(ma));    memset(G, -1, sizeof(G));    M.clear();    M["Park"] = 1;}void makeSet(){    for(int i = 1; i <= n; i++) fa[i] = i;}int findFa(int i){    while(i != fa[i]) i = fa[i];    return i;}void kruskal(){    makeSet();    sort(edge, edge+m, cmp);    int u, v;    for(int i = 0; i < m; i++)    {        if(edge[i].u == 1 || edge[i].v == 1) continue;        u = findFa(edge[i].u);        v = findFa(edge[i].v);        if(u == v) continue;        fa[u] = v;        ma[edge[i].u][edge[i].v] = ma[edge[i].v][edge[i].u] = 1;        ans += edge[i].cost;    }}//更新dpvoid dfs(int u, int pre){    for(int v = 2; v <= n; v++)    {        if(v == pre) continue;        if(ma[u][v])        {            if(dp[v].cost != -1)            {                if(dp[u].cost < G[u][v]) dp[v].u = u, dp[v].v = v, dp[v].cost = G[u][v];                else dp[v] = dp[u];            }            dfs(v, u);        }    }}int main(){    //FOP;    while(~scanf("%d", &m))    {        init();        int cost;        string s1, s2;        for(int i = 0; i < m; i++)        {            cin>>s1>>s2>>cost;            if(!M[s1]) M[s1] = ++n;            if(!M[s2]) M[s2] = ++n;            edge[i].u = M[s1], edge[i].v = M[s2], edge[i].cost = cost;            G[edge[i].u][edge[i].v] = G[edge[i].v][edge[i].u] = cost;        }        scanf("%d", &k);        kruskal();        //mi[x]表示顶点1到连通分量x的最小边的权,to[x]表示连通分量x中与到顶点1的最小边相连的顶点        int mi[maxn], to[maxn];        for(int i = 1; i <= n; i++) mi[i] = inf;        for(int v = 2; v <= n; v++)        {            if(G[1][v] == -1) continue;            int x = findFa(v);            if(mi[x] > G[1][v]) mi[x] = G[1][v], to[x] = v;        }        int ki = 0; //ki表示最小度        //把顶点1与连通分量连接起来        for(int x = 1; x <= n; x++)        {            if(mi[x] == inf) continue;            ki++;            ma[1][to[x]] = ma[to[x]][1] = 1;            ans += G[1][to[x]];        }        for(ki = ki+1; ki <= k; ki++)        {            dp[1].cost = -1; //标记为-1,后面的dfs不遍历            for(int v = 2; v <= n; v++)            {                if(ma[1][v]) dp[v].cost = -1;                else dp[v].cost = 0;            }            dfs(1, 0);            int to, mi = inf;            for(int v = 2; v <= n; v++)            {                if(G[1][v] == -1) continue;                if(mi > G[1][v]-dp[v].cost) mi = G[1][v]-dp[v].cost, to = v;            }            if(mi >= 0) break; //找不到更小的生成树了,直接跳出            //修改图            ma[1][to] = ma[to][1] = 1;            ma[dp[to].u][dp[to].v] = ma[dp[to].v][dp[to].u] = 0;            ans += mi;        }        printf("Total miles driven: %d\n", ans);    }    return 0;}


0 0
原创粉丝点击