Network Flow, question 24, question 14, island rescue, problem hierarchy

Source: Internet
Author: User

A grid chart has some points that may have some key. There may be a door between the node and the node. Some doors can pass through with specific keys, and some cannot pass through in any way. Calculate the shortest time from (1, 1) to (m, n.


Idea: layer chart + state compression. F [I] [J] [K], where I and j describe the current location, K is the current key that is compressed (because the number of keys is <= 10, so all States can be completed within the space of 1 <10 ). Then, when updating data in four directions, determine whether the data can pass through the 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,sizeof(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 << 11); ++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][_status] > 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 false;if(need_key == -1)return true;return (status >> need_key)&1;}


Network Flow, question 24, question 14, island rescue, problem hierarchy

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.