CF76A.Gift

题意:noi2014魔法森林弱化版QwQ,最小化\(max(g_i)*G + max(s_i)*S\)的最小生成树


考虑按g升序加边,用已在生成树中的边和新加入的边求当前最小生成树。

复杂度\(O(nm)\)

vector真好用

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 205, M = 50005;
const ll inf = 2e18+5; inline int read() {
int x = 0, f = 1; char c = getchar();
while(c<'0' || c>'9') {if(c=='-') f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
} int n, m, G, S;
struct edge {
int x, y, g, s;
bool operator <(const edge &r) const {
return g < r.g;
}
} e[M];
bool cmp_s(const edge &a, const edge &b) {
return a.s < b.s;
}
int fa[N];
int find(int x) {return x == fa[x] ? x : fa[x] = find(fa[x]);} vector<edge> v;
vector<edge>::iterator it;
ll ans = inf;
ll kruskal(int m) { //printf("kruskal %d\n", m);
int mg = 0, ms = 0;
for(int i=1; i<=n; i++) fa[i] = i;
int cnt = 0;
for(it = v.begin(); it != v.end(); ) {
int x = find(it->x), y = find(it->y);
//printf("%d %d %d %d\n", e[i].x, e[i].y, x, y); cout << endl;
if(x == y) {
it = v.erase(it);
continue;
}
fa[y] = x;
mg = max(mg, it->g);
ms = max(ms, it->s);
if(++cnt == n-1) break;
it++;
}
if(cnt == n-1) return (ll) mg * G + (ll) ms * S;
else return inf;
} void solve() {
sort(e+1, e+1+m);
for(int i=1; i<=n-2; i++) v.push_back(e[i]);
sort(v.begin(), v.end(), cmp_s);
for(int p=n-1; p<=m; p++) { //printf("ppp %d\n", p);
edge now = e[p];
int flag = 0;
for(it = v.begin(); it != v.end(); it++) {
if(it->s > now.s) {
v.insert(it, now);
flag = 1;
break;
}
}
if(!flag) v.insert(it, now); ans = min(ans, kruskal(p));
}
if(ans == inf) ans = -1;
cout << ans;
} int main() {
//freopen("in", "r", stdin);
cin >> n >> m >> G >> S;;
for(int i=1; i<=m; i++) {
e[i].x = read();
e[i].y = read();
e[i].g = read();
e[i].s = read();
}
solve();
}

CF76A.Gift [最小生成树]的更多相关文章

  1. CodeForces 76A Gift - 最小生成树

    The kingdom of Olympia consists of N cities and M bidirectional roads. Each road connects exactly tw ...

  2. CF76A Gift

    题目描述 有一个国家有N个城市和M条道路,这些道路可能连接相同的城市,也有可能两个城市之间有多条道路. 有一天,有一伙强盗占领了这个国家的所有的道路.他们要求国王献给他们礼物,进而根据礼物的多少而放弃 ...

  3. CodeForces - 76A:Gift (最小生成树 解决单调性问题是思想)

    题意:给定N点M边的无向连通图,每条边有两个权值(g,s). 给定G,S. 让你给出一组(g0,s0)使得图中仅留下g<=g0, s<=s0的边之后,依然连通,并求Gg0+Ss0的最小值. ...

  4. 最小生成树(Kruskal算法-边集数组)

    以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...

  5. 最小生成树计数 bzoj 1016

    最小生成树计数 (1s 128M) award [问题描述] 现在给出了一个简单无向加权图.你不满足于求出这个图的最小生成树,而希望知道这个图中有多少个不同的最小生成树.(如果两颗最小生成树中至少有一 ...

  6. USACO . Greedy Gift Givers

    Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts ...

  7. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  8. 【BZOJ 1016】【JSOI 2008】最小生成树计数

    http://www.lydsy.com/JudgeOnline/problem.php?id=1016 统计每一个边权在最小生成树中使用的次数,这个次数在任何一个最小生成树中都是固定的(归纳证明). ...

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

    Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...

随机推荐

  1. 使用 LD_PRELOAD 变量拦截调用

    背景&原理 很多 a.out 程序都依赖动态库 libc.so, 比如使用 strcmp() 比较密码, 其实是不安全的 使用 LD_PRELOAD 变量可以使该变量中的可链接文件(编译时使用 ...

  2. THUWC2019 GG记

    所以要什么时候才不会出现像这样的滚粗记呢? Day-?~Day-4 我也不清楚具体干了什么 不过学了很多东西,至少相对于去年还是个小菜鸡,今年已经变成大菜鸡了 Day-3~Day-1 几乎就是复习前面 ...

  3. 动态解析xml,并生成excel,然后发邮件。

    直接贴代码了! DECLARE @CurrentServer NVARCHAR(100)DECLARE @CurrentDatabase NVARCHAR(100)DECLARE @CurrentLo ...

  4. Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) B. Weakened Common Divis

    题目链接 让你找一个数,使得这个数,可以被每个二元组的两个数中的一个数整除. 先将第一个二元组的两个数质因数分解一下,分解的质数加入set中,然后,对剩下的n-1个二元组进行遍历,每次遍历到的二元组对 ...

  5. 移动文件(git mv)

    使用git mv命令将mian.c移动为main2.c $ git mv main.c main2.c D:\Git\test (master -> origin) $ git status O ...

  6. list转换string

    方法1 "".join 若list中为字符型 str = "".join(list) >>> L=['a','b','c'] >> ...

  7. 通过命令修改mysql的提示符(转)

    本文转自冲出地球的博客原文链接:https://www.cnblogs.com/zhengchenhui/p/6649235.html 在cmd窗口操作mysql数据库的时候,前面的提示符永远都是my ...

  8. 清北学堂学习总结day3

    小学知识总结 上午篇 •积性函数的卷积公式 (1)(f * g)( n ) = ∑(d|n) f( d ) x g ( n / d ) (2)代码实现 LL f[N], g[N], h[N]; voi ...

  9. Beans 自动装配

    http://wiki.jikexueyuan.com/project/spring/beans-auto-wiring/spring-autowiring-byname.html

  10. 基于注解的SpringMVC自定义DispatcherServlet配置

    通过重载AbstractAnnotationConfigDispatcherServletInitializer实现类的customizeRegistration()方法来自定义DispatcherS ...