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. Flask-Login用户登陆

    Flask-Login Flask-Login 提供用户会话管理,处理常见的登录.退出和注册的任务. Flask-Login 没有绑定数据库,可以从数据库回调用户对象. 安装flask-login p ...

  2. 使用git把本地目录传到远程仓库

    需求: 要把本地不为空的一个目录和远程现有的一个仓库关联 步骤如下: git init //生成.git目录 git add . //把当前目录里的文件加入到暂存区 git commit -m '上传 ...

  3. 24 Game

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  4. PostCSS 实战

    专题截图: 项目截图: 目录说明: dest/ 发布代码文件夹: src/              预编译代码文件夹; node_modules    node 插件; gulpfile.js   ...

  5. C# 断言 Assert

    重构-断言 现象:某一段代码需要对程序状态做出某种假设 做法:以断言明确表现这种假设 动机: 常常有这种一段代码:只有某个条件为真是,该改名才能正常运行. 通常假设这样的假设并没有代码中明确表现出来, ...

  6. SQLAlchemy+Flask-RESTful使用(四)

    前言 顺利出到4啦,其实学习过程中发现了不少错误,不过有些实在是没啥代表性. 最近买了两本小程序和安卓方面的书,其实从初中开始,想搞编程的目的就是写些安卓软件. 现在看来不太可能了.拿来当当兴趣爱好还 ...

  7. matlab读取图片的异常表现

    在对人脸图片进行对齐时,发现有两张人脸图片一直检测不到人脸,但这两张图片中的人脸明明很好检测(证件照),经过 排查,最终发现问题所在:图片的格式不对. img = imread("**.jp ...

  8. Chrome浏览器自动填充<input>标签的密码

    问题:登录页面登录时,Chrome浏览器保存了用户名和密码,在其他页面管理其他的账户和密码时,密码框先是显示正确的密码,然后一闪而过被覆盖. 原因:问了技术主管才得知,Chrome浏览器中的,保存用户 ...

  9. java 静态代理模式

    package proxy.staticproxy; public interface IStar { public void sing(); } package proxy.staticproxy; ...

  10. 一个Tomcat下部署两个,甚至多个项目

    是的這是我粘過來的 Tomcat目录下的结构如图: 第一步:Tomcat默认空间webapps,中已经存在一个项目了,此时要增加一个项目运行可以将原本webapps目录copa一份, 改名为webap ...