poj 1639 Picnic Planning(最小K度限制生成树)

Language:
Picnic Planning
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 9563 Accepted: 3427

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



求一个无向图的最小生成树 其中有个点的度有限制(不大于K)

具体内容可以参考这篇博客: http://www.cnblogs.com/jackge/archive/2013/05/12/3073669.html

代码能力有限  只能学着别人的代码 写下 作为模版使用

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#include <map>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 10010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}
const int N=30;
struct node
{
    int v,cap;
    node() {}
    node(int _v,int _cap):v(_v),cap(_cap){}
    bool operator < (const node &a)const
    {
        return cap>a.cap;
    }
};
map<string,int> mp;
int g[N][N],dis[N],clo[N],pre[N],fst[N],max_side[N];
int n,m,k;//点的数量 边的数量 指定点度数

int Prim(int src,int id)
{
    priority_queue<node> q;
    while(!q.empty())
        q.pop();
    dis[src]=0;
    q.push(node(src,0));
    int ans=0;
    while(!q.empty())
    {
        node cur=q.top(); q.pop();
        int u=cur.v;
        if(!clo[u])
        {
            clo[u]=id;
            ans+=dis[u];
            for(int i=1;i<n;i++)
            {
                if(!clo[i]&&g[u][i]!=0&&dis[i]>g[u][i])//满足松弛条件
                {
                    pre[i]=u;
                    dis[i]=g[u][i];
                    q.push(node(i,dis[i]));
                }
            }
        }
    }
    return ans;
}

void update(int cur,int last,int maxside)//dfs过程 直到搜回到起点 并完成max_sidegengxin
{
    max_side[cur]=maxside>g[cur][last]?maxside:g[cur][last];
    for(int i=1;i<n;i++)
    {
        if(i!=last&&g[cur][i]!=0&&(pre[cur]==i||pre[i]==cur))
            update(i,cur,max_side[cur]);
    }
}

void Solve()
{
    int res,cnt;
    for(int i=0;i<n;i++)
    {
        dis[i]=INF;
        clo[i]=pre[i]=fst[i]=0;
    }
    res=0; cnt=1;   //除去根节点后,图中连通子图个数 即最小生成树个数
    for(int i=1;i<n;i++)
        if(!clo[i])
            res+=Prim(i,cnt++);
    for(int i=1;i<n;i++)//找到每个生成树和Park最近的点使之和Park相连
    {
        int id=clo[i];
        if(g[0][i]!=0&&(!fst[id]||g[0][i]<g[0][fst[id]]))
            fst[id]=i;
    }
    for(int i=1;i<cnt;i++)//把m个生成树上和根节点相连的边加入res 得到关于Park的最小m度生成树
    {
        res+=g[0][fst[i]];
        g[0][fst[i]]=g[fst[i]][0]=0;
        update(fst[i],0,0);
    }
     /*
    添删操作:将根节点和生成树中一个点相连,会产生一个环,将这个环上(除刚添的那条边外)权值最大
    的边删去.由于每次操作都会给总权值带来影响 d=max_side[tmp]-mat[0][tmp],我们需要得到最小生
    成树,所以我们就要求 d 尽量大
    */
    k=k-cnt+1; //接下来重复操作 直到度数满足条件
    while(k--)
    {
        int tmp=0;
        for(int i=1;i<n;i++)//找d值最大的点(就是完成增删操作之后可以使总边权减小的值最大)
            if(g[0][i]!=0&&(tmp==0||max_side[tmp]-g[0][tmp]<max_side[i]-g[0][i]))
                tmp=i;
        if(max_side[tmp]<=g[0][tmp])//总权值无法再减小
            break;
        res=res-max_side[tmp]+g[0][tmp];
        g[0][tmp]=g[tmp][0]=0;
        int p=0;
        for(int i=tmp;pre[i]!=0;i=pre[i])
            if(p==0||g[p][pre[p]]<g[i][pre[i]])
                p=i;
        pre[p]=0;
        update(tmp,0,0);
    }
    printf("Total miles driven: %d\n",res);
}

int main()
{
//    fread;
    char s1[20],s2[20];
    int cap;
    while(scanf("%d",&m)!=EOF)
    {
        mp["Park"]=0;
        n=1;
        MEM(g,0);
        while(m--)
        {
            scanf("%s %s %d",s1,s2,&cap);
            if(!mp.count(s1))//如果键存在返回1,否则返回0
                mp[s1]=n++;
            if(!mp.count(s2))
                mp[s2]=n++;
            int u=mp[s1],v=mp[s2];
            if(!g[u][v]||g[u][v]>cap)//u v之间还没建边或者之间的距离大于cap
                g[u][v]=g[v][u]=cap;
        }
        scanf("%d",&k);
        Solve();
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值