題目鏈接:點(diǎn)擊查看
題目大意:將數(shù)軸視為跑道,現(xiàn)在有數(shù)個(gè)學(xué)生正在跑步,每個(gè)學(xué)生都有四個(gè)相關(guān)屬性:
起始時(shí)間 停止時(shí)間 起始位置 方向(正向,反向)
現(xiàn)在給出 n 處監(jiān)控,每處監(jiān)控在第 t 秒第 x 個(gè)位置拍到了至少一個(gè)學(xué)生,問(wèn)最少需要多少個(gè)學(xué)生進(jìn)行跑步,才能滿(mǎn)足 n 處監(jiān)控的條件
題目分析:因?yàn)榕艿赖姆秶?1 ~ 1e9 ,所以貪心可以得出,因?yàn)樾枰尡M量少的學(xué)生進(jìn)行跑步,所以每個(gè)學(xué)生盡量跑更多的路,可以使得被更多的監(jiān)控拍到,這樣我們不妨直接設(shè)所有跑步的學(xué)生為兩類(lèi),一類(lèi)是從點(diǎn) 0 出發(fā)正向跑,另一類(lèi)是從點(diǎn) 1e9 出發(fā)倒著跑
那么對(duì)于監(jiān)控中拍到的人,因?yàn)楸O(jiān)控是在 ( t , x ) 的條件下拍到的,換句話(huà)說(shuō),這個(gè)人,可以是第 t - x 秒從點(diǎn) 0 出發(fā)正著跑的那個(gè)人,也可以是第 t - ( 1e9 - x ) 秒從點(diǎn) 1e9 出發(fā)倒著跑的那個(gè)人
如此一來(lái)就將問(wèn)題轉(zhuǎn)換為二分圖的最小點(diǎn)覆蓋問(wèn)題了,首先引用一下最小點(diǎn)覆蓋的定義(來(lái)自大藍(lán)書(shū))
最小點(diǎn)覆蓋: 給定一張二分圖,求出一個(gè)最小的點(diǎn)集S,使得圖中任意一條邊都有至少一個(gè)端點(diǎn)屬于S。
在這個(gè)題目中,每條邊代表的是每個(gè)監(jiān)控的信息,每個(gè)點(diǎn)代表的是跑步的學(xué)生,這樣一來(lái),最小點(diǎn)覆蓋的定義與本題就對(duì)應(yīng)起來(lái)了:求出最少跑步的學(xué)生,使得所有的監(jiān)控條件都滿(mǎn)足
不過(guò)本題中 n 和 m 都是 1e5 的級(jí)別,樸素的求最小點(diǎn)覆蓋的算法是匈牙利算法,時(shí)間復(fù)雜度是 n * n 的,所以我們需要用網(wǎng)絡(luò)流將其優(yōu)化為 msqrt( n?) 的時(shí)間復(fù)雜度才行
代碼: ?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=2e5+100;const int limit=1e9;int l[N],r[N],n;vector<int>vl,vr;struct Edge
{int to,w,next;
}edge[N<<1];//邊數(shù)int head[N],cnt;void addedge(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向邊邊權(quán)設(shè)置為0edge[cnt].next=head[v];head[v]=cnt++;
}int d[N],now[N];//深度 當(dāng)前弧優(yōu)化bool bfs(int s,int t)//尋找增廣路
{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;
}int dinic(int x,int t,int flow)//更新答案
{if(x==t)return flow;int rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(w&&d[v]==d[x]+1){int k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;
}void init()
{memset(now,0,sizeof(now));memset(head,-1,sizeof(head));cnt=0;
}int solve(int st,int ed)
{int ans=0,flow;while(bfs(st,ed))while(flow=dinic(st,ed,inf))ans+=flow;return ans;
}void discreate()//離散化
{sort(vl.begin(),vl.end());sort(vr.begin(),vr.end());vl.erase(unique(vl.begin(),vl.end()),vl.end());vr.erase(unique(vr.begin(),vr.end()),vr.end());for(int i=1;i<=n;i++){l[i]=lower_bound(vl.begin(),vl.end(),l[i])-vl.begin()+1;r[i]=lower_bound(vr.begin(),vr.end(),r[i])-vr.begin()+1+vl.size();}
}void build(int st,int ed)//建圖
{init();for(int i=1;i<=n;i++)addedge(l[i],r[i],1);for(int i=0;i<vl.size();i++)addedge(st,i+1,1);for(int i=0;i<vr.size();i++)addedge(i+1+vl.size(),ed,1);
}int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--){vl.clear(),vr.clear();int st=N-1,ed=st-1;scanf("%d",&n);for(int i=1;i<=n;i++){int t,x;scanf("%d%d",&t,&x);l[i]=t-x,r[i]=t-(limit-x);vl.push_back(l[i]);vr.push_back(r[i]);}discreate();build(st,ed);printf("%d\n",solve(st,ed));}return 0;
}
?
總結(jié)
以上是生活随笔 為你收集整理的HDU多校4 - 6808 Go Running(最小点覆盖+网络流优化) 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。