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.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...
随机推荐
- 计算型属性 vs 懒加载
只实现 getter 方法的属性被称为计算型属性,等同于 OC 中的 ReadOnly 属性 计算型属性本身不占用内存空间 不可以给计算型属性设置数值 计算型属性可以使用以下代码简写 var titl ...
- CAD绘制自定义实体(com接口)
在cad使用过程中,用户可以绘制自定义实体.点击此处下载演示实例. 调用DrawCustomEntity函数,绘制一个自定义实体对象. 下面代码绘制一个自定义实体,C#代码实现如下: private ...
- java_IO_2
1.字节流 InputStream(抽象类) package ioStudy; import java.io.File; import java.io.FileInputStream; import ...
- 数据结构与算法(3)- C++ STL与java se中的vector
声明:虽然本系列博客与具体的编程语言无关.但是本文作者对c++相对比较熟悉,其次是java,所以难免会有视角上的偏差.举例也大多是和这两门语言相关. 上一篇博客概念性的介绍了vector,我们有了大致 ...
- Luogu P3797 妖梦斩木棒
解题思路 用线段树做这个就不用说了吧,但是要维护的东西确实很神奇.在每一个节点上都维护一个$lbkt$,表示这个区间上最靠左的右括号的位置:一个$rbkt$,表示这个区间上最靠右的左括号的位置.还有一 ...
- POJ3616 Milking Time【dp】
Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her producti ...
- 题解 NOI2018 归程
题解 NOI2018 归程 题意 本题的故事发生在魔力之都,在这里我们将为你介绍一些必要的设定. 魔力之都可以抽象成一个 n 个节点.m 条边的无向连通图(节点的编号从 1 至 n).我们依次用 l, ...
- Linux:DNS主、从、缓存服务器配置、DNS同步加密TSIG配置、DNS分离解析配置
DNS主服务器配置(正向解析.反向解析) 正向解析:根据主机名查找对应的IP地址.当用户访问一个域名时(不考虑hosts文件等因素),正常情况会向指定的DNS主机发送递归查询请求反向解析:根据IP地址 ...
- shell日志颜色处理
记录一下shell日志颜色处理 _COLORS=${BS_COLORS:-$(tput colors >/dev/)} __detect_color_support() { # shellche ...
- buf.writeUInt16BE()
buf.writeUInt16BE(value, offset[, noAssert]) buf.writeUInt16LE(value, offset[, noAssert]) value {Num ...