JZOJ 5177. 【NOIP2017提高组模拟6.28】TRAVEL

Description

Description

Input

Input

Output

Output

Sample Input

4 4
1 2 1 10
2 4 3 5
1 3 1 5
2 4 2 7

Sample Output

6
2 3 4 5 6 7

Solution

  • 这题一眼望去有些难,特别是超级大的“泡泡怪”数量令人无从下手。

  • 不过只需稍微思考,就很容易发现其答案一定是连续的一段,因为通过的也是一段。

  • 而且进一步思考可以发现左右端点也会与虫洞限制重合,不然就会有扩展的更优解。

  • 于是考虑在 M 个虫洞中枚举其左右限制,共 O(M2) ,再 O(N) 扫一遍判断是否可行。

  • 但这样总时间复杂度就达到了 O(NM2) ,无法通过本题。

  • 所以我们可以先将那 M 个虫洞按 R (右端点)从小到大排一次序,二分查找即可。

  • 这样的总时间复杂度就是 O(NMlogM) ,AC本题。

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1001,M=N*6;
struct data
{
    int x,y;
}a[N*3],ans;
int n,m,tot;
int first[N],next[M],en[M],w1[M],w2[M];
bool pd;
bool bz[N];
inline int read()
{
    int X=0,w=1; char ch=0;
    while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}
    while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();
    return X*w;
}
inline void write(int x)
{
     if(x<0) putchar('-'),x=-x;
     if(x>9) write(x/10);
     putchar(x%10+'0');
}
inline bool cmp(data x,data y)
{
    return x.y<y.y;
}
inline void insert(int x,int y,int l,int r)
{
    next[++tot]=first[x];
    first[x]=tot;
    en[tot]=y;
    w1[tot]=l,w2[tot]=r;
}
inline void dfs(int x,int l,int r)
{
    if(pd) return;
    if(x==n)
    {
        ans.x=l,ans.y=r;
        pd=true;
        return;
    }
    for(int i=first[x];i;i=next[i])
        if(!bz[en[i]] && w1[i]<=l && r<=w2[i])
        {
            bz[en[i]]=true;
            dfs(en[i],l,r);
        }
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=m;i++)
    {
        int x=read(),y=read(),l=read(),r=read();
        a[i].x=l,a[i].y=r;
        insert(x,y,l,r);
        insert(y,x,l,r);
    }
    sort(a+1,a+1+m,cmp);
    for(int i=ans.x=1;i<=m;i++)
    {
        int l=1,r=m;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(a[i].x<=a[mid].y)
            {
                if(a[mid].y-a[i].x<ans.y-ans.x)
                {
                    l=mid+1;
                    continue;
                }
                if(a[mid].y-a[i].x==ans.y-ans.x && a[i].x>=ans.x)
                {
                    l=mid+1;
                    continue;
                }
                memset(bz,false,sizeof(bz));
                pd=false;
                dfs(bz[1]=1,a[i].x,a[mid].y);
                if(pd) l=mid+1; else r=mid-1;
            }else l=mid+1;
        }
    }
    write(ans.y-ans.x+1),putchar('\n');
    for(int i=ans.x;i<=ans.y;i++) write(i),putchar(' ');
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值