码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces 1340C Nastya and Unexpected Guest(01bfs)

时间:2020-04-25 17:37:26      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:exp   pop   expected   ++   amp   space   front   back   printf   

http://codeforces.com/contest/1340/problem/C

我完了,连普及的题都不会做。

\(f[i][j]\)表示在\(j\)时刻到\(i\),最少多少个红绿灯回合。

发现i只用往i-1和i+1走,设\(t=|x1-x2|\),则\(j+t<g\)\(j+t=g\)(此时回合数+1)

那么相当于有一些边权=\(0\),有一些边权\(=1\),01bfs即可。

从网上学到01bfs原来可以用deque写。

Code:

#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x, _b = y; i <= _b; i ++)
#define ff(i, x, y) for(int i = x, _b = y; i <  _b; i ++)
#define fd(i, x, y) for(int i = x, _b = y; i >= _b; i --)
#define ll long long
#define pp printf
#define hh pp("\n")
using namespace std;
 
const int N = 10005;
 
const int M = 1005;
 
int n, m, a[N], g, r;
int dis[N][M], bz[N][M];
 
struct P {
	int x, y;
	P(int _x = 0, int _y = 0) {
		x = _x, y = _y;
	}
};
 
int ans;
 
deque<P> q;
 
int main() {
	scanf("%d %d", &n, &m);
	fo(i, 1, m) scanf("%d", &a[i]);
	scanf("%d %d", &g, &r);
	sort(a + 1, a + m + 1);
	m = unique(a + 1, a + m + 1) - (a + 1);
	bz[1][0] = 1; q.push_front(P(1, 0));
	ans = 1e9;
	while(!q.empty()) {
		P b = q.front(); q.pop_front();
		int x = b.x, y = b.y;
		if(y == 0 && g >= n - a[x]) {
			ans = min(ans, dis[x][y] * (g + r) + n - a[x]);
		}
		if(x > 1) {
			int v = b.y + a[x] - a[x - 1];
			if(v < g) {
				if(!bz[x - 1][v]) {
					bz[x - 1][v] = 1;
					dis[x - 1][v] = dis[x][y];
					q.push_front(P(x - 1, v));
				}
			} else
			if(v == g) {
				if(!bz[x - 1][0]) {
					bz[x - 1][0] = 1;
					dis[x - 1][0] = dis[x][y] + 1;
					q.push_back(P(x - 1, 0));
				}
			}
		}
		if(x < m) {
			int v = b.y + a[x + 1] - a[x];
			if(v < g) {
				if(!bz[x + 1][v]) {
					bz[x + 1][v] = 1;
					dis[x + 1][v] = dis[x][y];
					q.push_front(P(x + 1, v));
				}
			} else
			if(v == g) {
				if(!bz[x + 1][0]) {
					bz[x + 1][0] = 1;
					dis[x + 1][0] = dis[x][y] + 1;
					q.push_back(P(x + 1, 0));
				}
			}
		}
	}
	pp("%d\n", ans == 1e9 ? -1 : ans);
}

Codeforces 1340C Nastya and Unexpected Guest(01bfs)

标签:exp   pop   expected   ++   amp   space   front   back   printf   

原文地址:https://www.cnblogs.com/coldchair/p/12773496.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!