您好,欢迎来到年旅网。
搜索
您的当前位置:首页bzoj 3890: [Usaco2015 Jan]Meeting Time bfs

bzoj 3890: [Usaco2015 Jan]Meeting Time bfs

来源:年旅网

→←


对于每个点我们维护两个bool数组

vis[0][i][j]=true表示第j时刻Bessie来过点i

vis[0][i][j]=true表示第j时刻Elsie来过点i

然后我们从1号点开始,每当vis[0或1][i][j]==false时就变成true,并放到队列里

最后vis[0][n][1~maxm]与vis[1][n][1~maxm]都为true时,第三维的值就是答案

若不存在就输出IMPOSSIBLE,千万不要输出-1........


代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>

using namespace std;

struct node{
	int to,len[2];
};

struct node1{
	int now,len;
};

int n,m;
vector<node>v[110];
bool vis[2][110][11000];
queue<node1>q;

int main(){
	scanf("%d%d",&n,&m);
	for(int i=0; i<m; i++){
		int x,y,z1,z2;
		scanf("%d%d%d%d",&x,&y,&z1,&z2);
		if(x>y)swap(x,y);
		node t;
		t.to=y;
		t.len[0]=z1;
		t.len[1]=z2;
		v[x].push_back(t);
	}
	node1 t;
	t.now=1;
	t.len=0;
	q.push(t);
	vis[0][1][0]=true;
	vis[1][1][0]=true;
	while(!q.empty()){
		node1 t=q.front();
		q.pop();
		if(t.now==n)continue;
		for(int i=0; i<v[t.now].size(); i++){
			int to=v[t.now][i].to;
			int len1=v[t.now][i].len[0];
			if(!vis[0][to][t.len+len1]){
				vis[0][to][t.len+len1]=true;
				node1 t1=t;
				t1.now=to;
				t1.len+=len1;
				q.push(t1);
			}
		}
	}
	t.now=1;
	t.len=0;
	q.push(t);
	while(!q.empty()){
		node1 t=q.front();
		q.pop();
		if(t.now==n)continue;
		for(int i=0; i<v[t.now].size(); i++){
			int to=v[t.now][i].to;
			int len2=v[t.now][i].len[1];
			if(!vis[1][to][t.len+len2]){
				vis[1][to][t.len+len2]=true;
				node1 t1=t;
				t1.now=to;
				t1.len+=len2;
				q.push(t1);
			}
		}
	}
	for(int i=1; i<=10000; i++){
		if(vis[0][n][i] && vis[1][n][i]){
			printf("%d\n",i);
			return 0;
		}
	}
	printf("IMPOSSIBLE\n");
	
	return 0;
}


vis[0][i][j]=true表示第j时刻 Bessie来过点i

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- oldu.cn 版权所有 浙ICP备2024123271号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务