Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
最大流,王记初始化了。。。嚷昂一次#include <iostream> #include <stdio.h> #include <queue> using namespace std; const int SSS = 2147483647; const int TTT = -2147483648; int N, M; int graph[60][1010] = {{0}}; bool s[1010] = {false}, t[1010] = {false};//false表示可用 void init(){ for(int i = 0; i < 60; i++){ for(int j = 0; j < 1010; j++){ graph[i][j] = 0; } } for(int i = 0; i < 1010; i++){ s[i] = t[i] = 0; } } bool pushflow(){ //如果push不了,返回false queue<int> bfs; bool getT = false; int noT; int S[1010] = {0}, T[1010] = {0}; for(int i = 1; i < N; i++){ if(!s[i]){ S[i] = SSS; bfs.push(i); } } while(!bfs.empty()){ int cur = bfs.front(); bfs.pop(); if(cur > 0){ //是s这边的点 for(int j = 1; j <= M; j++){ if(graph[cur][j] == 1 && T[j] == 0){ bfs.push(-j); T[j] = cur; } } } else{ //是t这边的点 int j = -cur; if(!t[j]){ getT = true; noT = cur; break; } for(int i = 1; i < N; i++){ if(graph[i][j] == -1 && S[i] == 0){ bfs.push(i); S[i] = cur; } } } } if(!getT) return false; t[-noT] = true; int TtoS = 1; while(1){ if(TtoS){ //push路径当前从S端到T端 int noS = T[-noT]; graph[noS][-noT] = -1; noT = noS; } else{ //从T端到S端 int noS = S[noT]; if(noS == SSS){ s[noT] = true; break; } graph[noT][-noS] = 1; noT = noS; } TtoS = 1 - TtoS; } return true; } int main() { int pl, cd; int cnt = 0; while(1){ init(); scanf("%d%d", &pl, &cd); if(pl == 0 && cd == 0) break; cnt++; int sIdx[60], tIdx[1010]; bool used[1010] = {0}; for(int i = 1; i <= cd; i++){ int temp; scanf("%d", &temp); used[temp] = 1; sIdx[i] = temp; } int myg = 1; for(int j = 1; j <= pl*cd; j++){ if(!used[j]){ tIdx[myg] = j; myg++; } } N = cd+1, M = (pl-1)*cd; for(int i = 1; i < N; i++){ for(int j = 1; j <= M; j++){ if(sIdx[i] < tIdx[j]) graph[i][j] = 1; } } int flow = 0; while(pushflow()){ flow++; //cout << flow << endl; } printf("Case %d: %d\n", cnt, cd-flow); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator