#include #include #include #include #include #include #include using namespace std; struct Flow { int x1,y1,x2,y2; int id; }; int help=0; int sumtime=0; bool DEBUG=false; Flow Flows[501]; vector< pair > FlowPath[501]; bool Blocked[111][111]; bool Flowed[111][111]; bool EndPoint[111][111]; int n; int p,B; int TFO[111][111]; int Key=1; int EndPointRiver[111][111]; int TIME_LIMIT=4*CLOCKS_PER_SEC+CLOCKS_PER_SEC/2; bool TimeIsOK() { return clock() > Path; pair Moves[5]; bool OK(int x,int y) { if (x<1 || x>n || y<1 || y>n) return false; if (TFO[x][y]==Key) return false; if (Blocked[x][y]) return false; if (Flowed[x][y]) return false; if (EndPoint[x][y] && (x!=GX || y!=GY) && (x!=SX || y!=SY) ) return false; return true; } bool DFS(int x,int y) { if (!OK(x,y)) return false; TFO[x][y]=Key; Path.push_back(make_pair(x,y)); if (x==GX && y==GY) return true; int i; for (i=1;i<=4;i++) { if ( DFS(x+Moves[i].first,y+Moves[i].second) ) return true; } Path.pop_back(); return false; } int Seen[301]; void FindRivers(int x,int y) { if (x<1 || x>n || y<1 || y>n) return; if (TFO[x][y]==Key) return; if (Blocked[x][y]) return; if (Flowed[x][y]) return; int i; TFO[x][y]=Key; if (EndPointRiver[x][y]!=0) { if ( (x!=SX || y!=SY) && (x!=GX || y!=GY) ) { Seen[ EndPointRiver[x][y] ]++; return; } } for (i=1;i<=4;i++) { FindRivers(x+Moves[i].first,y+Moves[i].second); } return; } int comefrom[100001]; pair q[100001]; int qL=0; void BFS(int x,int y) { int uk=1; int i; int rem=-1; qL=1; q[1]=make_pair(x,y); comefrom[1]=0; TFO[x][y]=Key; while(uk<=qL) { x=q[uk].first; y=q[uk].second; if (x==GX && y==GY) { rem=uk; break; } for (i=1;i<=4;i++) { if (OK(x+Moves[i].first,y+Moves[i].second)) { qL++; q[qL]=make_pair(x+Moves[i].first,y+Moves[i].second); comefrom[qL]=uk; TFO[ x+Moves[i].first ][ y+Moves[i].second ]=Key; } } uk++; } if (rem==-1) return; while(rem!=0) { Path.push_back( q[rem] ); rem=comefrom[rem]; } reverse(Path.begin(),Path.end()); return; } vector< pair > BestPath[501]; int BestValue=0; void Evaluate() { int paths=0; int flooded=0; int i,j; int Value; for (i=1;i<=p;i++) { if (!FlowPath[i].empty()) paths++; flooded+=FlowPath[i].size(); } Value=flooded*paths; //fprintf(stderr,"Value = %d\n",Value); if (Value>BestValue) { BestValue=Value; ///fprintf(stderr,"%dx%d\n",paths,flooded); for (i=1;i<=p;i++) { BestPath[i].clear(); for (j=0;j > LongestPath; vector Added; void CLEAR(int k) { int i; for (i=0;iLongestPath.size()) { LongestPath.clear(); for (in=0;in