C - Gr-idian MST
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
Problem Statement
On an xy plane, in an area satisfying 0≤x≤W,0≤y≤H, there is one house at each and every point where both x and y are integers.
There are unpaved roads between every pair of points for which either the x coordinates are equal and the difference between the y coordinates is 1, or the ycoordinates are equal and the difference between the x coordinates is 1.
The cost of paving a road between houses on coordinates (i,j) and (i+1,j) is pi for any value of j, while the cost of paving a road between houses on coordinates(i,j) and (i,j+1) is qj for any value of i.
Mr. Takahashi wants to pave some of these roads and be able to travel between any two houses on paved roads only. Find the solution with the minimum total cost.
Constraints
- 1≦W,H≦105
- 1≦pi≦108(0≦i≦W−1)
- 1≦qj≦108(0≦j≦H−1)
- pi(0≦i≦W−1) is an integer.
- qj(0≦j≦H−1) is an integer.
Input
Inputs are provided from Standard Input in the following form.
W H
p0
:
pW−1
q0
:
qH−1
Output
Output an integer representing the minimum total cost.
Sample Input 1
2 2
3
5
2
7
Sample Output 1
29
It is enough to pave the following eight roads.
- Road connecting houses at (0,0) and (0,1)
- Road connecting houses at (0,1) and (1,1)
- Road connecting houses at (0,2) and (1,2)
- Road connecting houses at (1,0) and (1,1)
- Road connecting houses at (1,0) and (2,0)
- Road connecting houses at (1,1) and (1,2)
- Road connecting houses at (1,2) and (2,2)
- Road connecting houses at (2,0) and (2,1)
Sample Input 2
4 3
2
4
8
1
2
9
3
Sample Output 2
60
类似于克鲁斯卡尔算法求最小生成树。只不过题目中给的点到点的值和往常不同。
/* ***********************************************
Author :guanjun
Created Time :2016/10/11 14:54:59
File Name :1.cpp
************************************************ */
#include <bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 10010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
typedef pair<ll,int> pii;
vector<pii>v;
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
ll h,w,x;
while(scanf("%lld %lld",&w,&h)!=EOF){
v.clear();
for(int i=;i<w;i++){
scanf("%lld",&x);
v.push_back({x,});
}
for(int i=;i<h;i++){
scanf("%lld",&x);
v.push_back({x,});
}
sort(v.begin(),v.end());
ll ans=;
w++;
h++;
for(int i=;i<v.size();i++){
if(v[i].second==){
ans+=w*v[i].first;
h--;
}
else {
ans+=h*v[i].first;
w--;
}
}
printf("%lld\n",ans);
}
return ;
}
C - Gr-idian MST的更多相关文章
- POJ1679 The Unique MST[次小生成树]
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28673 Accepted: 10239 ...
- 基于MST的立体匹配及相关改进(A Non-Local Cost Aggregation Method for Stereo Matching)
怀着很纠结的心情来总结这篇论文,这主要是因为作者提虽然供了源代码,但是我并没有仔细去深究他的code,只是把他的算法加进了自己的项目.希望以后有时间能把MST这一结构自己编程实现!! 论文题目是基于非 ...
- BZOJ 2654 & 玄学二分+MST
题意: 给一张图,边带权且带颜色黑白,求出一棵至少包含k条白边的MST SOL: 正常人都想优先加黑边或者是白边,我也是这么想的...你看先用白边搞一棵k条边的MST...然后维护比较黑边跟白边像堆一 ...
- LA 5713 秦始皇修路 MST
题目链接:http://vjudge.net/contest/144221#problem/A 题意: 秦朝有n个城市,需要修建一些道路使得任意两个城市之间都可以连通.道士徐福声称他可以用法术修路,不 ...
- [poj1679]The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28207 Accepted: 10073 ...
- [BZOJ2654]tree(二分+MST)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2654 分析:此题很奇葩,我们可以给所有白边加上一个权值mid,那么在求得的MST中白边 ...
- CodeForces 125E MST Company
E. MST Company time limit per test 8 seconds memory limit per test 256 megabytes input standard inpu ...
- 2015baidu复赛2 连接的管道(mst && 优先队列prim)
连接的管道 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- ACM/ICPC 之 判别MST唯一性-Kruskal解法(POJ1679)
判别MST是否唯一的例题. POJ1679-The Unique MST 题意:给定图,求MST(最小生成树)是否唯一,唯一输出路径长,否则输出Not Unique! 题解:MST是否唯一取决于是否有 ...
- hdu 4756 MST+树形dp ****
题意:给你n(n = 1000)个二维点,第一个点是power plant,还有n - 1个点是dormitories.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...
随机推荐
- (转)淘淘商城系列——商品搜索功能Dao实现
http://blog.csdn.net/yerenyuan_pku/article/details/72909286 终于进入商品搜索功能的开发中了,本文我来教大家编写实现商品搜索功能的Dao层代码 ...
- SDK _ 静态控件的使用
静态控件的使用 静态控件主要区分两种使用方式:文本 \ 图片 在使用静态控件的时候,ID始终默认为 IDC_STATIC,需要进行更改 怎样通过可视化编程显示一张图片 需要添加一个位图资源 需要添加一 ...
- C++ map使用总结
0. Backgroud 此文章源于博主(sunshinewave),转到自己博客以后方便查看 map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次, ...
- Python学习-变量
什么是变量? 概念:变量就是会变化的量,主要是“变”与“量”二字.变即是“变化”. 特点:与其他编程语言相同,变量是最基本的存储单位,是用来存放数据的容器.可以引用一个具体的数值,进而直接去改变这个引 ...
- db2构建临时结果集
一 values ('1',2,3) 为一行 ‘1’ 2 3 行数据类型可以不同 values ('1',2,3),('f',5,6) 为两行 (values 1,2,3 ...
- eclipse自动提示配置
打开Window->Preferences
- Centos下Yum安装PHP5.5,5.6,7.0及扩展
默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案: 1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 ...
- Django DTL模板语法中的循环
from django.shortcuts import render def index(request): context={ 'books':[ '5年高考3年模拟', '家猪养殖与配种', ' ...
- 【tips】xadmin - django第三方后台管理系统
Django 为大家提供了一个完善的后台管理系统—admin,但是这个后台管理系统总体来说不太适合国人的习惯,所以有大神就使用 bootstrap 和 jQuery,为我们开发了一个第三 方的 Dja ...
- mesh topology for airfoil, wing, blade, turbo
ref Ch. 5, Anderson, CFD the basics with applications numerical grid generation foundations and appl ...