POJ 1639 Picnic Planning 

Picnic Planning

Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions:11644   Accepted: 4181

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

 

题意限制到终点度数,求最小生成树。一开始想只连最短的s条边,多了就不连了,后来发现明显错了。还找了半天bug

思路:首先连成一个树,如果树的终点度超过了s就一条一条的删去,当然,如果删去这条边必然需要补一条边。然后每次加上删除这条边和补的哪条边的差值,删到只有s条边的时候就是答案。

 

这题有dijkstra算法写,修改一下并查集就行了。每次直接连上Prak的特判一下,两个结点相连,优先连结终点是Park的。

AC代码

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<stack>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const long long mod=1e9+7;
const int maxv=500;          //最大顶点数
const int maxm=1e5+25;          //最大边数
const int INF=0x7fffffff;
const int inf=0x3f3f3f3f;
const double eps=1e-8;

int V,m;                  //顶点数目;
int s,park,dp[maxv],dis[maxv][maxv];
vector<int> v;
struct edge {
    int nu,u,v,cost;
};

bool cmp(const edge  & a,const edge & b) {
    return a.cost<b.cost;
}
edge es[maxm];

int par[maxv];
int u[maxm],us[maxm];
int find(int x) {
    if(x==par[x])return x;
    else {
        int temp=find(par[x]);          //返回直接连上根结点的结点,不直接返回根结点,配合后面删边,不懂的+QQ3035536707或者留言
        par[x]=temp;
        if(temp==park){
            return x;
        }
        else return temp;
    }
}

void unit(int x,int y) {
    x=find(x);          //找到 x 的根节点
    y=find(y);
//    cout<<x<<y<<endl;
    if(par[x]==park)par[y]=x;
    else if(par[y]==park)par[x]=y;      //优先连接Park
    else par[x]=y;           //把 x 的根节点 连接上 y.
}

void init(int n) {
    for(int i=0; i<=n; i++)par[i]=i; //初始化的时候根节点都是自己;
}
int dt,uv;

int dfs(int x) {
    int ans=dis[x][park];
    par[x]=x;           //把这条边删掉
    for(int i= 0; i<m; i++) {
        edge &e=es[i];
        if(u[i])continue;
        if(par[find(e.u)]!=par[find(e.v)]&&find(e.u)!=park&&find(e.v)!=park) {      //如果根节点不相同就连接   注意不能连接park 因为你park的度不能再添回去。
            uv=i;
            par[x]=park;
            return dis[e.v][e.u]-ans;
        }
    }
    par[x]=park;
    return 1e9;
}

int Kruskal() {
    sort(es,es+m,cmp);    //按  边的权值排序;
    init(V);
    int res=0,se=0,t=0;   //se 保存边数。
    for(int i= 0; i<m; i++) {
        edge &e =es[i];
        if(par[find(e.u)]!=par[find(e.v)]) {      //如果根节点不相同就连接
            if(e.u==park) {
                t++;
                v.push_back(e.v);
            }
            if(e.v==park){
                t++;
                v.push_back(e.u);
            }
            unit(e.u,e.v);
            res+=e.cost;
            e.cost=1e9;
            if(++se==V-1)break;   //如果连了V-1条边就跳出;
        }
    }
    sort(es,es+m,cmp);
    m=m-se;
    for(int j=0; j<t-s; j++) {
        dt=1e9;
        int temp=0,ji=-1,jl=-1;         //记录删除的边和添加的边
        for(int i=0; i<v.size(); i++) {
            if(us[i]==1)continue;
            temp=dfs(v[i]);
            if(temp<dt) {
                dt=temp;
                ji=uv; 
                jl=i;
            }
        }
        if(ji==-1)continue;
        u[ji]=1;
        res+=dt;
        us[jl]=1;
    }
    return res;
}
map<string,int> mp;
int main() {
    string s3="Park";
    scanf("%d",&m);
    int ct=1;
    memset(dis,inf,sizeof(dis));
    for(int i=0; i<m; i++) {
        string s1,s2;
        int cost=0;
        cin>>s1>>s2>>cost;
        if(mp[s1]==0) {
            mp[s1]=ct++;
            if(s1==s3) {
                park=ct-1;
            }
        }
        if(mp[s2]==0) {
            mp[s2]=ct++;
            if(s2==s3) {
                park=ct-1;
            }
        }
        es[i].nu=0;
        es[i].u=mp[s1];
        es[i].v=mp[s2];
        dis[mp[s2]][mp[s1]]=dis[mp[s1]][mp[s2]]=min(dis[mp[s2]][mp[s1]],cost);
        es[i].cost=cost;
    }
    scanf("%d",&s);
    V=ct-1;
    printf("Total miles driven: %d\n",Kruskal());
    return 0;
}