7.29 总结
Cabbage_Squirrel · · 个人记录
P1194 买礼物
题意
一共要买
分析
把每件商品当做无向图的一个节点,优惠价
全部不使用优惠,总花费为
使用 Kruskal 选边,从小到大遍历优惠边。两个商品不在同一连通块,且优惠价比单件原价更低,就用优惠替换原价,总费用减少
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e2 + 5;
struct node {
int x,y,z;
}e[N * N];
bool cmp(node u,node v) {
return u.z < v.z;
}
int fa[N],vis[N],idx,a,b,ans;
int find(int x) {
if(fa[x] == x) return fa[x];
else return fa[x] = find(fa[x]);
}
void kruskal() {
sort(e + 1,e + idx + 1,cmp);
for(int i = 1;i <= idx;i ++) {
int x = e[i].x,y = e[i].y;
int RootX = find(x);
int RootY = find(y);
if(RootX != RootY && a > e[i].z) {
ans = ans - a + e[i].z;
fa[RootY] = RootX;
}
}
}
signed main() {
cin >> a >> b;
ans = a * b;
for(int i = 1;i <= b;i ++) {
fa[i] = i;
}
for(int i = 1;i <= b;i ++) {
for(int j = 1;j <= b;j ++) {
int x;
cin >> x;
if(x != 0) {
idx ++;
e[idx] = {i,j,x};
}
}
}
kruskal();
cout << ans;
}
T707651 新的开始
题意
现有
- 单独在矿井
i 修建发电站,花费v_i ;一座发电站可给任意多连通矿井供电。 - 在矿井
i,j 之间架设电网,花费p_{i,j} ,电网双向互通,保证p_{i,j}=p_{j,i},p_{i,i}=0 。
需要让全部矿井通电,求整体最小总开销。
分析
可以新建虚拟节点统一两种花费,增设编号为
因为每个连通块必须恰好连向超级源一次,对应块内有且仅有一处建电站且连通块内部用便宜电网相连所以直接求
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e2 + 5;
struct node {
int x,y,z;
}e[N * N];
bool cmp(node u,node v) {
return u.z < v.z;
}
int fa[N],vis[N],ans,idx,n;
int find(int x) {
if(fa[x] == x) return fa[x];
else return fa[x] = find(fa[x]);
}
void kruskal() {
sort(e + 1,e + idx + 1,cmp);
for(int i = 1;i <= idx;i ++) {
int x = e[i].x,y = e[i].y;
int RootX = find(x);
int RootY = find(y);
if(RootX != RootY) {
ans += e[i].z;
fa[RootY] = RootX;
}
}
}
signed main() {
cin >> n;
for(int i = 1;i <= n;i ++) {
fa[i] = i;
}
for(int i = 1;i <= n;i ++) {
int v;
cin >> v;
e[++ idx] = {i,n + 1,v};
}
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
int x;
cin >> x;
e[++ idx] = {i,j,x};
}
}
kruskal();
cout << ans;
}
P4826 [USACO15FEB] Superbull S
将每两个队伍比赛得分情况抽象成一条带权边
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e3 + 5;
struct node {
int x,y,z;
}e[N * N];
bool cmp(node u,node v) {
return u.z > v.z;
}
int fa[N],a[N],idx,n;
int find(int x) {
if(fa[x] == x) return fa[x];
else return fa[x] = find(fa[x]);
}
void kruskal() {
sort(e + 1,e + idx + 1,cmp);
int ans = 0,cnt = 0;
for(int i = 1;i <= idx;i ++) {
int x = e[i].x,y = e[i].y;
int RootX = find(x);
int RootY = find(y);
if(RootX != RootY) {
fa[RootY] = RootX;
ans += e[i].z;
}
}
cout << ans;
}
signed main() {
cin >> n;
for(int i = 1;i <= n;i ++) {
fa[i] = i;
}
for(int i = 1;i <= n;i ++) {
cin >> a[i];
}
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
if(i == j) continue;
++ idx;
e[idx].x = i;
e[idx].y = j;
e[idx].z = a[i] ^ a[j];
}
}
kruskal();
}
最小生成树
最小生成树是图论中连接无向图所有顶点的极小连通子图,其边权总和达到最小值。该结构包含原图所有顶点及保持连通的
在一给定的无向图
用 Kruskal 算法,即先把所有边按权重从小到大排好队,然后从头开始挑,只要这条边连的两个点还没通(不构成环),就把它加进来,直到所有点都连上 。
#include<bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
const int M = 2e5 + 5;
struct node{
int x,y,z;
}a[M];
bool cmp(node u,node v) {
return u.z < v.z;
}
int n,m,fa[N],ans;
int find(int x) {
if(fa[x] == x) return fa[x];
return fa[x] = find(fa[x]);
}
void kruskal() {
sort(a + 1,a + 1 + m,cmp);
int cnt = n;
for(int i = 1;i <= m;i ++) {
int x = a[i].x,y = a[i].y,z = a[i].z;
int RootX = find(x);
int RootY = find(y);
if(RootX != RootY) {
ans += z;
fa[RootY] = RootX;
cnt --;
}
}
if(cnt != 1) {
cout << "orz";
} else {
cout << ans;
}
}
int main() {
cin >> n >> m;
for(int i = 1;i <= n;i ++) fa[i] = i;
for(int i = 1;i <= m;i ++) {
cin >> a[i].x >> a[i].y >> a[i].z;
}
kruskal();
}
点权化边权
首先,给出一个权值是点权的例子:
有一个无向图
G=\{V,E\} ,其中点集V 中的元素以一个有序二元组v=\{u,w\} 给出,表示点u 的权值为w , 边集E 中的元素以一个有序二元组e=\{u,v\} 给出,表示点v 的直接前驱为v 。
在这个问题中,我们每到一个点
既然每走一个点就要付出该点相应的代价,那不妨改成
这样走一条边代价不变,也转化成了边权,但边权的计算因体=题而异,比如在 P1938 中就是