[USACO13OPEN]照片Photo 解题报告

  • Post author:
  • Post category:其他




[USACO13OPEN]照片Photo 解题报告



题目描述

Farmer John has decided to assemble a panoramic photo of a lineup of his N cows (1 <= N <= 200,000), which, as always, are conveniently numbered from 1…N. Accordingly, he snapped M (1 <= M <= 100,000) photos, each covering a contiguous range of cows: photo i contains cows a_i through b_i inclusive. The photos collectively may not necessarily cover every single cow.

After taking his photos, FJ notices a very interesting phenomenon: each photo he took contains exactly one cow with spots! FJ was aware that he had some number of spotted cows in his herd, but he had never actually counted them. Based on his photos, please determine the maximum possible number of spotted cows that could exist in his herd. Output -1 if there is no possible assignment of spots to cows consistent with FJ’s photographic results.

农夫约翰决定给站在一条线上的N(1 <= N <= 200,000)头奶牛制作一张全家福照片,N头奶牛编号1到N。

于是约翰拍摄了M(1 <= M <= 100,000)张照片,每张照片都覆盖了连续一段奶牛:第i张照片中包含了编号a_i 到 b_i的奶牛。但是这些照片不一定把每一只奶牛都拍了进去。

在拍完照片后,约翰发现了一个有趣的事情:每张照片中都有且仅有一只身上带有斑点的奶牛。约翰意识到他的牛群中有一些斑点奶牛,但他从来没有统计过它们的数量。 根据照片,请你帮约翰估算在他的牛群中最多可能有多少只斑点奶牛。如果无解,输出“-1”。



输入输出格式



输入格式:

  • Line 1: Two integers N and M.

  • Lines 2…M+1: Line i+1 contains a_i and b_i.



输出格式:

  • Line 1: The maximum possible number of spotted cows on FJ’s farm, or -1 if there is no possible solution.

输入输出样例:



inoput

5 3

1 4

2 5

3 4



output

3



思路:

差分约束!!!!!!

根据输入数据 d[b] – d[a – 1] = 1(可转化成两个不等式关系),d[i] – d[i – 1] <= 1,d[i – 1] – d[i] <= 0 从而建图跑最短路。

(注意卡时间,如果到了一定时间还没跑完直接输出-1!)

code:

#include<bits/stdc++.h>
using namespace std;

const int maxn=200005,maxm=100005,INF=0x3f3f3f3f;
int n,m;
struct Edge
{
    int from,to,dis;
}e[maxn+maxm<<1];
int head[maxn],cnt;
int q[maxn],d[maxn],vis[maxn],top;

bool cmp(int p,int q)
{
    return d[p] > d[q];
}

inline void push(int x)
{
    q[top++]=x;
    push_heap(q,q+top,cmp);
}

inline void pop()
{
    pop_heap(q,q+top--,cmp);
}

inline int Top()
{
    return *q;
}

inline void add(int u,int v,int w)
{
    e[++cnt].from=head[u];
    e[cnt].to=v;
    e[cnt].dis=w;
    head[u]=cnt;
}

inline bool empty()
{
    return top==0;
}

inline int spfa()
{
    vis[0]=1;
    push(0);
    for(int i=1;i<=n;i++)
        d[i]=INF;
    int num=0;
    while(!empty()){
        int u=Top();pop();vis[u]=0;
        for(int i=head[u];i;i=e[i].from){
            int y=e[i].to;
            if(d[y] > d[u]+e[i].dis){
                d[y]=d[u]+e[i].dis;
                if(!vis[y]){
                    if(++num >= 800000) return -1;
                    push(y);
                    vis[y]=1;
                }
            }
        }
    }
    return d[n];
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1,a,b;i<=m;i++){
        scanf("%d%d",&a,&b);
        add(a-1,b,1);
        add(b,a-1,-1);
    }
    for(int i=1;i<=n;i++){
        add(i-1,i,1);
        add(i,i-1,0);
    }
    printf("%d",spfa());
    return 0;
}



版权声明:本文为SGDBS233原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。