青岛区域赛 Finding Hotels 最小费用流概率建图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82975392

                         链接:https://www.nowcoder.com/acm/contest/207/K
                                                      来源:牛客网
 

                       时间限制:C/C++ 1秒,其他语言2秒
                    空间限制:C/C++ 262144K,其他语言524288K
                                     64bit IO Format: %lld

题目描述

There are N hotels all over the world. Each hotel has a location and a price. M guests want to find a hotel with an acceptable price and a minimum distance from their locations. The distances are measured in Euclidean metric. 

输入描述:

The first line is the number of test cases. For each test case, the first line contains two integers N (N ≤ 200000) and M (M ≤ 20000). Each of the following N lines describes a hotel with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the hotel, c is its price. It is guaranteed that each of the N hotels has distinct x, distinct y, and distinct c. Then each of the following M lines describes the query of a guest with 3 integers x (1 ≤ x ≤ N), y (1 ≤ y ≤ N) and c (1 ≤ c ≤ N), in which x and y are the coordinates of the guest, c is the maximum acceptable price of the guest.

输出描述:

For each guests query, output the hotel that the price is acceptable and is nearest to the guests location. If there are multiple hotels with acceptable prices and minimum distances, output the first one.

示例1

输入

复制

2
3 3
1 1 1
3 2 3
2 3 2
2 2 1
2 2 2
2 2 3
5 5
1 4 4
2 1 2
4 5 3
5 2 1
3 3 5
3 3 1
3 3 2
3 3 3
3 3 4
3 3 5

输出

复制

1 1 1
2 3 2
3 2 3
5 2 1
2 1 2
2 1 2
1 4 4
3 3 5

题解:  现场赛没打,打的是牛客的重现赛,但是这道题真的不难,建图就是普通的费用流建图,然后就是要解决概率怎么由相乘变成相加的问题,由于走一条路破坏整个图的概率为pi,所以总的破坏概率 为 1 - (1-pi)*(1-pi+1)*(1pi+2)........ 所以是让总的破坏概率最小,就是让 (1-pi)*(1-pi+1)*(1pi+2)........ 最大,记得以前做过一道概率最短路的,就是取对数就可以把乘法变成加法了,还可以解决精度问题。 所以直接取负对数,所以就是求 -log(1-pi)  - log(1-pi+1)-....最小,然后把边权设置为概率的负对数,然后跑一边费用就可以可,最后结果再还原成概率。  具体的建图请看代码~

p.s 由于自己输出没写\n 的锅 检查代码检查了两个小时,要怀疑人生了的感觉。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define inf 0x3f3f3f3f
 
#define Pi 4.0*atan(1.0)
#define Sqrt(x) (x)*(x)
 
#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
 
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 200000+10;
using namespace std;
inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int idx;
struct node{
    int f[3];
    int id; //标记多个最短距离取第一个输入的数据
    bool operator<(const node &t)const{
        return f[idx]<t.f[idx];
    }
}data[maxn],kd[maxn<<2];
int flag[maxn<<2];
pair<ll,node> res;
void build(int l,int r,int rt,int dept)
{
    if(l>r)return;
    flag[rt]=1;
    flag[rt<<1]=flag[rt<<1|1]=-1;
    idx=dept%2;
    int mid=(l+r)>>1;
    nth_element(data+l,data+mid,data+r+1);
    kd[rt]=data[mid];
    build(l,mid-1,rt<<1,dept+1);
    build(mid+1,r,rt<<1|1,dept+1);
}
inline ll getDis(int rt,node p) //求距离
{
    return (ll)Sqrt((ll)p.f[0]-kd[rt].f[0])+(ll)Sqrt((ll)p.f[1]-kd[rt].f[1]);
}
void query(node p,int rt,int dept)
{
    if(flag[rt]==-1)return;
    pair<ll,node> cur{getDis(rt,p),kd[rt]};
    int idm=dept%2;
    bool fg=false;
    int x=rt<<1;
    int y=rt<<1|1;
    if(p.f[idm]>=kd[rt].f[idm]){
        swap(x,y);
    }
    if(~flag[x]){
        query(p,x,dept+1);
    }
    if(res.fir==-1){ //取第一个
        if(cur.sec.f[2]<=p.f[2]){
            res.fir=cur.fir,res.sec=cur.sec;
        }
        fg=true;
    }
    else{
        if(cur.sec.f[2]<=p.f[2]&&(cur.fir<res.fir||(cur.fir==res.fir&&cur.sec.id<res.sec.id))){
            res.fir=cur.fir,res.sec=cur.sec;
        }
        if((ll)Sqrt(kd[rt].f[idm]-p.f[idm])<res.fir){
            fg=true;
        }
    }
    if(~flag[y]&&fg){
        query(p,y,dept+1);
    }
}
int main()
{
  // fin;
    int t,n,m;
    t=read();
    while(t--){
        n=read(),m=read();
        for(int i=0;i<n;++i){
            for(int j=0;j<3;++j){
                data[i].f[j]=read();
            }
            data[i].id=i;
        }
        build(0,n-1,1,0);
        while(m--){
            node p;
            for(int i=0;i<3;++i){
                p.f[i]=read();
            }
            res.fir=-1;
            query(p,1,0);
            printf("%d %d %d\n",res.sec.f[0],res.sec.f[1],res.sec.f[2]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82975392