【USACO 2017Feb】 Why Did the Cow Cross the Road

本文介绍了一个使用SPFA算法解决特定路径寻找问题的例子。通过定义状态dist[i][j][k]来表示走到格点(i,j)且步数除以3的余数为k的最小花费,实现了在一个n×n网格中从左上角到右下角的最短路径计算。该文详细展示了SPFA算法的具体实现过程。

【题目链接】

           点击打开链接

【算法】

         dist[i][j][k]表示当前走到(i,j),走的步数除以3的余数为k的最小花费

         spfa即可

【代码】

         

#include<bits/stdc++.h>
using namespace std;
#define MAXN 110
const int INF = 1e9;

struct info
{
		int x,y,s;
};

const int dx[4] = {0,0,-1,1};
const int dy[4] = {-1,1,0,0};

int i,j,n,t;
int val[MAXN][MAXN];

template <typename T> inline void read(T &x)
{
    int f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
    x *= f;
}
template <typename T> inline void write(T x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x/10);
    putchar(x%10+'0');
}
template <typename T> inline void writeln(T x)
{
    write(x);
    puts("");
}
bool ok(int x,int y)
{
		return x >= 1 && x <= n && y >= 1 && y <= n;
}
inline void spfa()
{
		int i,j,tx,ty,ans;
		queue< info > q;
		static int dist[MAXN][MAXN][3],inq[MAXN][MAXN][3];
		info cur;
		for (i = 1; i <= n; i++)
		{
				for (j = 1; j <= n; j++)
				{
						dist[i][j][0] = dist[i][j][1] = dist[i][j][2] = INF;
				}
		}
		dist[1][1][0] = 0;
		inq[1][1][0] = 1;
		q.push((info){1,1,0});
		while (!q.empty())
		{
				cur = q.front(); 
				inq[cur.x][cur.y][cur.s] = 0;
				q.pop();	
				for (i = 0; i < 4; i++)
				{
						tx = cur.x + dx[i];
						ty = cur.y + dy[i];
						if (ok(tx,ty))
						{
								if (!cur.s)
								{
										if (dist[cur.x][cur.y][0] + t < dist[tx][ty][1])
										{
												dist[tx][ty][1] = dist[cur.x][cur.y][0] + t;
												if (!inq[tx][ty][1])
												{
														inq[tx][ty][1] = 1;
														q.push((info){tx,ty,1});
												}
										}
								}
								if (cur.s == 1)
								{
										if (dist[cur.x][cur.y][1] + t < dist[tx][ty][2])
										{
												dist[tx][ty][2] = dist[cur.x][cur.y][1] + t;
												if (!inq[tx][ty][2])
												{
														inq[tx][ty][2] = 1;
														q.push((info){tx,ty,2});
												}
										}
								}
								if (cur.s == 2)
								{
										if (dist[cur.x][cur.y][2] + val[tx][ty] + t < dist[tx][ty][0])
										{
												dist[tx][ty][0] = dist[cur.x][cur.y][2] + val[tx][ty] + t;
												if (!inq[tx][ty][0])
												{
														inq[tx][ty][0] = 1;
														q.push((info){tx,ty,0});
												}
										}
								}
						}
				}
		}		
		ans = min(min(dist[n][n][0],dist[n][n][1]),dist[n][n][2]);
		writeln(ans);
}

int main() {
		
		read(n); read(t);
		
		for (i = 1; i <= n; i++)
		{
				for (j = 1; j <= n; j++)
				{
						read(val[i][j]);		
				}		
		}
		spfa();
		
		return 0;
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值