Time limit : 2sec / Memory limit : 256MB

Score : 500 points

Problem Statement

On an xy plane, in an area satisfying 0≤xW,0≤yH, 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≦iW−1)
  • 1≦qj≦108(0≦jH−1)
  • pi(0≦iW−1) is an integer.
  • qj(0≦jH−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

Copy
2 2
3
5
2
7

Sample Output 1

Copy
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

Copy
4 3
2
4
8
1
2
9
3

Sample Output 2

Copy
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的更多相关文章

  1. POJ1679 The Unique MST[次小生成树]

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28673   Accepted: 10239 ...

  2. 基于MST的立体匹配及相关改进(A Non-Local Cost Aggregation Method for Stereo Matching)

    怀着很纠结的心情来总结这篇论文,这主要是因为作者提虽然供了源代码,但是我并没有仔细去深究他的code,只是把他的算法加进了自己的项目.希望以后有时间能把MST这一结构自己编程实现!! 论文题目是基于非 ...

  3. BZOJ 2654 & 玄学二分+MST

    题意: 给一张图,边带权且带颜色黑白,求出一棵至少包含k条白边的MST SOL: 正常人都想优先加黑边或者是白边,我也是这么想的...你看先用白边搞一棵k条边的MST...然后维护比较黑边跟白边像堆一 ...

  4. LA 5713 秦始皇修路 MST

    题目链接:http://vjudge.net/contest/144221#problem/A 题意: 秦朝有n个城市,需要修建一些道路使得任意两个城市之间都可以连通.道士徐福声称他可以用法术修路,不 ...

  5. [poj1679]The Unique MST(最小生成树)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28207   Accepted: 10073 ...

  6. [BZOJ2654]tree(二分+MST)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2654 分析:此题很奇葩,我们可以给所有白边加上一个权值mid,那么在求得的MST中白边 ...

  7. CodeForces 125E MST Company

    E. MST Company time limit per test 8 seconds memory limit per test 256 megabytes input standard inpu ...

  8. 2015baidu复赛2 连接的管道(mst && 优先队列prim)

    连接的管道 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. ACM/ICPC 之 判别MST唯一性-Kruskal解法(POJ1679)

    判别MST是否唯一的例题. POJ1679-The Unique MST 题意:给定图,求MST(最小生成树)是否唯一,唯一输出路径长,否则输出Not Unique! 题解:MST是否唯一取决于是否有 ...

  10. hdu 4756 MST+树形dp ****

    题意:给你n(n = 1000)个二维点,第一个点是power plant,还有n - 1个点是dormitories.然后现在知道有一条寝室到寝室的边是不能连的,但是我们不知道是哪条边,问这种情况下 ...

随机推荐

  1. Spring框架系列(七)--Spring常用注解

    Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...

  2. HDU多校Round 3

    Solved:4 rank:268 C. Dynamic Graph Matching  状压DP一下 #include <stdio.h> #include <algorithm& ...

  3. layer iframe层弹出图片

    这个js是在一个layer iframe弹出层中,点击按钮弹出图片

  4. 前端安全 xss

    整体的 XSS 防范是非常复杂和繁琐的,不仅需要在全部需要转义的位置,对数据进行对应的转义.而且要防止多余和错误的转义,避免正常的用户输入出现乱码. 虽然很难通过技术手段完全避免 XSS,但可以总结以 ...

  5. HDU - 5952 Counting Cliques(dfs搜索)

    题目: A clique is a complete graph, in which there is an edge between every pair of the vertices. Give ...

  6. Linux命令学习(3): zcat 直接查看压缩文件

    版权声明:本文为博主原创文章,未经允许不得转载. zcat 用途:在不解压文件的情况下,直接将文件内容输出到标准输出.(原压缩文件不做任何更改) 格式:zcat [-n] [-V] [FILE] 参数 ...

  7. 782B The Meeting Place Cannot Be Changed(二分)

    链接:http://codeforces.com/problemset/problem/782/B 题意: N个点,需要找到一个点使得每个点到这个点耗时最小,每个点都同时开始,且都拥有自己的速度 题解 ...

  8. node-sass 安装失败

    安装 npm install 时偶尔遇到报错:没有安装python或node-sass 安装失败的问题,百度之后发现是被墙了,但根据百度的方法换了淘宝镜像和用了vpn都安装失败, 原因可能是没有卸载之 ...

  9. KMP超强模板贴一份

    )== ) {         );         next[]=; ;         ;i<=n;i++) {             ]!=str[i]) j=next[j];      ...

  10. 1827 tarjan+缩点

    #include<stdio.h> #include<stack> #include<iostream> #include<string.h> #inc ...