Network Flow 24 Questions 14 Island rescue problem Layer Map

Source: Internet
Author: User

The main topic: a grid chart, some of the points above may have some kind of key. There may be gates between nodes and nodes. Some doors require specific keys to pass, and some do not. The shortest time to start from (m,n).


Idea: Hierarchical graph + state compression.

F[i][j][k], where I and J describe the current location. K is the current key that has been compressed (because the number of keys is <=10, so the whole state can be 1<<10 within the space). Then, when updating in four directions, it is possible to pass through a door.


CODE:


#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define MAX 20#define INF 0x3f3f3f3fusing namespace Std;const int dx[] = {0,1,-1,0,0};const int dy[] = {0,0,0 , 1,-1};struct Complex{int x,y,status; Complex (int _,int __,int ___): X (_), Y (__), status (___) {}complex () {}};int m,n,p,doors,keys;int f[max][max][1 << 11 ];bool v[max][max][1 << 11];int map[max][max][max][max];int key[max][max];void SPFA (); inline bool Accelerator ( int X1,int y1,int x2,int y2,int status); int main () {cin >> m >> n >> P >> doors;memset (Map,-1,sizeo F (map)); for (int a,b,c,d,x,i = 1;i <= doors; ++i) {scanf ("%d%d%d%d%d", &a,&b,&c,&d,&x); Map[a][b] C [d] = map[c][d][a][b] = x;} CIN >> keys;for (int x,y,z,i = 1;i <= keys; ++i) {scanf ("%d%d%d", &x,&y,&z); Key[x][y] |= 1 << z;} SPFA (); int ans = inf;for (int i = 0;i < (1 << one); ++i) ans = min (ans,f[m][n][i]); if (ans = = INF) ans = -1;cout << ans << endl;return 0;} void Spfa () {memset (f,0x3f,sizeof (f)); F[1][1][0] = 0;static queue<complex> q;while (!q.empty ()) Q.pop (); Q.push ( Complex (1,1,0)); while (!q.empty ()) {Complex now = Q.front (); Q.pop (); V[now.x][now.y][now.status] = False;int _status = Now.status;if (Key[now.x][now.y]) _status |= key[now.x][now.y];for (int i = 1;i <= 4; ++i) {int FX = now.x + dx[i];int fy = Now.y + dy[i];if (!fx | |!fy | | FX > M | | fy > N) continue;if (Accelerator (Now.x,now.y,fx,fy,_status)) if (f[fx][fy][_s Tatus] > F[now.x][now.y][now.status] + 1) {F[fx][fy][_status] = F[now.x][now.y][now.status] + 1;if (!v[fx][fy][_ Status]) V[fx][fy][_status] = True,q.push (Complex (Fx,fy,_status));}}} inline bool Accelerator (int x1,int y1,int x2,int y2,int status) {int need_key = map[x1][y1][x2][y2];if (!need_key) return FA Lse;if (Need_key = =-1) return True;return (status >> need_key) &1;}


Network Flow 24 Questions 14 Island rescue problem Layer Map

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.