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.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...
随机推荐
- mysql 性能优化索引、缓存、分表、分布式实现方式。
系统针对5000台终端测试结果 索引 目标:优化查询速度3秒以内 需要优化.尽量避免使用select * 来查询对象.使用到哪些属性值就查询出哪些使用即可 首页页面: 设备-组织查询 优化 避免使用s ...
- Ubuntu搭建LAMP开发环境
1.安装Apache sudo apt-get install apache2 测试: 浏览器访问 (如:http://localhost),出现It Works!网页. 查看状态: service ...
- C#读取文件-古文观止(总结一下)
1,读取单个文件 //读取一个文本文件 private void buttonRead_Click(object sender, EventArgs e) { String path = Enviro ...
- Vue.js 观察者(watch)
Vue.js 观察者(watch) watch 属性用于监视 vue 实例上的数据变动,并相应的改变其他变量的值. 用法 实例 1 <!DOCTYPE html> <html> ...
- 计算机网络篇(前端、HTTP)
全端工程师需知道的计算机网络知识 一.网络篇-http报文详解 1. 分类 请求报文 响应报文 2. 报文结构 (一).请求报文 一个HTTP请求报文由请求行(request line).请求头部(h ...
- circumferential averge streamwise velocity using Tecplot and Matlab
Input: results from solver output: circumferential averge physical quantities( such as streamwise ve ...
- BZOJ 2274 [Usaco2011 Feb]Generic Cow Protests
[题解] 很容易可以写出朴素DP方程f[i]=sigma f[j] (sum[i]>=sum[j],1<=j<=i). 于是我们用权值树状数组优化即可. #include<c ...
- Android ToggleButton:状态切换的Button
Android ToggleButton:状态切换的Button Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以 ...
- Eclipse不编译解决方案
原文链接:http://blog.csdn.net/huahuagongzi99999/article/details/7719882 转来自己用 这两天Eclipse 不编译了,无论怎么更改保 ...
- kendo grid 点击更新没有反映
因为没有在dataSource上写schema schema: { model: { id: "DeptId", fields: { CompanyId: { editable: ...