COJ 2106 road
| road |
| 难度级别: A; 编程语言:不限;运行时间限制:1000ms; 运行空间限制:131072KB; 代码长度限制:102400B |
|
试题描述
|
|
某国有N个城市,这N个城市由M条双向道路连接。现在这些道路都很破旧,而政府希望开支尽量少,因此政府决定只改建这M条道路中的N-1条,这N-1条道路恰好能够构成一棵树。政府将公路分成两级:一级公路和二级公路。修建一级公路需要的费用较高,但是一级公路上的车速较快。因此政府要求在这N-1条公路中至少修建K条一级公路,其余的修建二级公路。政府希望修建费用最大的那条公路的修建费用尽量小,请帮助政府解决这个问题。 |
|
输入
|
|
第一行,三个整数N、K、M。
接下来的M行,每行四个整数a、b、c1、c2,表示在a和b之间原来有一条道路,将这条道路改建成一级公路需要的费用为c1,改建成二级公路的费用为c2。 |
|
输出
|
|
一行,一个整数,表示修建费用最大的那条公路的修建费用的最小值。
|
|
输入示例
|
|
10 4 20
3 9 6 3 1 3 4 1 5 3 10 2 8 9 8 7 6 8 8 3 7 1 3 2 4 9 9 5 10 8 9 1 2 6 9 1 6 7 9 8 2 6 2 1 3 8 9 5 3 2 9 6 1 6 10 3 5 6 3 1 2 7 6 1 7 8 6 2 10 9 2 1 7 1 10 2 |
|
输出示例
|
|
5
|
|
其他说明
|
|
对于20%的数据,1<=N<=100,1<=M<=150;
对于40%的数据,1<=N<=2000,1<=M<=5000; 对于全部的数据,1<=N<=10000,1<=M<=20000,1<=c2<=c1<=30000。 |
题解:在最小生成树的基础上先满足k个1,再补其他的2
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=+,maxm=+,inf=-1u>>;
struct edge{int x,y,w;}e1[maxm],e2[maxm];
bool operator<(const edge&a,const edge&b){return a.w<b.w;}
int n,m,k,fa[maxn];
int findset(int x){return x==fa[x]?x:fa[x]=findset(fa[x]);}
bool check(int lim){
int num=,tot=n;for(int i=;i<=n;i++)fa[i]=i;
for(int i=;i<m;i++){
if(e1[i].w>lim)break;
int f1=findset(e1[i].x),f2=findset(e1[i].y);
if(f1!=f2)fa[f1]=f2,num++,tot--;
}if(num<k)return false;
for(int i=;i<m;i++){
if(e2[i].w>lim)break;
int f1=findset(e2[i].x),f2=findset(e2[i].y);
if(f1!=f2)fa[f1]=f2,tot--;
}return tot==?true:false;
}
inline int read(){
int x=,sig=;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=;
for(;isdigit(ch);ch=getchar())x=*x+ch-'';
return sig?x:-x;
}
inline void write(int x){
if(x==){putchar('');return;}if(x<)putchar('-'),x=-x;
int len=,buf[];while(x)buf[len++]=x%,x/=;
for(int i=len-;i>=;i--)putchar(buf[i]+'');return;
}
void init(){
n=read();k=read();m=read();int x,y;
for(int i=;i<m;i++)x=read(),y=read(),e1[i]=(edge){x,y,read()},e2[i]=(edge){x,y,read()};
sort(e1,e1+m);sort(e2,e2+m);
int L=,R=,M;
while(L<R){
M=L+R>>;if(check(M))R=M;else L=M+;
}write(L);
return;
}
void work(){
return;
}
void print(){
return;
}
int main(){init();work();print();return ;}
COJ 2106 road的更多相关文章
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- Codeforces #380 div2 C(729C) Road to Cinema
C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- 三分 --- CSU 1548: Design road
Design road Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...
- hdu 5861 Road 两棵线段树
传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...
- HDU4081 Qin Shi Huang's National Road System(次小生成树)
枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...
- 杭电 1595 find the safest road
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- 首页在linux下的哪个文件夹
/data/mindo/tomcat-live/webapps/ROOT/WEB-INF/templates/default/pages/index.jsp svn检出地址 https://sif ...
- Java基础知识强化之集合框架笔记35:List练习之产生10个1~20之间的随机数(要求:随机数不能重复)
1. 需求:获取10个1-20之间的随机数,要求不能重复 用数组实现,但是数组的长度是固定的,长度不好确定.所以我们使用集合实现. 分析: • 创建产生随机数的对象 • 创建一个存储随机数的集合. • ...
- .NET中TextBox控件设置ReadOnly=true后台取不到值三种解决方法
当TextBox设置了ReadOnly=true后要是在前台为控件添加了值,后台是取不到的,值为空,多么郁闷的一个问题经过尝试,发现可以通过如下的方式解决这个问题.感兴趣的朋友可以了解下 当TextB ...
- 关于封装的一个小问题和TA的例子
写个小例子吧 -- 很多细节(如校验.判断等等)都略了 其实不是有意写成这样,而是很多朋友都这么写(当然里面也有点夸张的写法) 这么写其实也没什么不好,简单明了,不用动脑子,一看就很直白, 但是如果 ...
- c#迭代算法
//用迭代算法算出第m个值 //1,1,2,3,5,8...; //{1,0+1,1+1,1+2,2+3 ,3+5} static void Main(string[] arg ...
- Getting Started with Testing ——开始单元测试
Android tests are based on JUnit, and you can run them either as local unit tests on the JVM or as i ...
- springMVC -- 整合UEditor(富文本编辑器)
工作中需要用到UEditor编辑文本,在与springMVC进行整合时,出现了一些问题,结果导致,在进行图片上传时出现如下提示: 上网查询了很多相关资料,此处简要记录下,防止以后遇到类似问题. 一种方 ...
- 【转】iOS-Core-Animation-Advanced-Techniques(二)
原文: http://www.cocoachina.com/ios/20150104/10816.html 视觉效果和变换 (四)视觉效果 嗯,园和椭圆还不错,但如果是带圆角的矩形呢? 我们现在能做到 ...
- basicAnimation移动图形
目的:采用CABasicAnimation 点击屏幕上的点来是实现图像的位置移动 并且位置能够不反弹 难点:1 通过动画的KeyPath找到layer的属性 2 通过NSValue将点包装成对象 ...
- Tomcat 8熵池阻塞变慢详解(转)
Tomcat 8熵池阻塞变慢详解 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: ...