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

  1. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  2. POJ 3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  3. 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 ...

  4. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  5. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  6. 三分 --- CSU 1548: Design road

    Design road Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...

  7. hdu 5861 Road 两棵线段树

    传送门:hdu 5861 Road 题意: 水平线上n个村子间有 n-1 条路. 每条路开放一天的价格为 Wi 有 m 天的操作,每天需要用到村子 Ai~Bi 间的道路 每条路只能开放或关闭一次. ( ...

  8. HDU4081 Qin Shi Huang's National Road System(次小生成树)

    枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...

  9. 杭电 1595 find the safest road

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. Call Directory Extension 初探

    推荐序 本文介绍了 iOS 10 中的 Call Directory Extension 特性,并且最终 Demo 出一个来电黑名单的 App. 作者:余龙泽,哈工大软件工程大四学生,之前在美图公司实 ...

  2. TCP与UDP区别

    原文链接:http://blog.sina.com.cn/s/blog_493309600100clrw.html TCP与UDP区别 TCP---传输控制协议,提供的是面向连接.可靠的字节流服务.当 ...

  3. Java环境的安装与配置

    Java环境的安装与配置 环境:Java8,win10 推荐oracle官网oracle官网https://www.oracle.com/index.html下载JDK进行安装 选择自己需要的版本下载 ...

  4. IOS 中得runloop 详细解释

    1.Runloop基础知识- 1.1 字面意思 a 运行循环 b 跑圈 - 1.2 基本作用(作用重大) a 保持程序的持续运行(ios程序为什么能一直活着不会死) b 处理app中的各种事件(比如触 ...

  5. (whh仅供自己参考)进行ip网络请求的步骤

    这个过程大致是这个样子: 1 添加通知 2 发送网络请求 里边有一个发送通知的操作 3 执行发送通知的具体操作 代码如下: 1 在VC添加通知 [[NSNotificationCenter defau ...

  6. 文字排版--字号、颜色(font-size, color)

    可以使用下面代码设置网页中文字的字号为12像素,并把字体颜色设置为#666(灰色): body{font-size:12px;color:#666} 示例: <!DOCTYPE HTML> ...

  7. sql server主动推送客户端更新数据

    小谈需求: 最近工作上接到一个需求,做一个web展示数据的报表,最好能实时更新,不限制所用技术. 第一个问题:web服务器推送给浏览器新数据,一开始我想到的最快的最简单的方法就是 在web页面上js轮 ...

  8. pthread_rwlock_t读写锁函数说明

    读写锁 索引: 初始化一个读写锁pthread_rwlock_init 读锁定读写锁      pthread_rwlock_rdlock 非阻塞读锁定 pthread_rwlock_tryrdloc ...

  9. oc 怎么接收NSSting字符的方法

    ]; //使用一个缓冲区 NSLog(@"请输入一个字符串:"); scanf("%s",buffer); NSString * str = [NSString ...

  10. CSS3 :nth-child() 选择器

    CSS3 :nth-child() 选择器 代码: <!DOCTYPE html> <html> <head> <style> p:nth-child( ...