題目大意:在平面中有一些巫妖和一些小精靈,還有一些樹會阻擋巫妖的視線。每一個巫妖都有一個攻擊半徑,如果一個小精靈在巫妖的攻擊半徑內,兩點之間的連線沒有樹木阻擋,那麼巫妖就可以秒殺小精靈。每個巫妖都有技能的CD。問最快多長時間可以使小精靈全滅。
思路:看出范圍知算法系列。很明顯的二分+最大流。二分最短時間,在這個時間內,每個巫妖可以發招time / CD + 1次。那麼每次建圖就從S到每個巫妖連能夠輸出的次數流量的邊。每個巫妖向能攻擊到的小精靈連邊,之後每個小精靈向T連邊。每次判斷max_flow是否等於小精靈數。
CODE:
#include#include #include #include #include #include #include #define MAX 510 #define MAXE 1000000 #define INF 0x3f3f3f3f #define S 0 #define T (MAX - 1) using namespace std; #define min(a,b) ((a) < (b) ? (a):(b)) struct Point{ int x,y; }pos[MAX]; struct Complex{ Point p; int r; void Read() { scanf("%d%d%d",&p.x,&p.y,&r); } }src[MAX],tree[MAX]; vector can[MAX]; int _time[MAX]; int cnt,_cnt,trees; inline double Calc(const Point &p1,const Point &p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } inline double GetArea(const Point &p1,const Point &p2,const Point &p3) { double l1 = Calc(p1,p2),l2 = Calc(p2,p3),l3 = Calc(p3,p1); double p = (l1 + l2 + l3) / 2; return sqrt(p * (p - l1) * (p - l2) * (p - l3)); } inline bool InRange(const Complex &tree,const Point &p1,const Point &p2) { double area = GetArea(p1,p2,tree.p) * 2; double dis = area / Calc(p1,p2); return dis <= tree.r; } int head[MAX],total; int next[MAXE],aim[MAXE],flow[MAXE]; inline void Add(int x,int y,int f) { next[++total] = head[x]; aim[total] = y; flow[total] = f; head[x] = total; } inline void Insert(int x,int y,int f) { Add(x,y,f); Add(y,x,0); } inline void BuildGraph(int ans) { memset(head,0,sizeof(head)); total = 1; for(int i = 1; i <= cnt; ++i) { Insert(S,i,ans / _time[i] + 1); for(vector ::iterator it = can[i].begin(); it != can[i].end(); ++it) Insert(i,cnt + *it,1); } for(int i = 1; i <= _cnt; ++i) Insert(cnt + i,T,1); } int deep[MAX]; inline bool BFS() { static queue q; while(!q.empty()) q.pop(); memset(deep,0,sizeof(deep)); deep[S] = 1; q.push(S); while(!q.empty()) { int x = q.front(); q.pop(); for(int i = head[x]; i; i = next[i]) if(!deep[aim[i]] && flow[i]) { deep[aim[i]] = deep[x] + 1; q.push(aim[i]); if(aim[i] == T) return true; } } return false; } int Dinic(int x,int f) { if(x == T) return f; int temp = f; for(int i = head[x]; i; i = next[i]) if(temp && deep[aim[i]] == deep[x] + 1 && flow[i]) { int away = Dinic(aim[i],min(flow[i],temp)); if(!away) deep[aim[i]] = 0; flow[i] -= away; flow[i^1] += away; temp -= away; } return f - temp; } int main() { cin >> cnt >> _cnt >> trees; for(int i = 1; i <= cnt; ++i) { src[i].Read(); scanf("%d",&_time[i]); } for(int i = 1; i <= _cnt; ++i) scanf("%d%d",&pos[i].x,&pos[i].y); for(int i = 1; i <= trees; ++i) tree[i].Read(); for(int i = 1; i <= cnt; ++i) for(int j = 1; j <= _cnt; ++j) if(Calc(src[i].p,pos[j]) <= src[i].r) { bool flag = true; for(int k = 1; k <= trees; ++k) if(InRange(tree[k],src[i].p,pos[j])) flag = false; if(flag) can[i].push_back(j); } int l = 0,r = 10000000,ans = -1; while(l <= r) { int mid = (l + r) >> 1; BuildGraph(mid); int max_flow = 0; while(BFS()) max_flow += Dinic(S,INF); if(max_flow == _cnt) r = mid - 1,ans = mid; else l = mid + 1; } cout << ans << endl; return 0; }