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

Picnic Planning
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 5846 Accepted: 1934

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
#include<iostream>
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
using namespace std;
//最小K度限制生成树
const int maxcost=(1<<28);
const int N=50;
int g[N][N];//邻接矩阵,从1开始
int n,m,K,ans,source;//点数,边数,度限制,最小K度限制生成树,度限制点
int pre[N],father[N],best[N],dist[N];
bool vis[N],mark[N],edge[N][N];
void dfs(int root)//将一棵最小生成树中各个点标记其father
{
 int i;
 for(i=1;i<=n;i++)
  if(!mark[i]&&!edge[root][i])
  {
   father[i]=root;
   mark[i]=true;
   dfs(i);
  }
}
void prim(int s)//求最小生成树
{
 int i,j,min,k;
 memset(mark,true,sizeof(mark));
 for(i=1;i<=n;i++)
 {
  pre[i]=s;
  dist[i]=g[s][i];
 }
 mark[s]=false;vis[s]=false;
 while(1)
 {
  min=maxcost;k=-1;
  for(i=1;i<=n;i++)
   if(vis[i]&&mark[i]&&dist[i]<min)
   {
    min=dist[i];k=i;
   }
   if(k==-1) break;
   mark[k]=false;vis[k]=false;
   edge[pre[k]][k]=edge[k][pre[k]]=false;
   ans+=min;
   for(i=1;i<=n;i++)
    if(vis[i]&&mark[i]&&g[k][i]<dist[i])
    {  dist[i]=g[k][i]; pre[i]=k;}
 }
    min=maxcost;
 int root=-1;
 for(i=1;i<=n;i++)
  if(!mark[i]&&g[source][i]<min&&i!=source)
  {  min=g[source][i]; root=i;}
  mark[root]=true;
  dfs(root);
  father[root]=source;
  ans+=min;
}
int Best(int j)//更新其中各个点到source路径上边权值最大的点
{
 if(father[j]==source)//如果father为source,记为-1
  return best[j]=-1;
 if(best[j]!=-1) return best[j];//如果已经存在 ,直接返回
 int tmp=Best(father[j]);
 if(tmp!=-1)//这说明其父节点不与source相连
 {
  if(g[tmp][father[tmp]]>g[father[j]][j])
   best[j]=tmp;
  else best[j]=j;
 }
 else best[j]=j;//其父节点与source相连  将j赋给自己
 return best[j];
}
void solve()
{
 int i,j;
 int mst=0;
 memset(father,-1,sizeof(father));
 memset(vis,true,sizeof(vis));
 memset(edge,true,sizeof(edge));
 vis[source]=false;
 for(i=1;i<=n;i++)//求得各个连通分量里的最小生成树
 {
  if(vis[i])
  {
   mst++;
   prim(i);
  }
 }
    for(i=mst+1;i<=n&&i<=K;i++)//求m+1度限制  直到为K
    {
        memset(best,-1,sizeof(best));
        for(j=1;j<=n;j++)
            if(j!=source&&best[j]==-1&&father[j]!=source)
                Best(j);
        int minadd=maxcost;
        int ax,bx,change;
        for(j=1;j<=n;j++)
            if(g[source][j]!=maxcost&&father[j]!=source)
            {
                ax=best[j];
                bx=father[ax];
                if(minadd>g[source][j]-g[ax][bx])
                {
                     minadd=g[source][j]-g[ax][bx];
                     change=j;
                }
            }
            if(minadd>=0) break;
            ans+=minadd;
            ax=best[change];
            bx=father[ax];
            g[ax][bx]=g[bx][ax]=maxcost;
            father[change]=source;
            g[source][change]=g[change][source]=maxcost;
    }
}
map<string,int> q;
void input()
{
    q.clear();
    for(int i=1;i<N;i++) for(int j=1;j<N;j++) g[i][j]=maxcost;
    char s[25],t[25];
    int u,v,w;
    int cnt=0;//从1开始 ++cnt
    for(int i=0;i<m;i++)
    {
        scanf("%s%s%d",s,t,&w);
        if(q[s]==0) q[s]=++cnt;
        if(q[t]==0) q[t]=++cnt;
        u=q[s],v=q[t];
        if(g[u][v]>w) g[u][v]=g[v][u]=w;//无向图
    }
    n=cnt;//点数
    scanf("%d",&K);//度限制
}
int main()
{
    while(scanf("%d",&m)==1)
    {
        input();
        source=q["Park"];//度限制点
        ans=0;
        solve();
        printf("Total miles driven: %d/n",ans);
    }
 return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值