POJ 1639 Picnic Planning(最小度限制生成树)
Description
Input
Output
Total miles driven: xxx
where xxx is the total number of miles driven by all the brothers' cars.
#include <cstring>
#include <string>
#include <iostream>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std; const int MAXN = ;
const int INF = 0x7f7f7f7f; struct Node {
int u, v, c, use;
Node() {}
Node(int uu, int vv, int cc): u(uu), v(vv), c(cc), use(false) {}
bool operator < (const Node &rhs) const {
return c < rhs.c;
}
}; map<string, int> mymap;
string s1, s2;
int n, m, k, ecnt, ans, cnt;
Node *p;
int fa[MAXN], head[MAXN], *next;
int mat[MAXN][MAXN]; void init() {
ecnt = n = ;
p = new Node[ * m + ];
next = new int[ * m + ];
mymap.clear();
mymap["Park"] = n++;
memset(mat, , sizeof(mat));
} int get_set(int x) {
return fa[x] == x ? x : get_set(fa[x]);
} void add_edge(int u, int v, int c) {
p[ecnt++] = Node(u, v, c);
p[ecnt++] = Node(v, u, c);
if(mat[u][v] == || c < mat[u][v])
mat[u][v] = mat[v][u] = c;
} void build_link() {
memset(head, -, sizeof(head));
for(int i = ecnt - ; i >= ; --i) {
next[i] = head[p[i].u];
head[p[i].u] = i;
}
} void kruskal_del0() {
ans = cnt = ;
for(int i = ; i < ecnt; ++i) {
if(p[i].u == || p[i].v == ) continue;
int x = get_set(p[i].u), y = get_set(p[i].v);
if(x == y) continue;
fa[x] = y;
p[i].use = p[i ^ ].use = true;
ans += p[i].c;
++cnt;
}
m = n - - cnt;
build_link();
for(int i = head[]; i != -; i = next[i]) {
if(p[i].u && p[i].v) continue;
int x = get_set(p[i].u), y = get_set(p[i].v);
if(x == y) continue;
fa[x] = fa[y] = ;
p[i].use = p[i ^ ].use = true;
ans += p[i].c;
if(++cnt == n - ) break;
}
} void dfs(int x) {
for(int i = head[x]; i != -; i = next[i]) {
if(p[i].use) {
fa[p[i].v] = x;
p[i].use = p[i ^ ].use = false;
dfs(p[i].v);
}
}
} int best[MAXN]; int get_best(int x) {
if(fa[x] == ) return -;
if(best[x] != -) return best[x];
return best[x] = max(mat[x][fa[x]], get_best(fa[x]));
} void exchange_edge() {
while(m++ < k) {
memset(best, -, sizeof(best));
for(int i = ; i < n; ++i) get_best(i);
int a = INF, y = ;
for(int i = head[]; i != -; i = next[i]) {
if(best[p[i].v] != - && a > p[i].c - best[p[i].v]) {
a = p[i].c - best[p[i].v];
y = p[i].v;
}
}
if(a >= ) return ;
ans += a; fa[y] = ;
}
} int main() {
int c;
while(scanf("%d", &m) != EOF) {
init();
while(m--) {
cin>>s1>>s2>>c;
if(mymap.find(s1) == mymap.end()) mymap[s1] = n++;
if(mymap.find(s2) == mymap.end()) mymap[s2] = n++;
add_edge(mymap[s1], mymap[s2], c);
}
scanf("%d", &k);
for(int i = ; i < n; ++i) fa[i] = i;
sort(p, p + ecnt);
kruskal_del0();
dfs();
exchange_edge();
printf("Total miles driven: %d\n", ans);
delete [] p;
delete [] next;
}
}
POJ 1639 Picnic Planning(最小度限制生成树)的更多相关文章
- POJ 1639 Picnic Planning:最小度限制生成树
题目链接:http://poj.org/problem?id=1639 题意: 给你一个无向图,n个节点,m条边,每条边有边权. 让你求一棵最小生成树,同时保证1号节点的度数<=k. 题解: 最 ...
- POJ 1639 Picnic Planning 最小k度生成树
Picnic Planning Time Limit: 5000MS Memory Limit: 10000K Total Submissions:11615 Accepted: 4172 D ...
- [POJ 1639] Picnic Planning
[题目链接] http://poj.org/problem?id=1639 [算法] 首先,我们可以用深度优先遍历求出1号节点去除后有几个联通块 设共有T个联通块,若T > K则无解,否则 : ...
- poj 1639 Picnic Planning 度限制mst
https://vjudge.net/problem/POJ-1639 题意: 有一群人,他们要去某一个地方,每个车可以装无数个人,给出了n条路,包含的信息有路连接的地方,以及路的长度,路是双向的,但 ...
- poj1639 Picnic Planning 最小度数限制生成树
题意:若干个人开车要去park聚会,可是park能停的车是有限的,为k.所以这些人要通过先开车到其它人家中,停车,然后拼车去聚会.另外,车的容量是无限的,他们家停车位也是无限的. 求开车总行程最短. ...
- poj1639,uva1537,uvalive2099,scu1622,fzu1761 Picnic Planning (最小限制生成树)
Picnic Planning Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10742 Accepted: 3885 ...
- POJ1639 - Picnic Planning
原题链接 Description 给出一张个点的无向边权图并钦定点,求使得点的度不超过的最小生成树. Solution 首先无视掉与相连的所有边,原图会变成若干互不连通的个块.对每个块分别求MST,再 ...
- K度限制MST poj 1639
/* k度限制MST:有一个点的度<=k的MST poj 1639 要求1号点的度不超过k 求MST 我们先把1号点扔掉 跑MST 假设有sum个连通分支 然后把这sum个分支连到1上 就得到了 ...
- luogu P5633 最小度限制生成树 wqs二分
LINK:最小度限制生成树 还是WQS二分的模板题 不过相当于我WQS二分的复习题. 对于求出强制k个的答案 dp能做不过复杂度太高了. 世界上定义F(x)表示选出x个的答案 画成图像 其实形成了一个 ...
随机推荐
- datatable去掉表头默认排序
禁用排序:"ordering":false 某一列禁用排序:"orderable":false 以某一列排序:"order":[[x,&qu ...
- npm安装包时 --save 和 --save-dev 的区别
以npm 安装 vue为例 1.npm install vue: 会把vue包安装到node_modules目录中: 不会修改package.json文件: 之后运行npm install命令时,不会 ...
- day 19 反射
1.isinstance, type, issubclass 的含义 isinstance: 判断你给对象时候是xxx类型的.(向上判断) type: 返回xxx对象的数据类型 issubclass ...
- java端连接zookeeper出现unknowHostException错误
连接zookeeper出现异常:unknowHostException 出现这种错误一开始以为是zookeeper的配置文件出了问题,所以一直在找配置文件的问题,但是zookeeper在虚拟机里面是可 ...
- 【blockly教程】Blockly编程案例
案例一 原码反码和补码 我们把一个数在计算机内被表示的二进制形式称为机器数,该数称为这个机器数的真值.机器数有固定的位数,具体是多少位与机器有关,通常是8位或16位.原码:是指符号位用0或1表示,0 ...
- Java和JDK版本的关系
JAVA的版本最开始是1995年的JDK Alpha and Beta版本,第二年发布JDK1.0版本之后就是JDK1.1,JDK1.2.到1998年,不再叫JDK了,而是叫J2SE,但是版本号还是继 ...
- 使用GeoServer发布shp数据为WMS服务和WFS服务
使用GeoServer发布shp数据为WMS服务和WFS服务 1安装GeoServer 2使用GeoServer上传数据 3使用GeoServer发布数据为WMS和WFS 看完本教程,你将学会安装Ge ...
- 虚拟机ubuntu使用串口
1. 电脑的串口默认是在windows系统上,需要把串口转到ubuntu上面,按照下面的步骤先 2. 找到需要使用的串口 3. 在VMWARE里面连接该串口 或者使用方法 4. 成功之后,检查一下ls ...
- macOS 10.13 High Sierra PHP开发环境配置
命令:sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems/MySQLCOM ...
- vscode 全透明背景图
一.前言 08.02更新:已魔改插件 可以直接下载插件使用了 10.18跟新:已发布到vscode扩展 下载地址 下载后手动安装就ok了,具体配置安装后点开插件有说明的!!! 今天看到了博客园 这篇 ...