Picnic Planning
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10642   Accepted: 3862

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

以下内容均为转载(代码风格也没改,就是这么懒=_=)http://www.cnblogs.com/jackge/archive/2013/05/12/3073669.html 膜

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<climits>
#include<queue>

using namespace std;

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 a.cap<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_side[]更新
    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 i,res,cnt;
    for(i=0;i<n;i++){
        dis[i]=INT_MAX;
        clo[i]=pre[i]=fst[i]=0;
    }
    res=0,cnt=1;    //除去根节点后,图中的连通子图个数,即最小生成树个数
    for(i=1;i<n;i++)
        if(!clo[i])
            res+=Prim(i,cnt++);
    for(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(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(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(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(){

//freopen("input.txt","r",stdin);
    char s1[20],s2[20];
    int cap;
    while(~scanf("%d",&m)){
        mp["Park"]=0;
        n=1;
        memset(g,0,sizeof(g));
        while(m--){
            scanf("%s %s %d",s1,s2,&cap);
            if(!mp.count(s1))
                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)
                g[u][v]=g[v][u]=cap;
        }
        scanf("%d",&k);
        Solve();
    }
    return 0;
}

由于一直觉得上一份有点错误 ,所以又找了一份,发现这两个思想一样啊 =_=还是觉得有点错误,先挖坑以后来填

#include <cstdio>
#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;

const int inf = 0x3f3f3f3f;
const int N = 1111;
int n,k;
map<string,int> name;
vector<string> vec;
int g[N][N],vis[N];
int li[N][N],lowcost[N],pre[N];
int kk,ans;
struct node{
 int u,v,len;
 node(){}
 node(int _u,int _v,int _del):u(_u),v(_v),len(_del){}
}del[N];
void prim(int u){
 for(int i = 1;i < n;i++){
  lowcost[i] = g[u][i];
  pre[i] = u;
 }
 vis[u] = true;
 while(true){
  int pr = -1 , mind = inf;
  for(int i = 1;i < n;i++)
   if(!vis[i] && lowcost[i] < mind){
    mind = lowcost[i];
    pr = i;
   }
  if(pr == -1) break;
  ans += mind;
  li[pre[pr]][pr] = li[pr][pre[pr]] = 1;
  if(g[0][kk] > g[0][pr]) kk = pr;
  vis[pr] = true;
  for(int i = 1;i < n;i++)
   if(!vis[i] && lowcost[i] > g[pr][i])
    lowcost[i] = g[pr][i] , pre[i] = pr;
 }
}
void dfs(int u,int fa,int del_u,int del_v){
 for(int i = 1;i < n;i++){
  if(li[u][i] && i!=fa){
   if(fa == -1 || g[del_u][del_v] < g[u][i]){
    del[i] = node(u,i,g[u][i]);
    dfs(i,u,u,i);
   }else{
    del[i] = node(del_u,del_v,g[del_u][del_v]);
    dfs(i,u,del_u,del_v);
   }
  }
 }
}
void solve(){
 memset(vis,0,sizeof(vis));
 memset(li,0,sizeof(li));
 ans = 0;
 vis[0] = true;
 for(int i = 1;i < n;i++){
  if(vis[i]) continue;
  kk = i;
  k--;
  prim(i);
  li[0][kk] = li[kk][0] = 1;
  ans += g[0][kk];
  dfs(kk,-1,-1,-1);
 }
 memset(vis,0,sizeof(vis));
 while(k--){
  int c = 0 , todel = -1;
  for(int i = 1;i < n;i++){
   if(li[0][i] || g[0][i] == inf) continue;
   if(c > g[0][i] - del[i].len){
    c = g[0][i] - del[i].len;
    todel = i;
   }
  }
  if(c == 0) break;
  ans += c;
  li[0][todel] = li[todel][0] = 1;
  li[del[todel].u][del[todel].v]=li[del[todel].v][del[todel].u] = 0;
  dfs(todel, 0 , -1,-1);
 }
 printf("Total miles driven: %d\n",ans);
}
int getNum(const string &s){
 if(name.count(s) > 0)
  return name[s];
 vec.push_back(s);
 return name[s] = vec.size()-1;
}
void init(){
 memset(g,0x3f,sizeof(g));
 name.clear();
 vec.clear();
 getNum("Park");
 string su,sv;
 for(int i = 0,w;i < n;i++){
  cin >> su >> sv;
  cin >> w;
  int u = getNum(su) ,v = getNum(sv);
  if(g[u][v] > w)
   g[u][v] = g[v][u] = w;
 }
 n = vec.size();
 scanf("%d",&k);
}
int main(){
 while(scanf("%d",&n)!=EOF){
  init();
  solve();
 }
 return 0;
}

限制某个顶点度数的最小生成树 poj1639的更多相关文章

  1. POJ-1639 Picnic Planning 度数限制最小生成树

    解法参考的论文:https://wenku.baidu.com/view/8abefb175f0e7cd1842536aa.html 觉得网上的代码好像都是用邻接矩阵来实现的,觉得可能数据量大了会比较 ...

  2. POJ1639顶点度限制最小生成树

    题目:http://poj.org/problem?id=1639 见汪汀的<最小生成树问题的拓展>. 大体是先忽略与根节点相连的边,做一遍kruscal,得到几个连通块和一个根节点: 然 ...

  3. UVA1537 Picnic Planning(思维+最小生成树)

    将1号点从图中去掉过后,图会形成几个连通块,那么我们首先可以在这些连通块内部求最小生成树. 假设有\(tot\)个连通块,那么我们会从1号点至少选\(tot\)个出边,使得图连通.这时我们贪心地选择最 ...

  4. 最小生成树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind

    最小支撑树树--Prim算法,基于优先队列的Prim算法,Kruskal算法,Boruvka算法,“等价类”UnionFind 最小支撑树树 前几节中介绍的算法都是针对无权图的,本节将介绍带权图的最小 ...

  5. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  6. [数据结构]最小生成树算法Prim和Kruskal算法

    最小生成树 在含有n个顶点的连通图中选择n-1条边,构成一棵极小连通子图,并使该连通子图中n-1条边上权值之和达到最小,则称其为连通网的最小生成树.  例如,对于如上图G4所示的连通网可以有多棵权值总 ...

  7. Prim算法和Kruskal算法求最小生成树

    Prim算法 连通分量是指图的一个子图,子图中任意两个顶点之间都是可达的.最小生成树是连通图的一个连通分量,且所有边的权值和最小. 最小生成树中,一个顶点最多与两个顶点邻接:若连通图有n个顶点,则最小 ...

  8. 图解最小生成树 - 普里姆(Prim)算法

    我们在图的定义中说过,带有权值的图就是网结构.一个连通图的生成树是一个极小的连通子图,它含有图中全部的顶点,但只有足以构成一棵树的n-1条边.所谓的最小成本,就是n个顶点,用n-1条边把一个连通图连接 ...

  9. 最小生成树之Kruskal算法和Prim算法

    依据图的深度优先遍历和广度优先遍历,能够用最少的边连接全部的顶点,并且不会形成回路. 这样的连接全部顶点并且路径唯一的树型结构称为生成树或扩展树.实际中.希望产生的生成树的全部边的权值和最小,称之为最 ...

随机推荐

  1. java中ThreadLocalRandom的使用

    java中ThreadLocalRandom的使用 在java中我们通常会需要使用到java.util.Random来便利的生产随机数.但是Random是线程安全的,如果要在线程环境中的话就有可能产生 ...

  2. Zookeeper之Error contacting service. It is probably not running.

    安装ZooKeeper时,无论是修改zoo.cfg:还是myid,都检查了几遍都没有错误.但是开启Zookeeper服务时出现: Error contacting service. It is pro ...

  3. webpack4.x下babel的安装、配置及使用

    前言 目前,ES6(ES2015)这样的语法已经得到很大规模的应用,它具有更加简洁.功能更加强大的特点,实际项目中很可能会使用采用了ES6语法的模块,但浏览器对于ES6语法的支持并不完善.为了实现兼容 ...

  4. Entity Framework使用EntityState和Attach来保存数据变化以及更新实体的个别字段

    在使用Entity Framework作为ORM来存取数据的过程中,最常规的操作就是对数据对象的更新.本文将会包含如何Attach Entity到一个数据Context中,以及如何使用EntitySt ...

  5. Alink漫谈(一) : 从KMeans算法实现不同看Alink设计思想

    Alink漫谈(一) : 从KMeans算法实现不同看Alink设计思想 目录 Alink漫谈(一) : 从KMeans算法实现不同看Alink设计思想 0x00 摘要 0x01 Flink 是什么 ...

  6. zabbix日常问题总结

    1.connection to database 'zabbix' failed: [1040] Too many connections 问题:数据库连接池太少解决:增加数据库连接池步骤:(1).进 ...

  7. C. K-Complete Word(小小的并查集啦~)

    永久打开的传送门 \(\color{Pink}{-------------分割-------------}\) \(n最大有2e5,那么暴力一定不行,找规律\) \(我们发现第i位的字符一定和第i+k ...

  8. 基于KepServer实现与S7-1200PLC之间的通信

    对于学习上位机开发,有一种通信方式是必须要了解的,那就是OPC是OLE for Process Control的简称,然而随着技术的不断发展,人们开始对它有了新的定义,比如Open Platform ...

  9. 【Hadoop离线基础总结】MapReduce入门

    MapReduce入门 Mapreduce思想 概述 MapReduce的思想核心是分而治之,适用于大量复杂的任务处理场景(大规模数据处理场景). 最主要的特点就是把一个大的问题,划分成很多小的子问题 ...

  10. [poj 1743] Musical Theme 后缀数组 or hash

    Musical Theme 题意 给出n个1-88组成的音符,让找出一个最长的连续子序列,满足以下条件: 长度大于5 不重叠的出现两次(这里的出现可以经过变调,即这个序列的每个数字全都加上一个整数x) ...