[poj1639]度限制生成树

原创 2016年08月28日 23:14:51

Problem 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.

算法思路

  1. 这个算法在某个大牛的博客里被反复的提及。
  2. 首先,我们将V0排除,建立若干个连通分支树。即使用Prim算法构造最小生成树。
  3. 然后,选择最短的边将这个图变成连通图。此时计算从V0出发到达每个点当中最长的边。
  4. 然后,在允许的度限制的范围内,反复加入与V0相关的边,每次加入一条边的时候,都会形成一个环,此时就删去这个环上最长的边。可以保证得到的生成树的权值最短。
  5. 说的这么轻松,但是实现起来还是十分麻烦的,下面附代码。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

#define MAXN 25
#define INF 0x3f3f3f3f

struct Edge{
    int u,v,len,next;
}EdgeTable[MAXN*MAXN*4];

struct Node{
    int id,dis;
    Node(){}
    Node(int a,int b){
        id=a;
        dis=b;
    }
    friend bool operator<(Node a,Node b){
        return a.dis>b.dis;
    }
};

char nameList[MAXN][15];
int head[MAXN];
int n,m,k,cnt,e;
int maxside[MAXN],fst[MAXN];
int pre[MAXN];
int clo[MAXN];
int dist[MAXN];

void Init()
{
    strcpy(nameList[0],"Park");
    memset(head,-1,sizeof(head));
    memset(pre,0,sizeof(pre));
    memset(clo,-1,sizeof(clo));
    memset(dist,INF,sizeof(dist));
    memset(maxside,0,sizeof(maxside));
    memset(fst,-1,sizeof(fst));
    n=1;
    cnt=0;
    e=0;
}

//Add new location
int LocQuery(char* name)
{
    int i;
    for(i=0;i<n;i++){
        if(strcmp(name,nameList[i])==0)
            return i;
    }
    strcpy(nameList[n],name);
    return n++;
}

void addEdge(int from,int to,int len)
{
    EdgeTable[e].u=from;
    EdgeTable[e].v=to;
    EdgeTable[e].len=len;
    EdgeTable[e].next=head[from];
    head[from]=e++;
}

int Prim(int s)
{
    int i;
    int res=0;
    priority_queue<Node>Q;
    Q.push(Node(s,0));
    dist[s]=0;

    while(!Q.empty()){
        Node cur = Q.top();
        Q.pop();

        if(clo[cur.id]<0){
            clo[cur.id]=cnt;
            res += dist[cur.id];

            for(i=head[cur.id];i!=-1;i=EdgeTable[i].next){
                int dest = EdgeTable[i].v;
                if(dest!=0&&dist[dest]>EdgeTable[i].len&&clo[dest]<0){
                    pre[dest]=cur.id;
                    dist[dest] = EdgeTable[i].len;
                    Q.push(Node(dest,dist[dest]));
                }
            }
        }
    }
    return res;
}

void Dfs(int cur,int fa,int lastEdge,int maxEdge)
{
    if(EdgeTable[lastEdge].len>EdgeTable[maxEdge].len)
        maxside[cur]=lastEdge;
    else
        maxside[cur]=maxEdge;
    int i;

    for(i=head[cur];i!=-1;i=EdgeTable[i].next){
        if(EdgeTable[i].len!=0&&EdgeTable[i].v!=fa&&(pre[EdgeTable[i].v]==cur||pre[cur]==EdgeTable[i].v))
            Dfs(EdgeTable[i].v,cur,i,maxside[cur]);
    }

    return;
}

