bzoj 5140 [Usaco2017 Dec]A Pie for a Pie

两只牛通过交换不同评分的水果派进行礼尚往来游戏,目标是最少轮次愉快结束。采用最短路径算法逆向思考,从评分0的派出发,利用双向队列与集合寻找可能的派送路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/archives/2917
Description

Bessie and Elsie have each baked N pies (1≤N≤10^5). Each of the 2N pies has a tastiness value acco
rding to Bessie and a (possibly different) tastiness value according to Elsie.Bessie is thinking abo
ut giving one of her pies to Elsie. If Elsie receives a pie from Bessie, she will feel obligated to
give one of her pies to Bessie. So as to not appear stingy nor flamboyant, Elsie will try to pick a
pie that is at least as tasty (in Elsie’s eyes) as the pie she received, but no more than D units ta
stier (0≤D≤10^9). Such a pie may not exist, in which case Elsie will adopt a pseudonym and exile h
erself to Japan.But if Elsie does give Bessie a pie in return, Bessie will similarly try to give Els
ie a pie which is at least as tasty but no more than DD units tastier (in Bessie’s eyes) as the pie
Elsie just gave her. Should this be impossible, Bessie too will exile herself. Otherwise she will gi
ve her chosen pie to Elsie. This cycle will continue until one of the cows is exiled, an unhappy out
come, or one of the cows receives a pie which she accords a tastiness value of 0, in which case the
gift exchange will end and both cows will be happy.Note that a pie may not be gifted twice, nor can
either cow return a pie gifted to her.For each of the NN pies Bessie could select as her initial gif
t to Elsie, determine the minimum number of pies that could possibly be gifted in the resulting exch
ange before the cows are happy.
两只牛,牛A和牛B,他们对水果派的品味不同,每个人都有n<=1e5个派,每个派都有两种得分a[i], b[i],其中a[
i]是牛A对该派的评分,b[i]是牛B的评分, 0 <= a[i], b[i] <= 1e9。两只牛开始礼尚往来,从牛A开始,它选一
个pie给牛B,设这个pie的评分是(a[i], b[i])。此时牛B为了不失礼节(不太小气,也不要太谄媚)要选出一个B
评分在[b[i], b[i]+d]范围内的pie(a[j], b[j]),给A。同理,A又要选出一个A评分在[a[j], a[j]+d]范围内的pi
e给B,如此往复。每个pie都只能传递一次。如果A收到了一个A评分为0的pie,或者B收到了一个B评分为0的pie,
那么礼尚往来就愉快的结束了。否则,就不愉快结束(即一个人无法选出合法的pie给对方时)。下面让你输出,
对于A的每一个pie,如果第一轮A把这个pie交给B,礼尚往来最少可以几轮愉快结束?如果无法愉快结束,就输出-1

Input

The first line contains the two integers N and D.
The next 2N lines contain two space-separated integers each,
respectively denoting the value of a particular pie according to Bessie, and the value of that pie according to Elsie.
The first N lines refer to Bessie’s pies, and the remaining N lines refer to Elsie’s pies.
It is guaranteed that all tastiness values are in the range [0,10^9]

Output

There should be N lines in the output.
Line i should contain a single integer: the minimum number of pies that could be gifted in a happy gift
exchange started with Bessie’s pie i.
If no gift exchange starting with pie i is happy, then line i should contain the single integer -1 instead.

Sample Input

2 1
1 1
5 0
4 2
1 4
Sample Output

3
1
HINT

Source

Gold

可以看出这是一个最短路问题 那么不妨从每一个终止状态逆推回去

那么每一个派相当于一个点 然后从任意一个=0的地方往回bfs 如何bfs 这个边相当于假设我现在在x节点 那么y节点 代表我收到了来自另一个人的y 我现在要回赠他x

注意这个makepair里第二关键字给0 否则有些相同权值的他会找不到..

