• [luoguP1849] [USACO12MAR]拖拉机Tractor(spfa)


    传送门

    神奇的spfa

    #include <queue>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #define N 1010
    #define max(x, y) ((x) > (y) ? (x) : (y))
    
    int n, mx, my;
    int dis[N][N];
    bool map[N][N], vis[N][N];
    int dx[4] = {0, -1, 0, 1}, dy[4] = {1, 0, -1, 0};
    
    struct node
    {
    	int x, y;
    	node(int x = 0, int y = 0) : x(x), y(y) {}
    };
    
    std::queue <node> q;
    
    inline int read()
    {
    	int x = 0, f = 1;
    	char ch = getchar();
    	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
    	return x * f;
    }
    
    int main()
    {
    	node u, v;
    	int i, x, y;
    	n = read();
    	x = read();
    	y = read();
    	memset(dis, 127, sizeof(dis));
    	dis[x][y] = 0;
    	q.push(node(x, y));
    	for(i = 1; i <= n; i++)
    	{
    		x = read();
    		y = read();
    		map[x][y] = 1;
    		mx = max(mx, x);
    		my = max(my, y);
    	}
    	while(!q.empty())
    	{
    		u = q.front();
    		q.pop();
    		vis[u.x][u.y] = 0;
    		for(i = 0; i < 4; i++)
    		{
    			x = u.x + dx[i];
    			y = u.y + dy[i];
    			if(x >= 0 && x <= mx + 1 && y >= 0 && y <= my + 1 && dis[x][y] > dis[u.x][u.y] + map[x][y])
    			{
    				dis[x][y] = dis[u.x][u.y] + map[x][y];
    				if(!vis[x][y])
    				{
    					vis[x][y] = 1;
    					q.push(node(x, y));
    				}
    			}
    		}
    	}
    	printf("%d
    ", dis[1][1]);
    	return 0;
    }
    

      

  • 相关阅读:
    PRTG安装
    如何诊断windows性能问题
    windows性能监控
    PAM 認 證 模 組
    RHEL磁盘修复
    RHEL下修改市区
    Recover database using backup controlfile until cancel
    Cancel-Based Recovery
    北京、上海的人口并不多
    swoole中http_server的配置与使用
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/7372092.html
Copyright © 2020-2023  润新知