void Solve()
{
    int i;
    int ans=0;

    for(i=1;i<n;i++){
        if(clo[i]<0){
            ans += Prim(i);
            cnt++;
        }
    }

    for(i=head[0];i!=-1;i=EdgeTable[i].next){
        //Compute the min length edge from V0 to every 
        //subtrees
        int id = clo[EdgeTable[i].v];
        if(fst[id]<0||EdgeTable[fst[id]].len>EdgeTable[i].len){
            fst[id] = i;
        }
    }

    for(i=0;i<cnt;i++){
        //Compute the max length edge in the route from V0 
        //to every point Vn
        ans += EdgeTable[fst[i]].len;
        EdgeTable[fst[i]].len = EdgeTable[fst[i]^1].len = 0;
        Dfs(EdgeTable[fst[i]].v,0,fst[i],fst[i]);
    }
    k=k-cnt;

    while(k--){
        int tmp=-1;
        int tmp2=-1;
        int tmp3=-1;
        for(i=head[0];i!=-1;i=EdgeTable[i].next){
            int dest = EdgeTable[i].v;
            if(EdgeTable[i].len>0){
                if(EdgeTable[maxside[dest]].len-EdgeTable[i].len>tmp2){
                    tmp=dest;
                    tmp2=EdgeTable[maxside[dest]].len-EdgeTable[i].len;
                    tmp3=i;
                }
            }
        }

        if(tmp<0||tmp2<0)break;
        ans -= tmp2;
        //Delete the edge
        EdgeTable[maxside[tmp]].len = EdgeTable[maxside[tmp]^1].len=0;
        EdgeTable[tmp3].len = EdgeTable[tmp3^1].len = 0;
        int u = EdgeTable[maxside[tmp]].u;
        int v = EdgeTable[maxside[tmp]].v;

        Dfs(tmp,0,tmp3,tmp3);
    }
    printf("Total miles driven: %d\n",ans);
    return;
}

int main()
{
    freopen("input","r",stdin);
    int i,tmp;
    char str1[15],str2[15];
    while(scanf("%d",&m)!=EOF){
    Init();

    for(i=0;i<m;i++){
        scanf("%s %s %d",str1,str2,&tmp);
        int index1 = LocQuery(str1);
        int index2 = LocQuery(str2);
        addEdge(index1,index2,tmp);
        addEdge(index2,index1,tmp);
    }

    scanf("%d",&k);

    Solve();
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 1639 k度限制生成树

题意就是求最小生成树  但是有一个顶点的度必须不大于k 具体的方法网上都有,但是代码写起来之复杂难以令人想象,我由于代码能力还太弱,导致只能看着别人的代码重写一遍,优化了一些部分。 1.求出除...

poj1639 Picnic Planning 最小度数限制生成树

题意:若干个人开车要去park聚会,但是park能停的车是有限的,为k。所以这些人要通过先开车到其他人家中,停车,然后拼车去聚会。另外,车的容量是无限的,他们家停车位也是无限的。求开车总行程最短。 ...

POJ 1639:Picnic Planning(最小度限制生成树)

Picnic Planning Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 7356  ...

P300-野餐计划(POJ-1639最小度限制生成树)

黑书上的例题,具体模型是求一个无向图的最小生成树,其中有一个点的度有限制(假设为 k)。   要求最小 k 度生成树,我们可以按照下面的步骤来做: 设有度限制的点为 V0 ,V0称为根节点 1...

POJ 1639 度限制最小生成树Prim

题意:给出n条无向带权边,求所有点的最小生成树,其中“Park”的度数不超过最后输入的k,输入保证有解。 思路:思路其实很好理解,分为几个步骤: 1.当然将“Park”作为根节点,一开始先删掉它,则原...

poj1639最小度限制生成树(kruscal+邻接表)

一、.思路 设限制结点为des.  1. 求去掉des的最小生成树,此时求出来的是最小生成森林  2.添加des到各连通分量的边,当然取最小的边。  3.此时得到m度的生成树,我们要求的是小于...

[POJ 1639] 单度限制最小生成树

POJ 1639 Picnic Planning 单度限制最小生成树

【POJ】1639 Picnic Planning 度限制最小生成树

Picnic Planning Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9137 ...

poj 1639 Picnic Planning 单度限制的最小生成树

题意: 给一个无向图连通图,求它的最小生成树,生成树满足条件点v0的度小于等于limit。 分析: 一般有度限制的最小生成树问题是np完全的,但单点度限制就比较简单了,先在原图上求不含v0的最小...

POJ1639 Picnic Planning(度限制生成树)

黑书上的例题,所以题意就不啰嗦了,具体模型是求一个无向图的最小生成树,其中有一个点的度有限制(假设为 k)。 要求最小 k 度生成树,我们可以按照下面的步骤来做: 设有度限制的点为 V0 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)