#include<set>
#include<queue>
#include<cstdio>
#include<cctype>
#include<cstring>
#define pa pair<int,int>
#define N 200020
#include<algorithm>
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S) {T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=gc();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=gc();
    return x*f;
}
const int OUT_LEN=1<<16;
char obuf[OUT_LEN],*oh=obuf;
inline void write_char(char c){
    if (oh==obuf+OUT_LEN) fwrite(obuf,1,OUT_LEN,stdout),oh=obuf;
    *oh++=c;
}
template<class T>
inline void W(T x){
    static int buf[30],cnt;
    if (!x) write_char('0');else{
        if (x<0) write_char('-'),x=-x;
        for (cnt=0;x;x/=10) buf[++cnt]=x%10+48;
        while(cnt) write_char(buf[cnt--]);
    }
}
inline void flush(){fwrite(obuf,1,oh-obuf,stdout);}
set<pa>::iterator it;
set<pa>s1,s2;queue<int> q;
int a[N],b[N],n,d,dis[N];
int main(){
    freopen("bzoj5140.in","r",stdin);
    n=read();d=read();memset(dis,-1,sizeof(dis));
    //s1.insert(make_pair(3,1));s1.insert(make_pair(3,3));
//  printf("%d\n",s1.size());
    for (int i=1;i<=n<<1;++i) a[i]=-read(),b[i]=-read();
    for (int i=1;i<=n;++i){
        if (!b[i]) dis[i]=1,q.push(i);
        else s1.insert(make_pair(b[i],i));
        if (!a[n+i]) dis[n+i]=1,q.push(n+i);
        else s2.insert(make_pair(a[n+i],n+i));
    }
    while(!q.empty()){
        int x=q.front();q.pop();
        if (x<=n){
            while(1){
                it=s2.lower_bound(make_pair(a[x],0));
                if (it==s2.end()||it->first>a[x]+d) break;
                int y=it->second;dis[y]=dis[x]+1;s2.erase(it);q.push(y);
            }
        }else{
            while(1){
                it=s1.lower_bound(make_pair(b[x],0));
                if (it==s1.end()||it->first>b[x]+d) break;
                int y=it->second;dis[y]=dis[x]+1;s1.erase(it);q.push(y);
            }
        }
    }for (int i=1;i<=n;++i) W(dis[i]),write_char('\n');flush();
    return 0;
}
题目描述 牛牛和她的朋友们正在玩一个有趣的游戏,他们需要构建一个有 $n$ 个节点的无向图,每个节点都有一个唯一的编号并且编号从 $1$ 到 $n$。他们需要从节点 $1$ 到节点 $n$ 找到一条最短路径,其中路径长度是经过的边权的和。为了让游戏更有趣,他们决定在图上添加一些额外的边,这些边的权值都是 $x$。他们想知道,如果他们添加的边数尽可能少,最短路径的长度最多会增加多少。 输入格式 第一行包含两个正整数 $n$ 和 $m$,表示节点数和边数。 接下来 $m$ 行,每行包含三个整数 $u_i,v_i,w_i$,表示一条无向边 $(u_i,v_i)$,权值为 $w_i$。 输出格式 输出一个整数,表示最短路径的长度最多会增加多少。 数据范围 $2 \leq n \leq 200$ $1 \leq m \leq n(n-1)/2$ $1 \leq w_i \leq 10^6$ 输入样例 #1: 4 4 1 2 2 2 3 3 3 4 4 4 1 5 输出样例 #1: 5 输入样例 #2: 4 3 1 2 1 2 3 2 3 4 3 输出样例 #2: 2 算法 (BFS+最短路) $O(n^3)$ 我们把问题转化一下,假设原图中没有添加边,所求的就是点 $1$ 到点 $n$ 的最短路,并且我们已经求出了这个最短路的长度 $dis$。 接下来我们从小到大枚举边权 $x$,每次将 $x$ 加入图中,然后再次求解点 $1$ 到点 $n$ 的最短路 $dis'$,那么增加的最短路长度就是 $dis'-dis$。 我们发现,每次加入一个边都需要重新求解最短路。如果我们使用 Dijkstra 算法的话,每次加入一条边需要 $O(m\log m)$ 的时间复杂度,总的时间复杂度就是 $O(m^2\log m)$,无法通过本题。因此我们需要使用更优秀的算法。 观察到 $n$ 的范围比较小,我们可以考虑使用 BFS 求解最短路。如果边权均为 $1$,那么 BFS 可以在 $O(m)$ 的时间复杂度内求解最短路。那么如果我们只是加入了一条边的话,我们可以将边权为 $x$ 的边看做 $x$ 条边的组合,每次加入该边时,我们就在原始图上添加 $x$ 条边,边权均为 $1$。这样,我们就可以使用一次 BFS 求解最短路了。 但是,我们不得不考虑加入多条边的情况。如果我们还是将边权为 $x$ 的边看做 $x$ 条边的组合,那么我们就需要加入 $x$ 条边,而不是一条边。这样,我们就不能使用 BFS 了。 但是,我们可以使用 Floyd 算法。事实上,我们每次加入边时,只有边权等于 $x$ 的边会发生变化。因此,如果我们枚举边权 $x$ 时,每次只需要将边权等于 $x$ 的边加入图中,然后使用 Floyd 算法重新计算最短路即可。由于 Floyd 算法的时间复杂度为 $O(n^3)$,因此总的时间复杂度为 $O(n^4)$。 时间复杂度 $O(n^4)$ 空间复杂度 $O(n^2)$ C++ 代码 注意点:Floyd算法计算任意两点之间的最短路径,只需要在之前的路径基础上加入新的边构成的新路径进行更新即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值