[ZJOJ] 5794 2018.08.10【2018提高组】模拟A组&省选 旅行
Description
Input
Output
Sample Input
Input1:
4 4
1 2 1 10
2 4 3 5
1 3 1 5
2 4 2 7
Input2:
2 2
1 2 1 3
1 2 4 6
Sample Output
Output1:
6
2 3 4 5 6 7
Output2:
3
1 2 3
Data Constraint
100%的数据 2 <= N <= 1000, 0 <= M <= 3000, 1 <= a, b <= N, 1 <= l <= r <= 10^6
题目解析
数据范围不算大,看了看题首先可以得出一个推论:答案区间一定是连续的。
所以我们只要知道区间的左右段就可以了,枚举一下就很好做了。
用并查集 + 贪心的思想,将所有边按右端点的限制大小排序,之后贪心的合并点,同时选取边,当点1和点n在同一集合内,就结束枚举,过程中记录答案。
其实有点像Kruskal的过程。
Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std; //1<=a , b<=N , 1<=l<=r<=1e6
const int MAXN = ;
const int MAXM = ; int n,m;
int ans,cnt,lft;
int fa[MAXN];
struct Edge {
int from,to;
int L,R;
} l[MAXM << ]; bool cmp(Edge x,Edge y) {
return x.R > y.R;
} int _find(int x) {
if(x == fa[x]) return x;
return fa[x] = _find(fa[x]);
} int _min(int x,int y) {
return x < y ? x : y;
} int _max(int x,int y) {
return x > y ? x : y;
} inline void add(int x,int y,int a,int b) {
cnt++;
l[cnt].from = x;
l[cnt].to = y;
l[cnt].L = a;
l[cnt].R = b;
return;
} inline void init_fa() {
for(int i = ;i <= n;i++) fa[i] = i;
return;
} inline void Dinic(int s,int t) {
for(int i = ;i <= m;i++) {
init_fa();
for(int j = ;j <= m;j++) {
if(l[j].L > l[i].L) continue;
fa[_find(l[j].from)] = _find(l[j].to);
if (_find()==_find(n)) {
if(l[j].R - l[i].L > ans - ) {
lft = l[i].L;
ans = l[j].R - l[i].L + ;
}
break;
}
}
}
return;
} int main() {
scanf("%d%d",&n,&m);
int x,y,a,b;
for(int i = ;i <= m;i++) {
scanf("%d%d%d%d",&x,&y,&a,&b);
add(x,y,a,b);
}
for(int i = ;i <= n;i++ ){
fa[x] = x;
}
sort(l+,l++m,cmp);
Dinic(,n);
printf("%d\n",ans);
for(int i = lft;i<=lft + ans - ;i++) {
printf("%d ",i);
}
return ;
}
[ZJOJ] 5794 2018.08.10【2018提高组】模拟A组&省选 旅行的更多相关文章
- 新手C#SQL Server使用记录2018.08.10
主键(PrimaryKey):主键就是每个数据行(记录)的唯一标识,不会有重复值的列(字段)才能当做主键.一个表可以没有主键,但是这样会很难处理表,因此一般情况表都要设置主键. 主键有两张选用策略,分 ...
- 2018.08.10【省赛&提高A组模拟】比赛总结
题解 这次题目可真是太难了! 糟糕的运气使我AK的步伐寸步难行(士气严重下降). T1 这题还是比较水的(尽管我比赛时只拿了50分) 一些大佬们说:这题只是一道简单的暴力题,枚举l+二分r 就可以了. ...
- [jzoj 5770]【2018提高组模拟A组8.6】可爱精灵宝贝 (区间dp)
传送门 Description Branimirko是一个对可爱精灵宝贝十分痴迷的玩家.最近,他闲得没事组织了一场捉精灵的游戏.游戏在一条街道上举行,街道上一侧有一排房子,从左到右房子标号由1到n. ...
- JZOJ5857 【NOIP提高组模拟A组2018.9.8】没有上司的舞会
题目 Description "那么真的有果尔德施坦因这样一个人?"他问道. "是啊,有这样一个人,他还活着.至于在哪里,我就不知道了." "那么那个 ...
- 2018.08.10 atcoder No Need(线性dp)
传送门 输入一个序列an" role="presentation" style="position: relative;">anan,输入k&q ...
- 2018.08.10 atcoder Median Sum(01背包)
传送门 题意简述:输入一个数组an" role="presentation" style="position: relative;">anan. ...
- Cheatsheet: 2018 08.01 ~ 2018 10.31
Other Building the Ultimate Developer PC 3.0 - The Parts List for my new computer, IronHeart Face re ...
- 【JZOJ4790】【NOIP2016提高A组模拟9.21】选数问题
题目描述 在麦克雷的面前有N个数,以及一个R*C的矩阵.现在他的任务是从N个数中取出R*C个,并填入这个矩阵中.矩阵每一行的法值为本行最大值与最小值的差,而整个矩阵的法值为每一行的法值的最大值.现在, ...
- 2018.12.08【NOIP提高组】模拟B组总结(未完成)
2018.12.08[NOIP提高组]模拟B组总结 diyiti 保留道路 进化序列 B diyiti Description 给定n 根直的木棍,要从中选出6 根木棍,满足:能用这6 根木棍拼出一个 ...
随机推荐
- linux /proc/stat 文件说明
/proc/stat 文件内容 # cat /proc/stat cpu 1411 1322 3070 1193539 2790 0 268 0 0 0 cpu0 472 658 787 297933 ...
- LeetCode 349. Intersection of Two Arrays (两个数组的相交)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- MySQL create table as与create table like对照
在MySQL数据库中,关于表的克隆有多种方式,比方我们能够使用create table ..as .. .也能够使用create table .. like ..方式. 然而这2种不同的方 ...
- Python类私有方法的陷阱
引言 Python不像C++.Java.C#等有明白的公共.私有或受保护的keyword来定义成员函数或属性,它使用约定的单下划线"_"和"__"双下划线作为函 ...
- install yael on the ubuntu 12.04
1. bits/predefs.h no such file or directory ??? sudo apt-get install gcc-multilib 2. sudo gedit /et ...
- windowActionModeOverlay
windowActionModeOverlay: android:windowActionModeOverlay=“true|false” : actionmode 弹出时覆盖部分布局 若 ...
- openstack 杂记 备忘
- redhat6 改 centos yum 源
**redhat的yum在线更新是收费的,如果没有注册的话不能使用,如果要使用,需将redhat的yum卸载后,重启安装其他yum源,再配置其他源.** 本文包括配置本地源及第三方源.第三方源包括:网 ...
- Rails mysql数据库连接的小坑
基本上直接clone下来的话,数据库连接必失败. 注意,把用户名密码写在.env文件下
- CSS样式适配杂记
1.问:input框的对齐,制作类似百度搜索框的时候,发现IE下前面输入框和后面按钮总是不能对齐. 解答:给input框增加vertical-align:bottom; 2.问:IE下display: ...