zoukankan      html  css  js  c++  java
  • 孤岛营救问题(最短路 状态压缩)  网络流24题

    状态压缩,最短路

    //http://www.cnblogs.com/IMGavin/
    #include <iostream>
    #include <stdio.h>
    #include <cstdlib>
    #include <cstring>
    #include <queue>
    #include <vector>
    #include <map>
    #include <stack>
    #include <set>
    #include <bitset>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    #define gets(A) fgets(A, 1e8, stdin)
    const int INF = 0x3F3F3F3F, N = 50, MOD = 1003, M = 1800;
    
    const double EPS = 1e-6;
    int f[N][N][N][N];
    int key[N][N];
    bool inq[N][N][M];
    int dis[N][N][M];
    int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    struct node{
    	int x, y, st;
    	node(int x1, int y1, int st1){
    		x = x1;
    		y = y1;
    		st = st1;
    	}
    };
    
    bool check(int x1, int y1, int x2, int y2, int st){
    	if(f[x1][y1][x2][y2] == INF){
    		return true;
    	}
    	if(f[x1][y1][x2][y2] == -1){
    		return false;
    	}
    	if((1 << f[x1][y1][x2][y2]) & st){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    
    int SPFA(int n, int m, int p){
    	memset(inq, 0, sizeof(inq));
    	memset(dis, 0x3F, sizeof(dis));
    	dis[1][1][0] = 0;
    	queue <node> q;
    	q.push(node(1, 1, 0));
    	while(!q.empty()){
    		node u = q.front();
    		q.pop();
    		inq[u.x][u.y][u.st] = 0;
    		for(int i = 0; i < 4; i++){
    			int x1 = u.x + dir[i][0];
    			int y1 = u.y + dir[i][1];
    			int st = u.st | key[x1][y1];
    			if(x1 > 0 && x1 <= n && y1 > 0 && y1 <= m && check(u.x, u.y, x1, y1, st)){
    				if(dis[x1][y1][st] > dis[u.x][u.y][u.st] + 1){
    					dis[x1][y1][st] = dis[u.x][u.y][u.st] + 1;
    					node v(x1, y1, st);
    					q.push(v);
    					inq[v.x][v.y][v.st] = 1;
    				}
    			}
    		}
    	}
    	int ans = INF;
    	for(int i = 0; i < (1 << p); i++){
    		ans = min(ans, dis[n][m][i]);
    	}
    	if(ans == INF){
    		ans = -1;
    	}
    	return ans;
    }
    
    int main(){
    	int n, m, p, k;
    	while(cin >> n >> m >> p){
    		int x1, y1, x2, y2, g;
    		memset(f, 0x3F, sizeof(f));
    		cin >> k;
    		while(k--){
    			cin >> x1 >> y1 >> x2 >> y2 >> g;
    			g--;
    			f[x2][y2][x1][y1] = f[x1][y1][x2][y2] = g;
    		}
    		memset(key, 0, sizeof(key));
    		cin >> k;
    		while(k--){
    			cin >> x1 >> y1 >> g;
    			g--;
    			key[x1][y1] |= (1 << g);
    		}
    		cout<<SPFA(n, m, p)<<endl;
    	}
    
    	return 0;
    }
  • 相关阅读:
    git 记录
    js 技巧
    首页三张幻灯片放什么
    6系统盈利方向
    WordPress获取某个分类关联的标签
    目标型长尾如何优化
    简介如何去除WordPress主题版权保护的方法
    2017.7.7 长尾关键词系统优化
    2017.7.6 目标关键词分析
    2017.7.2 seo知识总纲
  • 原文地址:https://www.cnblogs.com/IMGavin/p/6391911.html
Copyright © 2011-2022 走看看