hdu_1281

来源:互联网 发布:htc m8数据连接已断开 编辑:程序博客网 时间:2024/06/08 15:32
ek超时,简单的dinic或者isap的应用
#include <stdio.h>#include <string.h>#include <queue>#include <algorithm>#define MAXROW 109#define MAXCOL 109#define INF 0x7fffffffusing namespace std;typedef struct p{int x;int y;}Point;int row;int col;int cnt;int vertex;int max_flow;int level[MAXROW+MAXCOL];int r[MAXROW+MAXCOL][MAXROW+MAXCOL];int t_r[MAXROW+MAXCOL][MAXROW+MAXCOL];Point arr[MAXROW*MAXCOL];bool bfs(void){int cur;queue<int> q;memset(level, -1, sizeof(level));level[0] = 0;q.push(0);while( !q.empty() ){cur = q.front(); q.pop();for(int i = 0; i <= vertex; i ++){if( -1 != level[i] || !r[cur][i] )continue;level[i] = level[cur]+1;q.push(i);}}return (-1 == level[vertex])? false : true;}int dfs(int u, int cur_val){int tmp_val;int val;if( u == vertex )return cur_val;tmp_val = cur_val;for(int v = 0; v <= vertex; v ++){if( (level[v] != level[u]+1) || !r[u][v])continue;val = dfs(v, min(tmp_val, r[u][v]));tmp_val -= val;r[u][v] -= val;r[v][u] += val;}return cur_val - tmp_val;}int dinic(void){int val;int ans;ans = val = 0;while( true == bfs() )while( val = dfs(0, INF) )ans += val;return ans;}void copy_arr(const int from[][MAXROW+MAXCOL], int to[][MAXROW+MAXCOL]){for(int i = 0; i <= vertex; i ++)for(int j = 0; j <= vertex; j ++)to[i][j] = from[i][j];}int main(int argc, char const *argv[]){//freopen("test.in", "r", stdin);int from;int to;int val;int ans;int cnts;cnts = 1;while( ~scanf("%d %d %d", &row, &col, &cnt) ){vertex = row+col+1;memset(r, 0, sizeof(r));for(int i = 1; i <= row; i ++)r[0][i] = 1;for(int i = row+1; i < vertex; i ++)r[i][vertex] = 1;for(int i = 0; i < cnt; i ++){scanf("%d %d", &from, &to);r[from][to+row] ++;arr[i].x = from;arr[i].y = to+row;}copy_arr(r, t_r);max_flow = dinic();ans = 0;copy_arr(t_r, r);for(int i = 0; i < cnt; i ++){r[arr[i].x][arr[i].y] --;val = dinic();if( val != max_flow )ans ++;copy_arr(t_r, r);}printf("Board %d have %d important blanks for %d chessmen.\n", cnts ++, ans, max_flow);}return 0;}

原创粉丝点击