bzoj3158 千钧一发

嘟嘟嘟


这题没想出来,还是菜。


知道是最小割,但不知道怎么建立关系。
首先,都能想到(除了我)两个奇数一定满足条件1,两个偶数一定满足条件1。所以只用考虑奇偶之间能否同时选。
然后搞一个二分图,暴力判断,如果不能同时选,就连一条INF的边。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 3e3 + 5;
const int maxe = 1e6  + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, t, sum = 0, a[maxn], b[maxn];
struct Edge
{
  int nxt, from, to, cap, flow;
}e[maxe];
int head[maxn << 1], ecnt = -1;
In void addEdge(int x, int y, int w)
{
  e[++ecnt] = (Edge){head[x], x, y, w, 0};
  head[x] = ecnt;
  e[++ecnt] = (Edge){head[y], y, x, 0, 0};
  head[y] = ecnt;
}

int dis[maxn << 1];
In bool bfs()
{
  Mem(dis, 0); dis[0] = 1;
  queue<int> q; q.push(0);
  while(!q.empty())
    {
      int now = q.front(); q.pop();
      for(int i = head[now], v; i != -1; i = e[i].nxt)
	{
	  if(!dis[v = e[i].to] && e[i].cap > e[i].flow)
	    dis[v] = dis[now] + 1, q.push(v);
	}
    }
  return dis[t];
}
int cur[maxn << 1];
In int dfs(int now, int res)
{
  if(now == t || res == 0) return res;
  int flow = 0, f;
  for(int& i = cur[now], v; i != -1; i = e[i].nxt)
    {
      v = e[i].to;
      if(dis[v] == dis[now] + 1 && (f = dfs(v, min(res, e[i].cap - e[i].flow))) > 0)
	{
	  e[i].flow += f; e[i ^ 1].flow -= f;
	  flow += f; res -= f;
	  if(res == 0) break;
	}
    }
  return flow;
}
In int minCut()
{
  int flow = 0;
  while(bfs())
    {
      memcpy(cur, head, sizeof(head));
      flow += dfs(0, INF);
    }
  return flow;
}


In ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
In bool judge(ll x, ll y)
{
  ll mul = x * x + y * y, z = sqrt(mul);
  if(z * z != mul) return 1;
  if(gcd(x, y) > 1) return 1;
  return 0;
}

int main()
{
  //freopen("ha.in", "r", stdin);
  Mem(head, -1);
  n = read(); t = n + n + 1;
  for(int i = 1; i <= n; ++i) a[i] = read();
  for(int i = 1; i <= n; ++i) b[i] = read(), sum += b[i];
  for(int i = 1; i <= n; ++i)
    {
      if(a[i] & 1) addEdge(0, i, b[i]);
      else addEdge(i, t, b[i]);
      for(int j = 1; j <= n; ++j)
	if((a[i] & 1) && !(a[j] & 1))
	  if(!judge(a[i], a[j])) addEdge(i, j, INF);
    }
  write(sum - minCut()), enter;
  return 0;
}
posted @ 2019-05-01 16:49  mrclr  阅读(129)  评论(0编辑  收藏  举报