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 ...
随机推荐
- label_设置行距、字距及计算含有行间距的label高度
// // ViewController.m // CNBlogs // // Created by PXJ on 16/5/27. // Copyright © 2016年 PXJ. All ...
- Driving the Activity Lifecycle
Before Robolectric 2.2, most tests created Activities by calling constructors directly, (new MyActiv ...
- ssh登录很慢解决方法
使用ssh客户端(如:putty)连接Linux服务器,可能会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析问题 ...
- Shell - 特殊变量
$0 表示所执行程序的路径名. [huey@huey-K42JE ~]$ ll ~/bin total 4 -rwxrwxr-x 1 huey huey 21 Oct 24 14:39 hello [ ...
- C# - string 转为 DateTime(自定义)
上代码: string dt = " 1 11 1961"; DateTime day; System.Globalization.DateTimeFormatInfo dtFor ...
- power desinger 学习笔记<五>
怎样才能在修改表的字段Name的时候,Code不自动跟着变 tools-> General Options-> Dialog:Operation Modes: 去掉 NameToC ...
- jQuery 删除元素
通过 jQuery,可以很容易地删除已有的 HTML 元素. 删除元素/内容 如需删除元素和内容,一般可使用以下两个 jQuery 方法: remove() - 删除被选元素(及其子元素) empty ...
- Java线程(学习整理)--3--简单的死锁例子
1.线程死锁的概念: 简单地理解下吧! 我们都知道,线程在执行的过程中是占着CPU的资源的,当多个线程都需要一个被锁住的条件才能结束的时候,死锁就产生了! 还有一个经典的死锁现象: 经典的“哲学家就餐 ...
- servlet的doPost 和doGet和web文件结构
doPost和doGet分别由 tomcat自己来决定调用post 还是get 方式查询 get:url有少量的参数信息,一般用到查询那里 (像百度.. post一般用来提交大文件数据(二进制数据 d ...
- VS 编程常见错误及解决方法
1. VS2013 无法打开包括文件:“cv.h"等一些头文件 解决方法: cv.h是build\include文件夹下的头文件,所在文件夹位置是D:\Program Files (x86) ...