HDU 3605 Escape

Escape

题解:如果一个人一条边和适应星球连一条流量为1的边,那么结果因为点数太多,边数太多而TLE。现在我们把所有的适应星球状态一样的人放在一起,然后在同一个状态的人的边一起连边,从而减少边数和点数。

代码:

  1 #include
  2 using namespace std;
  3 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
  4 #define LL long long
  5 #define ULL unsigned LL
  6 #define fi first
  7 #define se second
  8 #define pb emplace_back
  9 #define lson l,m,rt<<1
 10 #define rson m+1,r,rt<<1|1
 11 #define lch(x) tr[x].son[0]
 12 #define rch(x) tr[x].son[1]
 13 #define max3(a,b,c) max(a,max(b,c))
 14 #define min3(a,b,c) min(a,min(b,c))
 15 typedef pair<int,int> pll;
 16 const int inf = 0x3f3f3f3f;
 17 const LL INF = 0x3f3f3f3f3f3f3f3f;
 18 const LL mod =  (int)1e9+7;
 19 const int N = 1024 + 100;
 20 const int M = N * 100;
 21 int head[N], deep[N], cur[N];
 22 int w[M], to[M], nx[M];
 23 int tot;
 24 void add(int u, int v, int val){
 25     w[tot]  = val; to[tot] = v;
 26     nx[tot] = head[u]; head[u] = tot++;
 27 
 28     w[tot] = 0; to[tot] = u;
 29     nx[tot] = head[v]; head[v] = tot++;
 30 }
 31 int bfs(int s, int t){
 32     queue<int> q;
 33     memset(deep, 0, sizeof(deep));
 34     q.push(s);
 35     deep[s] = 1;
 36     while(!q.empty()){
 37         int u = q.front();
 38         q.pop();
 39         for(int i = head[u]; ~i; i = nx[i]){
 40             if(w[i] > 0 && deep[to[i]] == 0){
 41                 deep[to[i]] = deep[u] + 1;
 42                 q.push(to[i]);
 43             }
 44         }
 45     }
 46     return deep[t] > 0;
 47 }
 48 int Dfs(int u, int t, int flow){
 49     if(u == t) return flow;
 50     for(int &i = cur[u]; ~i; i = nx[i]){
 51         if(deep[u]+1 == deep[to[i]] && w[i] > 0){
 52             int di = Dfs(to[i], t, min(w[i], flow));
 53             if(di > 0){
 54                 w[i] -= di, w[i^1] += di;
 55                 return di;
 56             }
 57         }
 58     }
 59     return 0;
 60 }
 61 
 62 int Dinic(int s, int t){
 63     int ans = 0, tmp;
 64     while(bfs(s, t)){
 65         for(int i = 0; i <= t; i++) cur[i] = head[i];
 66         while(tmp = Dfs(s, t, inf)) ans += tmp;
 67     }
 68     return ans;
 69 }
 70 int a[N];
 71 void init(){
 72     memset(head, -1, sizeof(head));
 73     memset(a, 0, sizeof(a));
 74     tot = 0;
 75 }
 76 int main(){
 77     int n, m;
 78     while(~scanf("%d%d", &n, &m)){
 79         init();
 80         int Max = (1 << m)- 1, val;
 81         for(int i = 1; i <= n; ++i){
 82             int tmp = 0;
 83             for(int i = 1; i <= m; ++i){
 84                 scanf("%d", &val);
 85                 tmp = tmp * 2 + val;
 86             }
 87             ++a[tmp];
 88         }
 89         int s = 0, t = Max + m + 1;
 90         for(int i = 1; i <= m; ++i){
 91             scanf("%d", &val);
 92             add(i + Max, t, val);
 93         }
 94         for(int i = 1; i <= Max; ++i){
 95             add(s, i, a[i]);
 96             for(int j = 1; j <= m; ++j){
 97                 if((i>>(m-j))&1) {
 98                     add(i, j+Max, inf);
 99                 }
100             }
101         }
102         if(Dinic(s,t) == n) puts("YES");
103         else puts("NO");
104     }
105     return 0;
106 }
View Code

 

转载于:https://www.cnblogs.com/MingSD/p/9737160.html

你可能感兴趣的:(HDU 3605 Escape)