POJ 1639

Picnic Planning
Time Limit: 5000MS Memory Limit: 10000K
Total Submissions: 10742 Accepted: 3885

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

#include"map"
#include"string"
#include"cmath"
#include"cstdio"
#include"iostream"
#include"cstring"
#include"algorithm"
#include"vector"
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 105;
map <string,int> mp;
string s1,s2;
int cnt;
int dis[maxn],fro[maxn];
int side[maxn][maxn];
int tail,head[maxn];
int K,ans;
int fa[maxn];
bool vis[maxn][maxn];

int getfa(int x)
{
    if(x == fa[x]) return x;
    return fa[x] = getfa(fa[x]);
}

struct edge{
    int u,v,nxt,c;
    edge(){}
    edge(int u, int v,int nxt, int c) : u(u),v(v), nxt(nxt), c(c){}
}e[maxn*maxn];

struct node{
    int u,v,c;
    node(){}
    node(int u, int v, int c):u(u),v(v),c(c){}
}dp[maxn];

void init()
{
    ans = cnt = 0;
    tail = -1;
    mp.clear();
    memset(head,-1,sizeof(head));
    memset(side,0x3f,sizeof(side));
    memset(vis,0,sizeof(vis));
    memset(dis,0x3f,sizeof(dis));
    memset(fro,0,sizeof(fro));
}

void add(int u, int v, int c)
{
    e[++tail] = edge(u,v,head[u],c);
    head[u] = tail;
}


int get_num(string s)
{
    if(mp.find(s) == mp.end()){
        mp[s] = ++cnt;
    }
    return mp[s];
}

bool cmp(edge a, edge b)
{
    return a.c < b.c;
}

void kruskal()
{
    sort(e,e+tail,cmp);
    for(int i = 1; i<= cnt; i++)
        fa[i] = i;
    for(int i = 0; i <= tail; i++){
        int u = e[i].u, v = e[i].v;
        if(u == 1 || v == 1) continue;
        int fu = getfa(u),   fv = getfa(v);
        if(fu == fv) continue;

        vis[u][v] = vis[v][u] = 1;
        fa[fu] = fv;
        ans += e[i].c;
    }
}

void dfs(int u, int pre)
{
    for(int i = 2; i <= cnt; i++)
    if(vis[u][i] && i!=pre){
        if(dp[u].c > side[u][i]){
            dp[i] = dp[u];
        }
        else{
            dp[i] = node(u,i,side[u][i]);
        }
        dfs(i,u);
    }
}

void solve()
{
    int m = 0;
    for(int i = 2; i<= cnt; i++){
        if(side[1][i] != INF){
            int fv = getfa(i);
            if(dis[fv] > side[1][i]){
                dis[fv] = side[1][i];
                fro[fv] = i;
            }
        }
    }

    for(int i = 2; i<= cnt; i++){
        if(dis[i] != INF){
            ans += dis[i];
            vis[1][fro[i]] = vis[fro[i]][1] = 1;
            ++m;
        }
    }

    for(int i = m+1; i <= K; i++){
        memset(dp,-1,sizeof(dp));
        dfs(1,-1);
        int minx = 0,temp = 1;

        for(int j = 2; j <= cnt; j++){
            if(side[1][j] != INF && !vis[1][j]){
                int val = side[1][j] - dp[j].c;
                if(minx > val){
                    minx = val;
                    temp = j;
                }
            }
        }

        if(minx >= 0) break;
        int u = dp[temp].u, v = dp[temp].v;
        vis[u][v] = vis[v][u] = 0;
        vis[1][temp] = 1;
        ans += minx;
    }
    printf("Total miles driven: %d\n",ans);
}

int main()
{
    int n,dis,u,v;
    scanf("%d",&n);

    init();
    get_num("Park");
    for(int i = 1; i<= n; i++){
        cin>>s1>>s2;
        scanf("%d",&dis);
        u = get_num(s1);
        v = get_num(s2);
        side[u][v] = min(side[u][v],dis);
        side[v][u] = min(side[v][u],dis);
    }

    for(int i = 1; i <= cnt; i++){
        for(int j = i+1; j <= cnt; j++){
            if(side[i][j] != INF){
                add(i,j,side[i][j]);
                add(j,i,side[j][i]);
            }
        }
    }
    scanf("%d",&K);
    kruskal();
    solve();
    return 0;
}



字符串“park”所连的边不能超过k条的最小生成树。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值