P2765-魔术球问题

  1 #include<bits/stdc++.h>
  2 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
  3 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
  4 #define INF 0x3f3f3f3f
  5 #define MOD 100000000
  6 #define maxn 100003
  7 #define pb push_back
  8 #define debug() printf("Miku Check OK!\n")
  9 typedef long long ll;
 10 
 11 using namespace std;
 12 typedef pair<int,int> P;
 13 inline ll read()
 14 {
 15     ll ans = 0;
 16     char ch = getchar(), last = ' ';
 17     while(!isdigit(ch)) last = ch, ch = getchar();
 18     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
 19     if(last == '-') ans = -ans;
 20     return ans;
 21 }
 22 inline void write(ll x)
 23 {
 24     if(x < 0) x = -x, putchar('-');
 25     if(x >= 10) write(x / 10);
 26     putchar(x % 10 + '0');
 27 }
 28 int ver[maxn],Next[maxn],head[maxn],val[maxn];
 29 int d[maxn];
 30 int pillars,s,t,tot,maxflow;
 31 const int N = 3000;
 32 void add(int x,int y,int w)
 33 {
 34     ver[++tot] = y,Next[tot] = head[x],head[x] = tot,val[tot] = w;
 35 }
 36 bool bfs()
 37 {
 38     memset(d,0,sizeof(d));
 39     queue<int> q;
 40     q.push(s);
 41     d[s] = 1;
 42     while(!q.empty())
 43     {
 44         int x = q.front();
 45         q.pop();
 46         for(int i = head[x]; i; i = Next[i])
 47             if(val[i] && !d[ver[i]])
 48             {
 49                 q.push(ver[i]);
 50                 d[ver[i]] = d[x]+1;
 51                 if(ver[i]==t)
 52                     return true;
 53             }
 54     }
 55     return false;
 56 }
 57 int dinic(int x,int flow)
 58 {
 59     if(x==t) return flow;
 60     // k为子节点增量
 61     int rest = flow, k;
 62     for(int i = head[x]; i && rest; i = Next[i])
 63     {
 64         if(val[i] && d[ver[i]] == d[x]+1)
 65         {
 66             k = dinic(ver[i],min(rest,val[i]));
 67             if(!k) d[ver[i]] = 0;
 68             val[i] -= k;
 69             val[i^1] += k;
 70             rest -= k;
 71         }
 72     }
 73     return flow - rest;
 74 }
 75 vector<int> st;
 76 int vvis[maxn];
 77 void go(int i,vector<int>& rnt)
 78 {
 79     rnt.pb(i);
 80     int j = head[i];
 81     for(int j = head[i]; j ; j = Next[j])
 82     {
 83         int y = ver[j];
 84         
 85         if(y>N && !vvis[y-N] && !val[j])
 86         {
 87             vvis[y-N] = 1;
 88             go(y-N,rnt);
 89             return ;
 90         }
 91     }
 92 }
 93 int main()
 94 {
 95     pillars = read();
 96     tot = 1;
 97     s = 0;
 98     t = 2*N+1;
 99     int ans = 0,balls = 0;
100 
101     while("Hatsune Miku")
102     {
103         ans ++;
104         balls ++;
105         _for(i,1,balls)
106         if(sqrt(i+balls) == (int)sqrt(i+balls))
107         {
108             add(i,balls+N,1);
109             add(balls+N,i,0);
110         }
111         add(0,balls,1);
112         add(balls,0,0);
113         add(balls+N,2*N+1,1);
114         add(2*N+1,balls+N,0);
115         int flow = 0;
116         while(bfs())
117             while(flow = dinic(s,INF))
118                 ans -= flow;
119         if(ans > pillars)
120             break;
121     }
122     
123     printf("%d\n",balls-1);
124     vvis[0] = 1;vvis[balls] = 1;
125     _for(i,1,balls)
126         for(int j = head[i+N]; j ; j = Next[j])
127             if(ver[j]==2*N+1 && val[j]==1)
128                 st.pb(i),vvis[i] = 1;
129 
130     vector<int> tmp;
131     _for(i,0,st.size())
132     {
133         vvis[st[i]] = 1,go(st[i],tmp);
134         _for(j,0,tmp.size()-1)
135         printf("%d ",tmp[j]);
136         printf("%d\n",tmp[tmp.size()-1]);
137         tmp.clear();
138     }
139 
140     return 0;
141 }

 

posted @ 2019-10-08 23:34  Asurudo  阅读(172)  评论(0编辑  收藏  举报