POJ 1639 Picnic Planning

Picnic Planning

Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 10561 Accepted: 3836
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
Source

East Central North America 2000

/*
strcpy(s1,s2);strcpy函数的意思是:把字符串s2中的内容
copy到s1中,连字符串结束标志也一起copy.
这样s1在内存中的存放为:ch\0;
strcmp函数是比较两个字符串的大小,返回比较的结果。
strcmp(s1,s2);
字符串1小于字符串2,strcmp函数返回一个负值;
字符串1等于字符串2,strcmp函数返回零;
字符串1大于字符串2,strcmp函数返回一个正值;
/************************************************* 
算法引入: 
最小k度限制生成树,就是指有特殊的某一点的度不能超过k时的最小生成树; 
如果T是G的一个生成树且dT(v0)=k,则称T为G的k度限制生成树; 
G中权值和最小的k度限制生成树称为G的最小k度生成树; 

算法思想: 
设特殊的那点为v0,先把v0删除,求出剩下连通图的所有最小生成树; 
假如有m棵最小生成树,那么这些生成树必定要跟v0点相连; 
也就是说这棵生成树的v0点至少是m度的; 
若m>k,条件不成立,无法找到最小k度限制生成树; 
若m<=k,则枚举m到k的所有最小生成树,即一步步将v0点的度加1,直到v0点的度为k为止; 
则v0点度从m到k的(k-m+1)棵最小生成树中最小的那棵即为答案; 

算法步骤: 
(1)先求出最小m度限制生成树: 
原图中去掉和V0相连的所有边(可以先存两个图,建议一个邻接矩阵,一个邻接表,用方便枚举边的邻接表来构造新图); 
得到m个连通分量,则这m个连通分量必须通过v0来连接; 
则在图G的所有生成树中dT(v0)>=m; 
则当k<m时,问题无解; 
对每个连通分量求一次最小生成树; 
对于每个连通分量V’,用一条与V0直接连接的最小的边把它与V0点连接起来,使其整体成为一个生成树; 
就得到了一个m度限制生成树,即为最小m度限制生成树; 

(2)由最小m度限制生成树得到最小m+1度限制生成树; 
连接和V0相邻的点v,则可以知道一定会有一个环出现(因为原来是一个生成树); 
只要找到这个环上的最大权边(不能与v0点直接相连)并删除,就可以得到一个m+1度限制生成树; 
枚举所有和V0相邻点v,找到替换后,增加权值最小的一次替换(如果找不到这样的边,就说明已经求出); 
就可以求得m+1度限制生成树; 
如果每添加一条边,都需要对环上的边一一枚举,时间复杂度将比较高; 
用动态规划解决; 
设dp(v)为路径v0—v上与v0无关联且权值最大的边; 
定义father(v)为v的父结点,由此可以得到状态转移方程: 
dp(v)=max(dp(father(v)),ω(father(v),v)); 
边界条件为dp[v0]=-∞(因为每次寻找的是最大边,所以-∞不会被考虑),dp[v’]=-∞|(v0,v’)∈E(T); 

(3)当dT(v0)=k时停止(即当V0的度为k的时候停止),但不一定k的时候最优; 

算法实现: 
并查集+kruskal; 
首先,每个连通分量的的最小生成树可以直接用一个循环,循环着Kruskal求出; 
这里利用了联通分量间的独立性,对每个连通分量分别求最小生成树,和放在一起求,毫不影响; 
而且kruskral算法保证了各连通分量边的有序性; 
找最小边的时候,可以用动态规划,也可以这么做: 
先走一个循环,但我们需要逆过来加边,将与v0关联的所有边从小到达排序; 
然后将各连通分量连接起来,利用并查集可以保证每个连通分量只有一条边与v0相连; 
由于边已经从小到达排序,故与每个连通分量相连的边就是每个连通分量与v0相连中的最小边; 
然后求m+1度的最小生成树时,可以直接用DFS,最小生成树要一直求到k度,然后从中找出一个最优值; 

算法测试: 
PKU1639(Picnic Planning); 

题目大意: 
给出m条边,每条边有两个端点和一个权值; 
求这个图在满足以下条件的情况下的最小生成树; 
在所有点中,有一个特殊点Park,它在求得的最小生成树中的度必须小于等于某个值; 
come from 始往流离,终比成章
**************************************************/  
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 21;
const int MAX_EDGE_WEIGHT = 1000000000;
struct Edge{int w,sv,ev;};
Edge mstEdge[maxn-1];
int g[maxn][maxn];
int tot_Nodes,n,s;//s 为最小生成树顶点0的最大度数,n是边数
int ans,isCycle,exist[maxn];
char Nodesname[maxn][12];
int VerInd(char* name){
    int rt=0;
    while(rt<tot_Nodes&&strcmp(Nodesname[rt],name)!=0) rt++;
    if(rt==tot_Nodes){
        strcpy(Nodesname[tot_Nodes],name);
        tot_Nodes++;
    }
    return rt;
}
int Mst_Prim(){
    int MST=0;
    for(int i=1;i<tot_Nodes-1;i++){
        mstEdge[i].sv=1;mstEdge[i].ev=i+1;
        mstEdge[i].w=g[1][i+1];
    }
    for(int k=2;k<tot_Nodes;k++){
        int minw=mstEdge[k-1].w,index=k-1;
        for(int j=k;j<tot_Nodes-1;j++)
            if(mstEdge[j].w<minw){
                minw=mstEdge[j].w;
                index=j;
            }

        MST+=minw;
        Edge tmp=mstEdge[k-1];
        mstEdge[k-1]=mstEdge[index];
        mstEdge[index]=tmp;

        int p=mstEdge[k-1].ev;
        for(int i=k;i<tot_Nodes-1;i++){
            int v=mstEdge[i].ev,w=g[p][v];
            if(w<mstEdge[i].w){
                mstEdge[i].w=w;
                mstEdge[i].sv=p;
            }
        }
    }
    return MST;
}
void Max_Edge_Incycle(int mv,int sv,int ev,int& maxw,int& ind){
    if(ev==mv){ isCycle=1;return ; }
    for(int i=0;i<tot_Nodes-1;i++){
        if(mstEdge[i].sv!=ev&&mstEdge[i].ev!=ev)
            continue;
        if(mstEdge[i].sv==ev&&mstEdge[i].ev!=sv){
            Max_Edge_Incycle(mv,ev,mstEdge[i].ev,maxw,ind);
            if(isCycle){
                if(maxw<mstEdge[i].w&&mstEdge[i].ev!=0){
                    maxw=mstEdge[i].w;
                    ind=i;
                }
                break;
            }
        }
        else if(mstEdge[i].ev==ev&&mstEdge[i].sv!=sv){
                Max_Edge_Incycle(mv,ev,mstEdge[i].sv,maxw,ind);
                if(isCycle){
                    if(maxw<mstEdge[i].w&&mstEdge[i].sv!=0){
                        maxw=mstEdge[i].w;
                        ind=i;
                    }
                    break;
                }
            }
    }
}
void Solve(){
    ans=Mst_Prim();
    //查找最小的边(0,ev)的权值 minw
    int minw=MAX_EDGE_WEIGHT+1,ev=-1;
    for(int i=1;i<tot_Nodes;i++){
        exist[i]=0;
        if(g[0][i]<minw){
            minw=g[0][i];
            ev=i;
        }
    }
    exist[ev]=1; ans+=minw;
    //将(0,ev)加入mstEdge中
    mstEdge[0].sv=0;mstEdge[0].ev=ev;mstEdge[0].w=minw;
    //枚举顶点0的度数等于2到s 求得最优解
    for(int degree=2;degree<=s;degree++){
        int dec=MAX_EDGE_WEIGHT+1,edgeInd=-1;
        ev=-1;
        for(int i=1;i<tot_Nodes;i++){
            if(exist[i]==1) continue;
            int maxw=0,ind=-1;
            isCycle=0;
            Max_Edge_Incycle(0,0,i,maxw,ind);
            if(dec>g[0][i]-maxw){
                dec=g[0][i]-maxw;
                edgeInd=ind;
                ev=i;
            }
        }
        if(dec>=0) break;
        else {
            if(edgeInd!=-1){
                mstEdge[edgeInd].sv=0;
                mstEdge[edgeInd].ev=ev;
                mstEdge[edgeInd].w=g[0][ev];
            }
            exist[ev]=1;ans+=dec;
        }
    }
}
int main(){
    char name1[12],name2[12];int dist;
    for(int i=0;i<maxn;i++)
        for(int j=0;j<maxn;j++)
            g[i][j]=MAX_EDGE_WEIGHT;
    tot_Nodes=0;
    cin>>n;
    strcpy(Nodesname[tot_Nodes],"Park");
    tot_Nodes++;
    for(int i=0;i<n;i++){
        cin>>name1>>name2>>dist;
        int r1=VerInd(name1),r2=VerInd(name2);
        g[r1][r2]=g[r2][r1]=dist;
    }
    cin>>s;
    Solve();
    cout<<"Total miles driven: "<<ans<<endl;
    return 0;
}
/*这个题教会我 格式输出的时候能复制的一定要复制
不能复制的要仔细检查,我的“driven”少打了一个n调了一下午*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七情六欲·

学生党不容易~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值