zoukankan      html  css  js  c++  java
  • 【网络流24题】孤岛营救问题

    这道题和网络流有什么关系吗?
    状态压缩BFS

    /*
    @Date    : 2019-07-16 21:37:18
    @Author  : Adscn (adscn@qq.com)
    @Link    : https://www.cnblogs.com/LLCSBlog
    */
    #include<bits/stdc++.h>
    using namespace std;
    #define IL inline
    #define RG register
    #define gi getint()
    #define gc getchar()
    #define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
    IL int getint()
    {
    	RG int xi=0;
    	RG char ch=gc;
    	bool f=0;
    	while(ch<'0'|ch>'9')ch=='-'?f=1:f,ch=gc;
    	while(ch>='0'&ch<='9')xi=(xi<<1)+(xi<<3)+ch-48,ch=gc;
    	return f?-xi:xi;
    }
    template<typename T>
    IL void pi(T k,char ch=0)
    {
    	if(k<0)k=-k,putchar('-');
    	if(k>=10)pi(k/10,0);
    	putchar(k%10+'0');
    	if(ch)putchar(ch);
    }
    int tab[12][12][12][12];
    vector<int> key[12][12];
    bool vis[12][12][(1<<15)];
    int dx[]={0,-1,1,0};
    int dy[]={1,0,0,-1};
    struct node{int nx,ny,nk,ans;};
    inline void getkey(int &k,int x,int y)
    {
    	for(auto &&p:key[x][y])k|=(1<<p);
    }
    int main(void)
    {
    	#ifndef ONLINE_JUDGE
    //	File("");
    	#endif
    	int n=gi,m=gi,p=gi;
    	int k=gi;
    	for(int i=1;i<=k;++i)
    	{
    		int _x=gi,_y=gi,x2=gi,y2=gi,G=gi;
    		tab[_x][_y][x2][y2]=tab[x2][y2][_x][_y]=(G!=0?G:p+1);
    	}
    	int s=gi;
    	for(int i=1;i<=s;++i)
    	{
    		int a=gi,b=gi,c=gi;
    		key[a][b].push_back(c);
    	}
    	static std::queue<node>Q;
    	int TMP=1;
    	getkey(TMP,1,1);
    	Q.push({1,1,TMP,0});
    	vis[1][1][TMP]=1;
    	while(!Q.empty())
    	{
    		node p=Q.front();Q.pop();
    		if(p.nx==n&&p.ny==m)pi(p.ans),exit(0);
    		for(int i=0;i<4;++i)
    		{
    			int nx=p.nx+dx[i],ny=p.ny+dy[i],nk=p.nk,state=tab[p.nx][p.ny][nx][ny];
    			getkey(nk,nx,ny);
    			if(nx<1||nx>n||ny<1||ny>m||(p.nk&(1<<state))==0||vis[nx][ny][nk])continue;	
    			Q.push((node){nx,ny,nk,p.ans+1}),vis[nx][ny][nk]=1;
    		}
    	}
    	pi(-1);
    	return 0;
    }
    
  • 相关阅读:
    POJ 1862 Stripies (简单贪心)
    HDU 2181 哈密顿绕行世界问题(DFS)
    POJ 3262 Protecting the Flowers (贪心)
    Aizu 2249 Road Construction (最短路 SPFA)
    POJ 3484 Showstopper(二分)
    HDU 6235 2017-CCPC-哈尔滨站 Permutation(简单模拟)
    IDEA常用插件
    解决IDEA插件加载失败
    解决zsh无法使用maven
    Linux安装Maven
  • 原文地址:https://www.cnblogs.com/LLCSBlog/p/11199240.html
Copyright © 2011-2022 走看看