zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Robin Hood CodeForces
    Arthur and Questions CodeForces
    AC日记——过河卒 洛谷 1002
    加密(模拟)
    AC日记——codevs 1086 栈 (卡特兰数)
    AC日记——搞笑世界杯 codevs 1060(dp)
    AC日记—— codevs 1031 质数环(搜索)
    AC日记——产生数 codevs 1009 (弗洛伊德)(组合数学)
    AC日记——阶乘之和 洛谷 P1009(高精度)
    AC日记——逃跑的拉尔夫 codevs 1026 (搜索)
  • 原文地址:https://www.cnblogs.com/mrclr/p/10800138.html
Copyright © 2011-2022 走看看